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

Automatically scoped span #9

Open
abh opened this issue Nov 27, 2023 · 3 comments
Open

Automatically scoped span #9

abh opened this issue Nov 27, 2023 · 3 comments

Comments

@abh
Copy link

abh commented Nov 27, 2023

This might just be another documentation request.

In my (still very limited) tests this seems like a commonly useful construct:

    my $span = $tracer->create_span(name => "foo");
    dynamically OpenTelemetry::Context->current = OpenTelemetry::Trace->context_with_span($span);

I don't know if it's possible, but something like

my $span = $tracer->create_scoped_span(name => "foo");

that'd do both in one would be a very useful (and Perl-ish?) feature.

@jjatria
Copy link
Owner

jjatria commented Nov 28, 2023

This is what the $tracer->in_span( sub ( $span, $context ) { ...} ) API aims to provide (with some additional sugar for ending spans, etc).

I'd love to have something like my $scoped_span = $tracer->magic( ... ), but I'm not sure that's possible, since I don't think that we can modify the context from within a method inside the tracer, and still make that change lexical to the scope of the caller. So you'd still have to do the dynamically otel_current_context = otel_context_with_span($span) dance after creating the span.

This would be nice to have because using in_span forces you to nest everything down a level, which is a code change. It'd be nice if you could add or remove tracing in a way that left the code untouched.

I've thought that maybe this could be done with a :Traced subroutine attribute, but I haven't really looked at that (and making that configurable might be tricky).

The alternative is something like the extremely experimental OpenTelemetry::Integration::namespace, but the interface there is very sketchy, so I wouldn't use that yet for anything but thought experiments.

I'll leave this open for now to see if maybe I'm wrong and this is doable, but I'm not sure it is.

@abh
Copy link
Author

abh commented Jan 2, 2024

I think you'd suggested it elsewhere (or used it in an example?), so I'll just add that for my use cases putting $span->end() in a defer seems to work fine. (Including recording errors or setting attributes).

Unless there are problems with that I haven't noticed yet, I think it's cleaner than the in_span() feature.

So basically I'm doing okay for the feature I was asking for; and I appreciate your work exploring "magic"! I dabbled a little with Integration::namespace, but for the project I'm instrumenting the code isn't structured well enough to get good spans. If I was doing a new project in Perl I'd try using it from the beginning and structure my code accordingly.

@jjatria
Copy link
Owner

jjatria commented Jan 5, 2024

I think for the more general case, the tricky part is the context management rather than ending the span on destruction. We can probably add that (although I'd have to check we're not going against the more obscure parts of the spec), but if the span has been attached to a context (which should be most cases) it won't actually go out of scope because a reference will be stored in the context, and we don't have a way to abstract away the dynamic scoping of the context without the manual call.

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

No branches or pull requests

2 participants