Skip to content

Commit b338833

Browse files
Update doc
1 parent cd5e639 commit b338833

File tree

1 file changed

+33
-20
lines changed
  • instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi

1 file changed

+33
-20
lines changed

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,48 @@ def GET(self):
8181
8282
Custom Metrics Attributes using Labeler
8383
***************************************
84-
The WSGI instrumentation reads from a Labeler utility that supports adding custom attributes
85-
to the HTTP duration metrics recorded by the instrumentation.
86-
84+
The WSGI instrumentation reads from a labeler utility that supports adding custom attributes
85+
to HTTP duration metrics at record time.
8786
8887
.. code-block:: python
8988
90-
from flask import Flask
89+
import web
90+
from cheroot import wsgi
91+
9192
from opentelemetry.instrumentation._labeler import get_labeler
9293
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
9394
94-
app = Flask(__name__)
95-
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
95+
urls = (
96+
'/', 'index',
97+
'/users/(.+)/', 'user_profile'
98+
)
99+
100+
class user_profile:
101+
def GET(self, user_id):
102+
# Get the labeler for the current request
103+
labeler = get_labeler()
104+
105+
# Add custom attributes to WSGI instrumentation metrics
106+
labeler.add("user_id", user_id)
107+
labeler.add("user_type", "registered")
96108
97-
@app.route("/user/<user_id>")
98-
def user_profile(user_id):
99-
# Get the labeler for the current request
100-
labeler = get_labeler()
101-
# Add custom attributes to WSGI instrumentation metrics
102-
labeler.add("user_id", user_id)
103-
labeler.add("user_type", "registered")
104-
# Or, add multiple attributes at once
105-
labeler.add_attributes({
106-
"feature_flag": "new_ui",
107-
"experiment_group": "control"
108-
})
109-
return f"User profile for {user_id}"
109+
# Or, add multiple attributes at once
110+
labeler.add_attributes({
111+
"feature_flag": "new_ui",
112+
"experiment_group": "control"
113+
})
114+
return f"User profile for {user_id}"
110115
111116
if __name__ == "__main__":
112-
app.run(debug=True)
117+
app = web.application(urls, globals())
118+
func = app.wsgifunc()
119+
120+
func = OpenTelemetryMiddleware(func)
121+
122+
server = wsgi.WSGIServer(
123+
("localhost", 5100), func, server_name="localhost"
124+
)
125+
server.start()
113126
114127
115128
Configuration

0 commit comments

Comments
 (0)