Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.incubator.common;

import static java.util.Objects.requireNonNull;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;

/**
* Utility for associating {@link Attributes} with a {@link Context} for automatic propagation to
* telemetry signals produced within that context.
*/
public final class ContextAttributes {

private static final ContextKey<Attributes> CONTEXT_ATTRIBUTES_KEY =
ContextKey.named("opentelemetry-context-attributes");

private ContextAttributes() {}

/**
* Returns a new {@link Context} containing the merged attributes from the given {@code context}
* and {@code attributes}. If the same attribute key is present in both, the value from {@code
* attributes} takes precedence.
*/
public static Context add(Context context, Attributes attributes) {
requireNonNull(context, "context");
if (attributes == null || attributes.isEmpty()) {
return context;
}
Attributes existing = context.get(CONTEXT_ATTRIBUTES_KEY);
if (existing == null) {
return context.with(CONTEXT_ATTRIBUTES_KEY, attributes);
}
Attributes merged = existing.toBuilder().putAll(attributes).build();
return context.with(CONTEXT_ATTRIBUTES_KEY, merged);
}

/**
* Returns the {@link Attributes} associated with the given {@code context}, or {@link
* Attributes#empty()} if none are present.
*
* <p>This method is intended primarily for telemetry pipeline components such as SDKs, samplers,
* processors, and exporters. Application code should treat context-scoped attributes as
* write-only via {@link #add(Context, Attributes)}.
*/
public static Attributes get(Context context) {
if (context == null) {
return Attributes.empty();
}
Attributes attributes = context.get(CONTEXT_ATTRIBUTES_KEY);
return attributes != null ? attributes : Attributes.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.incubator.common;

import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import org.junit.jupiter.api.Test;

class ContextAttributesTest {

@Test
void emptyContext_returnsEmptyAttributes() {
assertThat(ContextAttributes.get(Context.root())).isEqualTo(Attributes.empty());
assertThat(ContextAttributes.get(null)).isEqualTo(Attributes.empty());
}

@Test
void addNullOrEmptyAttributes_returnsSameContext() {
Context root = Context.root();
assertThat(ContextAttributes.add(root, null)).isSameAs(root);
assertThat(ContextAttributes.add(root, Attributes.empty())).isSameAs(root);
assertThatThrownBy(() -> ContextAttributes.add(null, Attributes.of(stringKey("k"), "v")))
.isInstanceOf(NullPointerException.class);
}

@Test
void addAndGet_singleLayer() {
Attributes attrs = Attributes.of(stringKey("user.id"), "u123", longKey("retry"), 1L);
Context ctx = ContextAttributes.add(Context.root(), attrs);

assertThat(ContextAttributes.get(ctx)).isEqualTo(attrs);
assertThat(ContextAttributes.get(Context.root())).isEqualTo(Attributes.empty());
}

@Test
void addAndGet_nestedContextsMergeAndOverride() {
Context outer =
ContextAttributes.add(
Context.root(), Attributes.of(stringKey("k1"), "v1", stringKey("k2"), "v2"));
Context inner =
ContextAttributes.add(
outer, Attributes.of(stringKey("k2"), "v2-override", stringKey("k3"), "v3"));

assertThat(ContextAttributes.get(outer))
.isEqualTo(Attributes.of(stringKey("k1"), "v1", stringKey("k2"), "v2"));
assertThat(ContextAttributes.get(inner))
.isEqualTo(
Attributes.of(
stringKey("k1"), "v1", stringKey("k2"), "v2-override", stringKey("k3"), "v3"));
}

@Test
void scopeAttachment_restoresOuterContextAttributesOnClose() {
Context outer = ContextAttributes.add(Context.root(), Attributes.of(stringKey("env"), "prod"));
try (Scope ignoredOuter = outer.makeCurrent()) {
assertThat(ContextAttributes.get(Context.current()))
.isEqualTo(Attributes.of(stringKey("env"), "prod"));

Context inner =
ContextAttributes.add(
Context.current(),
Attributes.of(stringKey("env"), "staging", stringKey("req"), "r1"));
try (Scope ignoredInner = inner.makeCurrent()) {
assertThat(ContextAttributes.get(Context.current()))
.isEqualTo(Attributes.of(stringKey("env"), "staging", stringKey("req"), "r1"));
}

assertThat(ContextAttributes.get(Context.current()))
.isEqualTo(Attributes.of(stringKey("env"), "prod"));
}
assertThat(ContextAttributes.get(Context.current())).isEqualTo(Attributes.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public SdkLoggerProviderBuilder create(
SdkLoggerProviderUtil.setLoggerConfigurator(builder, configuratorBuilder.build());
}

Boolean contextScopedAttributes =
LoggerProviderModelAccessor.getContextScopedAttributes(loggerProviderModel);
if (contextScopedAttributes != null) {
SdkLoggerProviderUtil.setContextScopedAttributesEnabled(builder, contextScopedAttributes);
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public SdkMeterProviderBuilder create(
ExemplarFilterFactory.getInstance().create(exemplarFilterModel, context));
}

Boolean contextScopedAttributes = MeterProviderModelAccessor.getContextScopedAttributes(model);
if (contextScopedAttributes != null) {
SdkMeterProviderUtil.setContextScopedAttributesEnabled(builder, contextScopedAttributes);
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public SdkTracerProviderBuilder create(
SdkTracerProviderUtil.setTracerConfigurator(builder, configuratorBuilder.build());
}

Boolean contextScopedAttributes =
TracerProviderModelAccessor.getContextScopedAttributes(tracerProviderModel);
if (contextScopedAttributes != null) {
SdkTracerProviderUtil.setContextScopedAttributesEnabled(builder, contextScopedAttributes);
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public final class LoggerProviderModelAccessor {
private LoggerProviderModelAccessor() {}

static final String LOGGER_CONFIGURATOR = "logger_configurator/development";
static final String CONTEXT_SCOPED_ATTRIBUTES = "context_scoped_attributes/development";

public static final Map<String, Class<?>> EXPERIMENTAL_PROPERTIES;

static {
EXPERIMENTAL_PROPERTIES = new HashMap<>();
EXPERIMENTAL_PROPERTIES.put(LOGGER_CONFIGURATOR, ExperimentalLoggerConfiguratorModel.class);
EXPERIMENTAL_PROPERTIES.put(CONTEXT_SCOPED_ATTRIBUTES, Boolean.class);
}

@Nullable
Expand All @@ -47,4 +49,17 @@ public static LoggerProviderModel setLoggerConfigurator(
model.setExtensionProperty(LOGGER_CONFIGURATOR, value);
return model;
}

@Nullable
public static Boolean getContextScopedAttributes(LoggerProviderModel model) {
return ExtensionPropertyUtil.get(
CONTEXT_SCOPED_ATTRIBUTES, model.getExtensionProperties(), Boolean.class);
}

public static LoggerProviderModel setContextScopedAttributes(
LoggerProviderModel model, Boolean value) {
requireNonNull(value, "value");
model.setExtensionProperty(CONTEXT_SCOPED_ATTRIBUTES, value);
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ private MeterProviderModelAccessor() {}

static final String METER_CONFIGURATOR = "meter_configurator/development";
static final String VIEW_MATCHING_MODE = "view_matching_mode/development";
static final String CONTEXT_SCOPED_ATTRIBUTES = "context_scoped_attributes/development";

public static final Map<String, Class<?>> EXPERIMENTAL_PROPERTIES;

static {
EXPERIMENTAL_PROPERTIES = new HashMap<>();
EXPERIMENTAL_PROPERTIES.put(METER_CONFIGURATOR, ExperimentalMeterConfiguratorModel.class);
EXPERIMENTAL_PROPERTIES.put(VIEW_MATCHING_MODE, ExperimentalViewMatchingModeModel.class);
EXPERIMENTAL_PROPERTIES.put(CONTEXT_SCOPED_ATTRIBUTES, Boolean.class);
}

@Nullable
Expand Down Expand Up @@ -63,4 +65,17 @@ public static MeterProviderModel setViewMatchingMode(
model.setExtensionProperty(VIEW_MATCHING_MODE, value);
return model;
}

@Nullable
public static Boolean getContextScopedAttributes(MeterProviderModel model) {
return ExtensionPropertyUtil.get(
CONTEXT_SCOPED_ATTRIBUTES, model.getExtensionProperties(), Boolean.class);
}

public static MeterProviderModel setContextScopedAttributes(
MeterProviderModel model, Boolean value) {
requireNonNull(value, "value");
model.setExtensionProperty(CONTEXT_SCOPED_ATTRIBUTES, value);
return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public final class TracerProviderModelAccessor {
private TracerProviderModelAccessor() {}

static final String TRACER_CONFIGURATOR = "tracer_configurator/development";
static final String CONTEXT_SCOPED_ATTRIBUTES = "context_scoped_attributes/development";

public static final Map<String, Class<?>> EXPERIMENTAL_PROPERTIES;

static {
EXPERIMENTAL_PROPERTIES = new HashMap<>();
EXPERIMENTAL_PROPERTIES.put(TRACER_CONFIGURATOR, ExperimentalTracerConfiguratorModel.class);
EXPERIMENTAL_PROPERTIES.put(CONTEXT_SCOPED_ATTRIBUTES, Boolean.class);
}

@Nullable
Expand All @@ -47,4 +49,17 @@ public static TracerProviderModel setTracerConfigurator(
model.setExtensionProperty(TRACER_CONFIGURATOR, value);
return model;
}

@Nullable
public static Boolean getContextScopedAttributes(TracerProviderModel model) {
return ExtensionPropertyUtil.get(
CONTEXT_SCOPED_ATTRIBUTES, model.getExtensionProperties(), Boolean.class);
}

public static TracerProviderModel setContextScopedAttributes(
TracerProviderModel model, Boolean value) {
requireNonNull(value, "value");
model.setExtensionProperty(CONTEXT_SCOPED_ATTRIBUTES, value);
return model;
}
}
Loading
Loading