Skip to content

Commit 67e7e8f

Browse files
Docstring
1 parent a16821a commit 67e7e8f

File tree

1 file changed

+15
-17
lines changed
  • opentelemetry-instrumentation/src/opentelemetry/instrumentation/_labeler/_internal

1 file changed

+15
-17
lines changed

opentelemetry-instrumentation/src/opentelemetry/instrumentation/_labeler/_internal/__init__.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
class Labeler:
2626
"""
27-
Labeler is used to allow instrumented web applications to add custom attributes
28-
to the metrics recorded by OpenTelemetry instrumentations.
27+
Labeler can be used by instrumented code or distro to add custom attributes
28+
to the metrics recorded by those OpenTelemetry instrumentations reading it.
2929
30-
This class is thread-safe and can be used to accumulate custom attributes
31-
that will be included in OpenTelemetry metrics for the current request.
30+
Labeler accumulates custom attributes for OpenTelemetry metrics for the
31+
current request in context.
32+
33+
This feature is experimental and unstable.
3234
"""
3335

3436
def __init__(self):
@@ -134,46 +136,42 @@ def enhance_metric_attributes(
134136
max_attr_value_length: int = 100
135137
) -> Dict[str, Any]:
136138
"""
137-
Enhance metric attributes with custom labeler attributes.
138-
139-
This function combines base metric attributes with custom attributes
139+
This function combines base_attributes with custom attributes
140140
from the current labeler.
141141
142+
Custom attributes are skipped if they would override base_attributes,
143+
exceed max_custom_attrs number, or are not simple types (str, int, float,
144+
bool). If custom attributes have string values exceeding the
145+
max_attr_value_length, then they are truncated.
146+
142147
Args:
143148
base_attributes: The base attributes for the metric
144149
include_custom: Whether to include custom labeler attributes
145150
max_custom_attrs: Maximum number of custom attributes to include
146151
max_attr_value_length: Maximum length for string attribute values
147152
148153
Returns:
149-
Enhanced attributes dictionary combining base and custom attributes
154+
Dictionary combining base and custom attributes
150155
"""
151156
if not include_custom:
152157
return base_attributes.copy()
153158

154-
# Get custom attributes from labeler
155159
custom_attributes = get_labeler_attributes()
156160
if not custom_attributes:
157161
return base_attributes.copy()
158162

159-
# Create enhanced attributes dict
160163
enhanced_attributes = base_attributes.copy()
161164

162-
# Filter and add custom attributes with safety checks
163165
added_count = 0
164166
for key, value in custom_attributes.items():
165167
if added_count >= max_custom_attrs:
166168
break
167-
168-
# Skip attributes that would override base attributes
169169
if key in base_attributes:
170170
continue
171-
172-
# Apply value length limit for strings
171+
173172
if isinstance(value, str) and len(value) > max_attr_value_length:
174173
value = value[:max_attr_value_length]
175-
176-
# Only include safe attribute types
174+
177175
if isinstance(value, (str, int, float, bool)):
178176
enhanced_attributes[key] = value
179177
added_count += 1

0 commit comments

Comments
 (0)