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

Add ObservationAwareSpanThreadLocalAccessor constructor taking ObservationRegistry #240

Conversation

acktsap
Copy link
Contributor

@acktsap acktsap commented Apr 24, 2023

This pr adds constructor with ObservationRegistry parameter for api flexibility.

…ationRegistry

Signed-off-by: Taeik Lim <sibera21@gmail.com>
@marcingrzejszczak
Copy link
Contributor

Hey, thanks for the PR. What's your use case that you need to add the ObservationRegistry on the OASTLA ?

@acktsap
Copy link
Contributor Author

acktsap commented Apr 24, 2023

Hi @marcingrzejszczak

Just for decorating purpose.

e.g.

final ObservationRegistry observationRegistry = new ObservationRegistry() {

    private ObservationRegistry delegate = ObservationRegistry.create();

    @Override
    public Observation getCurrentObservation() {
        /* do something here */
        return delegate.getCurrentObservation();
    }

    @Override
    public Observation.Scope getCurrentObservationScope() {
        return delegate.getCurrentObservationScope();
    }

    @Override
    public void setCurrentObservationScope(Observation.Scope current) {
        delegate.setCurrentObservationScope(current);
    }

    @Override
    public ObservationConfig observationConfig() {
        return delegate.observationConfig();
    }
};

@marcingrzejszczak
Copy link
Contributor

Technically i understand why, but why would you even want to decorate it?

@acktsap
Copy link
Contributor Author

acktsap commented Apr 25, 2023

Not a critical one. I just want to track if it has currentObservation by log.

This is code snippet.

@Bean
fun observationRegistry(tracer: Tracer): ObservationRegistry {
    val delegate = ObservationRegistry.create().apply {
        observationConfig().observationHandler(DefaultTracingObservationHandler(tracer))
    }
    return object : ObservationRegistry by delegate {
        override fun getCurrentObservation(): Observation? {
            return delegate.currentObservation.also {
                logger.debug { "Current observation: $it" }
            }
        }
    }
}

@Bean
fun contextRegistry(observationRegistry: ObservationRegistry, tracer: Tracer): ContextRegistry {
    return ContextRegistry().apply {
        loadThreadLocalAccessors()
            .registerThreadLocalAccessor(ObservationAwareSpanThreadLocalAccessor(observationRegistry, tracer)) // use decorated one
    }
}

I want to track which value is return by this code.

Apart from that, it seems nice to taking ObservationRegistry parameter not just creating SimpleObservationRegistry.

@marcingrzejszczak marcingrzejszczak added the enhancement New feature or request label Apr 25, 2023
@marcingrzejszczak marcingrzejszczak added this to the 1.1.0 milestone Apr 25, 2023
@marcingrzejszczak marcingrzejszczak merged commit 6c8384e into micrometer-metrics:main Apr 25, 2023
7 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants