|
24 | 24 |
|
25 | 25 | class Labeler:
|
26 | 26 | """
|
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. |
29 | 29 |
|
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. |
32 | 34 | """
|
33 | 35 |
|
34 | 36 | def __init__(self):
|
@@ -134,46 +136,42 @@ def enhance_metric_attributes(
|
134 | 136 | max_attr_value_length: int = 100
|
135 | 137 | ) -> Dict[str, Any]:
|
136 | 138 | """
|
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 |
140 | 140 | from the current labeler.
|
141 | 141 |
|
| 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 | + |
142 | 147 | Args:
|
143 | 148 | base_attributes: The base attributes for the metric
|
144 | 149 | include_custom: Whether to include custom labeler attributes
|
145 | 150 | max_custom_attrs: Maximum number of custom attributes to include
|
146 | 151 | max_attr_value_length: Maximum length for string attribute values
|
147 | 152 |
|
148 | 153 | Returns:
|
149 |
| - Enhanced attributes dictionary combining base and custom attributes |
| 154 | + Dictionary combining base and custom attributes |
150 | 155 | """
|
151 | 156 | if not include_custom:
|
152 | 157 | return base_attributes.copy()
|
153 | 158 |
|
154 |
| - # Get custom attributes from labeler |
155 | 159 | custom_attributes = get_labeler_attributes()
|
156 | 160 | if not custom_attributes:
|
157 | 161 | return base_attributes.copy()
|
158 | 162 |
|
159 |
| - # Create enhanced attributes dict |
160 | 163 | enhanced_attributes = base_attributes.copy()
|
161 | 164 |
|
162 |
| - # Filter and add custom attributes with safety checks |
163 | 165 | added_count = 0
|
164 | 166 | for key, value in custom_attributes.items():
|
165 | 167 | if added_count >= max_custom_attrs:
|
166 | 168 | break
|
167 |
| - |
168 |
| - # Skip attributes that would override base attributes |
169 | 169 | if key in base_attributes:
|
170 | 170 | continue
|
171 |
| - |
172 |
| - # Apply value length limit for strings |
| 171 | + |
173 | 172 | if isinstance(value, str) and len(value) > max_attr_value_length:
|
174 | 173 | value = value[:max_attr_value_length]
|
175 |
| - |
176 |
| - # Only include safe attribute types |
| 174 | + |
177 | 175 | if isinstance(value, (str, int, float, bool)):
|
178 | 176 | enhanced_attributes[key] = value
|
179 | 177 | added_count += 1
|
|
0 commit comments