Skip to content

Commit cd5e639

Browse files
Tidy Labeler
1 parent fc74620 commit cd5e639

File tree

3 files changed

+13
-43
lines changed

3 files changed

+13
-43
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
OpenTelemetry Labeler
1717
=====================
1818
19-
The labeler utility provides a way to add custom attributes to metrics generated by OpenTelemetry instrumentors. This supports enriching metrics with context-specific information available only after HTTP requests are received, at their origin rather than requiring downstream processing or additional metric calculations.
19+
The labeler utility provides a way to add custom attributes to some metrics generated by some OpenTelemetry instrumentors.
2020
2121
This was inspired by OpenTelemetry Go's net/http instrumentation Labeler
2222
https://github.com/open-telemetry/opentelemetry-go-contrib/pull/306
2323
2424
Usage
2525
-----
2626
27-
The labeler works within the context of an instrumented request or operation. Use ``get_labeler()`` to obtain a labeler instance for the current context, then add attributes using the ``add()`` or ``add_attributes()`` methods.
27+
The labeler works within the context of an instrumented request or operation. Use ``get_labeler`` to obtain a labeler instance for the current context, then add attributes using the ``add`` or ``add_attributes`` methods.
2828
2929
Example with Flask
3030
------------------
3131
32-
Here's an example showing how to use the labeler with Flask instrumentation:
32+
Here's an example showing how to use the labeler with programmatic Flask instrumentation:
3333
3434
.. code-block:: python
3535
@@ -42,15 +42,6 @@
4242
4343
@app.route("/healthcheck")
4444
def healthcheck():
45-
# Get the labeler for the current request
46-
labeler = get_labeler()
47-
48-
labeler.add_attributes(
49-
{
50-
"endpoint_type": "healthcheck",
51-
"internal_request": True,
52-
}
53-
)
5445
return "OK"
5546
5647
@app.route("/user/<user_id>")
@@ -70,7 +61,9 @@ def user_profile(user_id):
7061
7162
return f"Got user profile for {user_id}"
7263
73-
The labeler also works with auto-instrumentation and those instrumentors that have implemented custom attributes support for metrics.
64+
The labeler also works with auto-instrumentation.
65+
66+
Custom attributes are merged by any instrumentors that use ``enhance_metric_attributes`` before their calls to report individual metrics recording, such as ``Histogram.record``. ``enchance_metrics_attributes`` does not overwrite base attributes that exist at the same keys.
7467
"""
7568

7669
from opentelemetry.instrumentation._labeler._internal import (

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ def add(self, key: str, value: Union[str, int, float, bool]) -> None:
4141
key: The attribute key
4242
value: The attribute value (must be a primitive type)
4343
"""
44-
if not isinstance(value, (str, int, float, bool)):
45-
raise ValueError(
46-
f"Attribute value must be str, int, float, or bool, got {type(value)}"
47-
)
48-
4944
with self._lock:
5045
self._attributes[key] = value
5146

@@ -58,12 +53,6 @@ def add_attributes(
5853
Args:
5954
attributes: Dictionary of attributes to add
6055
"""
61-
for key, value in attributes.items():
62-
if not isinstance(value, (str, int, float, bool)):
63-
raise ValueError(
64-
f"Attribute value for '{key}' must be str, int, float, or bool, got {type(value)}"
65-
)
66-
6756
with self._lock:
6857
self._attributes.update(attributes)
6958

@@ -140,9 +129,9 @@ def enhance_metric_attributes(
140129
"""
141130
Combines base_attributes with custom attributes from the current labeler,
142131
returning a new dictionary of attributes. Custom attributes are skipped
143-
if they would override base_attributes, exceed max_custom_attrs number,
144-
or are not simple types (str, int, float, bool). If custom attributes
145-
have string values exceeding the max_attr_value_length, then they are truncated.
132+
if they would override base_attributes, or exceed max_custom_attrs number.
133+
If custom attributes have string values exceeding the max_attr_value_length,
134+
then they are truncated.
146135
147136
Args:
148137
base_attributes: The base attributes for the metric
@@ -151,7 +140,8 @@ def enhance_metric_attributes(
151140
max_attr_value_length: Maximum length for string attribute values
152141
153142
Returns:
154-
Dictionary combining base and custom attributes
143+
Dictionary combining base and custom attributes. If no custom attributes,
144+
returns a copy of the original base attributes.
155145
"""
156146
if not include_custom:
157147
return base_attributes.copy()
@@ -172,8 +162,7 @@ def enhance_metric_attributes(
172162
if isinstance(value, str) and len(value) > max_attr_value_length:
173163
value = value[:max_attr_value_length]
174164

175-
if isinstance(value, (str, int, float, bool)):
176-
enhanced_attributes[key] = value
177-
added_count += 1
165+
enhanced_attributes[key] = value
166+
added_count += 1
178167

179168
return enhanced_attributes

opentelemetry-instrumentation/tests/test_labeler.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ def test_add_attributes_dict(self):
6161
attributes = labeler.get_attributes()
6262
self.assertEqual(attributes, attrs)
6363

64-
def test_invalid_attribute_types(self):
65-
labeler = Labeler()
66-
67-
with self.assertRaises(ValueError):
68-
labeler.add("key", [1, 2, 3])
69-
70-
with self.assertRaises(ValueError):
71-
labeler.add("key", {"nested": "dict"})
72-
73-
with self.assertRaises(ValueError):
74-
labeler.add_attributes({"key": None})
75-
7664
def test_overwrite_attribute(self):
7765
labeler = Labeler()
7866
labeler.add("key1", "original")

0 commit comments

Comments
 (0)