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

Expose method to get the Trace Id of an ongoing trace #60

Open
Jefftree opened this issue Aug 22, 2017 · 3 comments
Open

Expose method to get the Trace Id of an ongoing trace #60

Jefftree opened this issue Aug 22, 2017 · 3 comments
Milestone

Comments

@Jefftree
Copy link
Contributor

There a probably a few cases where it would be useful to retrieve the trace id within an ongoing trace for logging/correlating/etc. The current way to do this is to call create_http_headers_for_new_span().get('X-B3-TraceId'). This does a bit of unnecessary work with creating new span ids, copying parent id, etc.

https://github.com/Yelp/py_zipkin/blob/master/py_zipkin/zipkin.py#L538

Would it be possible to expose a method (eg: get_trace_id()) that just retrieves the trace id and omits the extra work? Again, this is probably just another micro optimization, and I'd like to hear some thoughts. The motivation for this is that I'd like to remove the dynamic (and slow) request.set_property() in pyramid zipkin, and use this new function in methods which read from the request object without adding too much complication.

https://github.com/Yelp/pyramid_zipkin/blob/master/pyramid_zipkin/request_helper.py#L139

@codefromthecrypt
Copy link

codefromthecrypt commented Aug 23, 2017 via email

@Jefftree
Copy link
Contributor Author

Thanks @adriancole. I think we can replicate that behaviour in py_zipkin.

There is already a get_zipkin_attrs() function here https://github.com/Yelp/py_zipkin/blob/master/py_zipkin/thread_local.py#L18

We could just expose this function in our main file: zipkin.py

@drolando drolando added this to the 1.0 milestone Feb 14, 2019
@dbishop
Copy link
Contributor

dbishop commented Feb 22, 2020

I found myself wanting access to the span context object after it was out of scope. A trivial example:

def foo():
    # I wish I could add an annotation or binary_annotation here!
    pass

def bar():
    with zipkin.zipkin_span(...):
        foo()

Our use-case is tracing OpenStack Swift, which is a WSGI app running in eventlet.

We monkey-patch all the get/set_default_tracer() functions with an eventlet greenthread-local-storage-aware version. This is obviously gross, so please let me know if you'd be interested in a PR to make the py_zipkin/storage.py infrastructure able to use eventlet thread-local storage.

Then, since the tracer is accessible from anywhere, we use a SpanSavingTracer subclass that provides a stack for span context objects. This works in concert with our ezipkin_span (e for "eventlet") subclass of zipkin_span which pushes and pops the span context object, as necessary.

We then created helper functions to add annotations and binary_annotations to the "current" span_context.

This is also pretty gross, and may even leak memory (not sure, just a general "spidey sense tingling" feeling).

Is there some better way to do this sort of thing?

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

4 participants