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

It should be possible to add dynamically global resource attributes #625

Closed
SpotOnDJ opened this issue Oct 9, 2024 · 2 comments
Closed

Comments

@SpotOnDJ
Copy link

SpotOnDJ commented Oct 9, 2024

Currently, the application is limited in its ability to add global Resource attributes. These attributes can only be set at the start of RUM (Real User Monitoring) sessions. Once the application has begun, there is no mechanism to dynamically add or modify these resource attributes throughout the application's lifetime.

OpenTelemetryRum.builder(application, config)
            .mergeResource(createResource())

also, there is no possibility to restart the session, because of that line of code:

public static void set(OpenTelemetry openTelemetry) {
    synchronized (mutex) {
      if (globalOpenTelemetry != null) {
        throw new IllegalStateException(
            "GlobalOpenTelemetry.set has already been called. GlobalOpenTelemetry.set must be "
                + "called only once before any calls to GlobalOpenTelemetry.get. If you are using "
                + "the OpenTelemetrySdk, use OpenTelemetrySdkBuilder.buildAndRegisterGlobal "
                + "instead. Previous invocation set to cause of this exception.",
            setGlobalCaller);
      }
      globalOpenTelemetry = new ObfuscatedOpenTelemetry(openTelemetry);
      setGlobalCaller = new Throwable();
    }
  }

It is really crucial for us to add additional global attributes dynamically in life time of application

@LikeTheSalad
Copy link
Contributor

Hi @SpotOnDJ

We currently provide a way to do what you're describing via the OtelRumConfig object using its setGlobalAttributes method. You can take a look at this line in the sample app to see how it can be used, although the example there is not exactly what you need, it shows how to prepare it all and make it work. The setGlobalAttributes method can be both static and dynamic, in the example we're making it static as we're passing an already built set of attributes, but in its "dynamic" form you'd need to pass a Supplier instead that would get queried on every new span or log creation, hence you can change its return value at any point during your app runtime which would essentially change your app's attrs dynamically. This is what it should look like:

val config = OtelRumConfig()
                .setGlobalAttributes { Attributes.of(stringKey("some_dynamic_key"), "its latest value") }

The lambda can change its returned value at any time making newer spans and logs to use the latest returned attrs.

What this approach does is intercept new spans and logs as they are created and set the provided attrs to them one by one, this is the only way we can have effectively dynamic "global attrs" in OTel as OTel Resources are immutable by design.

@SpotOnDJ
Copy link
Author

Hi @LikeTheSalad
Thank You for your detailed answer. I completely miss this method

.setGlobalAttributes { Attributes.of(stringKey("some_dynamic_key"), "its latest value") }

That is what I was looking for. Thank you again appreciate fast response

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