Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up typing for OTel #3168

Merged
merged 28 commits into from
Jun 26, 2024
Merged

Set up typing for OTel #3168

merged 28 commits into from
Jun 26, 2024

Conversation

sentrivana
Copy link
Contributor

@sentrivana sentrivana commented Jun 13, 2024

TBH I'm not sure if this is the way to go (with all the casts), but all my other attempts at narrowing down types were unsuccessful.

@sentrivana sentrivana linked an issue Jun 19, 2024 that may be closed by this pull request
Comment on lines -56 to -59
trace_id = format_trace_id(ctx.trace_id)
span_id = format_span_id(ctx.span_id)

if trace_id == INVALID_TRACE_ID or span_id == INVALID_SPAN_ID:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INVALID_TRACE_ID and INVALID_SPAN_ID are integers while trace_id and span_id are strings, so this would never be True.

@sentrivana sentrivana marked this pull request as ready for review June 20, 2024 11:29
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the otel_span.attributes.get() does not have a return type that "just works" for everything?

Comment on lines 133 to 135
if parent_context is None:
return

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this new behavior?

Copy link
Contributor Author

@sentrivana sentrivana Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do something about parent_context being possibly None because technically speaking, the next line, _get_trace_data, doesn't support parent_context being None.

I thought that without parent_context we can't really do anything after this, but double checking now I realize that's wrong, will fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here: 4a4ca4a

Comment on lines +338 to +344
peer_name = otel_span.attributes.get(SpanAttributes.NET_PEER_NAME, None)
if peer_name:
description += " {}".format(peer_name)

target = otel_span.attributes.get(SpanAttributes.HTTP_TARGET, None)
if target:
description += " {}".format(target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there no cast here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because mypy doesn't complain 🙃

Prolly because target is only used in the format string and whatever type it is, it'll have a __str__ defined, so no type issues there. Whereas for example for http_method above, it's __add__ed to a string which is only supported for strings, however mypy wasn't able to infer that it is in fact a string.

sentry_sdk/integrations/opentelemetry/span_processor.py Outdated Show resolved Hide resolved
@sentrivana
Copy link
Contributor Author

sentrivana commented Jun 24, 2024

I guess the otel_span.attributes.get() does not have a return type that "just works" for everything?

Ah sorry, missed this question last week.

otel_span.attributes is of type Attributes (see here), which is an optional mapping of str to basically anything. The individual attributes though all have a specific type implicitly (e.g. SpanAttributes.HTTP_URL can never be a float or int), but mypy doesn't know this, so when you try to use an attribute in a function that expects a specific type, mypy will complain, because officially the attribute is of type

Union[
    str,
    bool,
    int,
    float,
    Sequence[str],
    Sequence[bool],
    Sequence[int],
    Sequence[float],
]

So you need to explicitly narrow the type down and tell mypy that a specific attribute can only be a subset of the Union so that its type matches the function you want to use it in.

Base automatically changed from ivana/potel/autoinstrumentation to master June 26, 2024 08:12
@sentrivana sentrivana merged commit 7c1685e into master Jun 26, 2024
121 checks passed
@sentrivana sentrivana deleted the ivana/potel/typing branch June 26, 2024 08:53
antonpirker added a commit that referenced this pull request Jun 27, 2024
sentrivana added a commit that referenced this pull request Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include OTel in type checks
2 participants