| 
13 | 13 | # limitations under the License.  | 
14 | 14 | 
 
  | 
15 | 15 | """  | 
16 |  | -Labeler that supports per-request custom attribute addition to web framework  | 
17 |  | -instrumentor-originating OpenTelemetry metrics.  | 
 | 16 | +OpenTelemetry Labeler  | 
 | 17 | +=====================  | 
 | 18 | +
  | 
 | 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.  | 
18 | 20 | 
  | 
19 | 21 | This was inspired by OpenTelemetry Go's net/http instrumentation Labeler  | 
20 | 22 | https://github.com/open-telemetry/opentelemetry-go-contrib/pull/306  | 
 | 23 | +
  | 
 | 24 | +Usage  | 
 | 25 | +-----  | 
 | 26 | +
  | 
 | 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.  | 
 | 28 | +
  | 
 | 29 | +Example with Flask  | 
 | 30 | +------------------  | 
 | 31 | +
  | 
 | 32 | +Here's an example showing how to use the labeler with Flask instrumentation:  | 
 | 33 | +
  | 
 | 34 | +.. code-block:: python  | 
 | 35 | +
  | 
 | 36 | +    from flask import Flask  | 
 | 37 | +    from opentelemetry.instrumentation._labeler import get_labeler  | 
 | 38 | +    from opentelemetry.instrumentation.flask import FlaskInstrumentor  | 
 | 39 | +
  | 
 | 40 | +    app = Flask(__name__)  | 
 | 41 | +    FlaskInstrumentor().instrument_app(app)  | 
 | 42 | +
  | 
 | 43 | +    @app.route("/healthcheck")  | 
 | 44 | +    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 | +        )  | 
 | 54 | +        return "OK"  | 
 | 55 | +
  | 
 | 56 | +    @app.route("/user/<user_id>")  | 
 | 57 | +    def user_profile(user_id):  | 
 | 58 | +        labeler = get_labeler()  | 
 | 59 | +
  | 
 | 60 | +        # Can add individual attributes or multiple at once  | 
 | 61 | +        labeler.add("user_id", user_id)  | 
 | 62 | +        labeler.add_attributes(  | 
 | 63 | +            {  | 
 | 64 | +                "has_premium": user_id in ["123", "456"],  | 
 | 65 | +                "experiment_group": "control",  | 
 | 66 | +                "feature_enabled": True,  | 
 | 67 | +                "user_segment": "active",  | 
 | 68 | +            }  | 
 | 69 | +        )  | 
 | 70 | +
  | 
 | 71 | +        return f"Got user profile for {user_id}"  | 
 | 72 | +
  | 
 | 73 | +The labeler also works with auto-instrumentation.  | 
21 | 74 | """  | 
22 | 75 | 
 
  | 
23 | 76 | from opentelemetry.instrumentation._labeler._internal import (  | 
 | 
0 commit comments