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

otel-bridge: add trace_flags to MDC #229

Closed
olivierboudet opened this issue Apr 11, 2023 · 7 comments
Closed

otel-bridge: add trace_flags to MDC #229

olivierboudet opened this issue Apr 11, 2023 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@olivierboudet
Copy link

In Slf4JEventListenerof micrometer-tracing-otel-bridge, span_id and trace_id are added to MDC.


Would it be possible to add also trace_flags which allow to know when a request is sampled (see https://www.w3.org/TR/trace-context/#trace-flags)

@jonatan-ivanov
Copy link
Member

I think so (we used to have it in Sleuth) but what would be the use-case for that?
Can you get the whole span through the Tracer: tracer.currentSpan()?

@olivierboudet
Copy link
Author

olivierboudet commented Apr 11, 2023

I would like to log the traceFlags field to know if the trace is sampled and if it worth searching it in storage backend. I am currently using the opentelemetry java agent and have a log pattern like this :
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %X{correlation-id:--} %X{trace_id:--} %X{trace_flags:--} %clr(${LOG_LEVEL_PATTERN:%5p}) %clr(${PID:0}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}
I am just trying to have a similar behavior with micrometer tracing (with Spring Boot).

@jonatan-ivanov
Copy link
Member

Let me ping @marcingrzejszczak to get some background about why this was removed from Sleuth.

@marcingrzejszczak
Copy link
Contributor

There's no point in having this in the logs out of the box. If we're talking about log correlation it doesn't matter if the span is sampled or not.

@olivierboudet
Copy link
Author

olivierboudet commented Apr 12, 2023

If it does not make sense to have it out of the box, can you explain a bit how can I have it with custom code please ?

I though to write a custom EventListener to insert traceFlags information in MDC put all methods of Scope*Event are protected.
I am trying to do something like that :

package com.cooperl.csuite.pass.eleveur.common;

import io.micrometer.tracing.otel.bridge.EventListener;
import io.micrometer.tracing.otel.bridge.EventPublishingContextWrapper;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Component
public class SLF4JEventListener implements EventListener {

    public static final String TRACE_FLAGS = "trace_flags";

    @Override
    public void onEvent(Object event) {
        if (event instanceof EventPublishingContextWrapper.ScopeAttachedEvent) {
            onScopeAttached((EventPublishingContextWrapper.ScopeAttachedEvent) event);
        }
        else if (event instanceof EventPublishingContextWrapper.ScopeClosedEvent) {
            onScopeClosed();
        }
        else if (event instanceof EventPublishingContextWrapper.ScopeRestoredEvent) {
            onScopeRestored((EventPublishingContextWrapper.ScopeRestoredEvent) event);
        }
    }

    private void onScopeClosed() {
        MDC.remove(TRACE_FLAGS);
    }

    private void onScopeAttached(EventPublishingContextWrapper.ScopeAttachedEvent event) {
        MDC.put(TRACE_FLAGS,  String.valueOf(event.getSpan().getSpanContext().getTraceFlags())); // does not work, getSpan() is not public

    }

    private void onScopeRestored(EventPublishingContextWrapper.ScopeRestoredEvent event) {
        MDC.put(TRACE_FLAGS,  String.valueOf(event.getSpan().getSpanContext().getTraceFlags())); // does not work, getSpan() is not public
    }
}

@marcingrzejszczak marcingrzejszczak added the enhancement New feature or request label Apr 12, 2023
@marcingrzejszczak marcingrzejszczak added this to the 1.1.0 milestone Apr 12, 2023
@marcingrzejszczak
Copy link
Contributor

I've opened the scope of getters for tracing 1.1.0. Until then you need to either use reflection or create a class in the io.micrometer.tracing.otel.bridge that will be able to access the package scope methods.

@olivierboudet
Copy link
Author

Thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants