feature: add open telemetry integration#3
Conversation
1a9f857 to
fff33eb
Compare
caseylocker
left a comment
There was a problem hiding this comment.
You're defining randomString in api/tests/open_telemetry_instrumentation_tests.py but not using it:
class OpenTelemetryInstrumentationTest(APITestCase):
def randomString(self, str_len):
...
Wrapt was downgraded in the requirements.txt. Was that intentional?
wrapt==2.0.1 -> wrapt==1.17.3
@caseylocker I removed this code you are right, its not been used, this was to generate a random string for the access_token we use to authenticate the requests on the other endpoints but here that does not happen
@caseylocker it was on propuse because newer versions create dependency conflicts when we try to install the open telemetry instrumentation libraries |
caseylocker
left a comment
There was a problem hiding this comment.
LGTM. I've confirmed that the wrapt downgrade was a requirement by the Opentelementry instrumentation packages. They explicitly require wrapt<2.0.0. No other packages require wrapt 2.x
smarcet
left a comment
There was a problem hiding this comment.
@fcarreno-collab please review comments
|
@smarcet please can you review this. |
smarcet
left a comment
There was a problem hiding this comment.
@fcarreno-collab please review
36aa1c2 to
5ed2630
Compare
|
@smarcet changes related your comments already on PR |
smarcet
left a comment
There was a problem hiding this comment.
@fcarreno-collab please review comments
|
@smarcet branch updated with requested changes |
feature: move settings import and update tests
36e886e to
221f64b
Compare
|
@smarcet please review again as all the requested/agreed changes were incorporated. |
| return | ||
|
|
||
| # Attach CF-Ray header | ||
| cf_ray = span.set_attribute("cf.ray_id", request.headers.get("Cf-Ray")) |
There was a problem hiding this comment.
span.set_attribute() returns None, so cf_ray is always falsy and the second attribute is never set. The cf.ray_id attribute is written correctly (as a side-effect of the first call), so
the test test_cf_ray_header passes despite this bug, masking it.
|
|
||
| DEV_EMAIL = os.getenv('DEV_EMAIL') | ||
|
|
||
| OTEL_INSTRUMENTATION_ENABLED = env_bool('OTEL_INSTRUMENTATION_ENABLED', True) |
There was a problem hiding this comment.
Any environment without this variable explicitly set to false will try to instrument. If OTEL_EXPORTER_OTLP_ENDPOINT is not configured, otel_endpoint mode will fail silently or log errors
on every request. Defaulting to False is safer.
|
It's ready to review @romanetar |
| if cf_ray: | ||
| span.set_attribute("cf.ray_id", cf_ray) | ||
| if cf_ray: | ||
| span.set_attribute("http.request.header.cf-ray", cf_ray) | ||
|
|
||
| # Attach baggage if present | ||
| baggage_val = baggage_api.get_baggage("cf.ray_id") | ||
| if baggage_val: | ||
| span.set_attribute("baggage.cf.ray_id", baggage_val) |
There was a problem hiding this comment.
@andrestejerina97
The cf.ray_id baggage fallback that shipped in mailing-api (PR #5) is missing here. When a request carries cf.ray_id via OTEL baggage but has no Cf-Ray header — e.g. service-to-service calls where the Cloudflare edge header isn't forwarded — the span never gets the primary cf.ray_id attribute (only baggage.cf.ray_id), so it won't correlate with upstream spans.
Suggested fix to match mailing-api's request_hook (also collapses the two redundant if cf_ray: blocks into one):
| if cf_ray: | |
| span.set_attribute("cf.ray_id", cf_ray) | |
| if cf_ray: | |
| span.set_attribute("http.request.header.cf-ray", cf_ray) | |
| # Attach baggage if present | |
| baggage_val = baggage_api.get_baggage("cf.ray_id") | |
| if baggage_val: | |
| span.set_attribute("baggage.cf.ray_id", baggage_val) | |
| if cf_ray: | |
| span.set_attribute("cf.ray_id", cf_ray) | |
| span.set_attribute("http.request.header.cf-ray", cf_ray) | |
| # Attach baggage if present | |
| baggage_val = baggage_api.get_baggage("cf.ray_id") | |
| if baggage_val: | |
| if not cf_ray: | |
| span.set_attribute("cf.ray_id", baggage_val) | |
| span.set_attribute("baggage.cf.ray_id", baggage_val) |
Also worth covering the header-absent path in tests: test_traceparent_and_baggage injects baggage without a Cf-Ray header but only asserts baggage.cf.ray_id, so this gap is currently untested — add an assertion that cf.ray_id == "xyz" there.
smarcet
left a comment
There was a problem hiding this comment.
@andrestejerina97 please review comments
ref: https://app.clickup.com/t/86b7bdn9g