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

fix: register lambda span #1073

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

xuan-cao-swi
Copy link
Contributor

Description

The aws lambda span was not registered with context after start_span, which cause current_span can't detect it. User couldn't append attributes outside call_wrapped span. This PR attach the span to current context and detach it before finish.

@@ -47,6 +47,9 @@ def call_wrapped(event:, context:)
kind: span_kind
)

trace_ctx = OpenTelemetry::Trace.context_with_span(span)
@trace_token = OpenTelemetry::Context.attach(trace_ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you decide to make this an instance variable?

Copy link
Contributor Author

@xuan-cao-swi xuan-cao-swi Jul 17, 2024

Choose a reason for hiding this comment

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

In case the process fail, on the ensure part, OpenTelemetry::Context.detach(@trace_token) if @trace_token won't throw error (e.g. undefined local variable or method 'trace_token' for ...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it! I understand wanting to prevent undefined errors, and I think we can achieve that with a local variable.

One option could be to update the existing guard clause to use if defined?(trace_token).

Another option could be to add a line to define trace_token earlier on in the method, like we do for span_kind, and then use if trace_token in the guard clause:

def method
  trace_token = nil
  # ... 
ensure
  OpenTelemetry::Context.detach(trace_token) if trace_token
end

Would one of these options work for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I will take the local variable, thanks!

Copy link
Contributor

@kaylareopelle kaylareopelle left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -47,6 +48,9 @@ def call_wrapped(event:, context:)
kind: span_kind
)

trace_ctx = OpenTelemetry::Trace.context_with_span(span)
trace_token = OpenTelemetry::Context.attach(trace_ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

@xuan-cao-swi I'm curious whether you tried using tracer.in_span instead of tracer.start_span and having to explicitly manage the context.

Since you're already using OpenTelemetry::Context.with_current(parent_context) in line 43 above to propagate any incoming context into the current one, in_span should be able to continue that and as a benefit record exception too. Some examples in exising "server" or "consumer" instrumentation that deal with the same concern of propagating incoming context from external process, setting it into current process's context, and starting a span.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I agree in_span is a better choice for this instrumentation. Updated!

@kaylareopelle kaylareopelle merged commit e7377e0 into open-telemetry:main Jul 23, 2024
55 checks passed
This was referenced Jul 23, 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.

3 participants