Skip to content

Commit

Permalink
(#9297)
Browse files Browse the repository at this point in the history
- use assertJ for unit test assertions
- use plural for method and field names
- add compile only dependency on `:javaagent-bootstrap` for `instrumentation:log4j:log4j-context-data:log4j-context-data-2.17:library-autoconfigure`
  • Loading branch information
cleverchuk committed Nov 9, 2023
1 parent 32e4a29 commit a0bf776
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
base.archivesName.set("${base.archivesName.get()}-autoconfigure")

dependencies {
compileOnly(project(":javaagent-bootstrap"))
library("org.apache.logging.log4j:log4j-core:2.17.0")

testImplementation(project(":instrumentation:log4j:log4j-context-data:log4j-context-data-common:testing"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -47,6 +48,7 @@ public Map<String, String> supplyContextData() {
contextData.put(TRACE_ID, spanContext.getTraceId());
contextData.put(SPAN_ID, spanContext.getSpanId());
contextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());
contextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes());

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.ConfiguredResourceAttributesHolder;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,6 +56,11 @@ public StringMap injectContextData(List<Property> list, StringMap stringMap) {
newContextData.putValue(SPAN_ID, currentContext.getSpanId());
newContextData.putValue(TRACE_FLAGS, currentContext.getTraceFlags().asHex());

for (Map.Entry<String, String> entry :
ConfiguredResourceAttributesHolder.getResourceAttributes().entrySet()) {
newContextData.putValue(entry.getKey(), entry.getValue());
}

if (BAGGAGE_ENABLED) {
Baggage baggage = Baggage.fromContext(context);
for (Map.Entry<String, BaggageEntry> entry : baggage.asMap().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void onExit(
spanContextData.put(SPAN_ID, spanContext.getSpanId());
spanContextData.put(TRACE_FLAGS, spanContext.getTraceFlags().asHex());

spanContextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttribute());
spanContextData.putAll(ConfiguredResourceAttributesHolder.getResourceAttributes());
}

if (LogbackSingletons.addBaggage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

public final class ConfiguredResourceAttributesHolder {

private static final Map<String, String> resourceAttribute = new HashMap<>();
private static final Map<String, String> resourceAttributes = new HashMap<>();

public static Map<String, String> getResourceAttribute() {
return resourceAttribute;
public static Map<String, String> getResourceAttributes() {
return resourceAttributes;
}

public static void initialize(Attributes resourceAttribute) {
Expand All @@ -27,8 +27,7 @@ public static void initialize(Attributes resourceAttribute) {
for (String key : mdcResourceAttributes) {
String value = resourceAttribute.get(stringKey(key));
if (value != null) {
ConfiguredResourceAttributesHolder.resourceAttribute.put(
String.format("otel.resource.%s", key), value);
ConfiguredResourceAttributesHolder.resourceAttributes.put(key, value);
}
}
}
Expand All @@ -44,7 +43,7 @@ private static String[] getConfiguredAttributes() {

@Nullable
public static String getAttributeValue(String key) {
return resourceAttribute.get(String.format("otel.resource.%s", key));
return resourceAttributes.get(key);
}

private ConfiguredResourceAttributesHolder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

package io.opentelemetry.javaagent.bootstrap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Expand All @@ -22,11 +21,14 @@ class ConfiguredResourceAttributesHolderTest {
key = "otel.instrumentation.mdc.resource-attributes",
value = "service.name,runtime")
void testGetAttributeValue() {
Attributes attributes = Attributes.builder().put("service.name", "test-service").build();
Attributes attributes =
Attributes.builder().put("service.name", "test-service").put("runtime", "JVM").build();

ConfiguredResourceAttributesHolder.initialize(attributes);
assertEquals(
"test-service", ConfiguredResourceAttributesHolder.getAttributeValue("service.name"));

assertThat(ConfiguredResourceAttributesHolder.getAttributeValue("service.name"))
.isEqualTo("test-service");
assertThat(ConfiguredResourceAttributesHolder.getAttributeValue("runtime")).isEqualTo("JVM");
}

@Test
Expand All @@ -38,7 +40,7 @@ void testGetAttributeValueWhenKeyIsNotString() {
.build();

ConfiguredResourceAttributesHolder.initialize(attributes);
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("items"));
assertThat(ConfiguredResourceAttributesHolder.getAttributeValue("items")).isNull();
}

@Test
Expand All @@ -48,6 +50,6 @@ void testGetAttributeValueWhenConfigIsNotSet() {
Attributes.builder().put(AttributeKey.stringArrayKey("don't care"), "won't care").build();

ConfiguredResourceAttributesHolder.initialize(attributes);
assertNull(ConfiguredResourceAttributesHolder.getAttributeValue("dc-wc"));
assertThat(ConfiguredResourceAttributesHolder.getAttributeValue("dc-wc")).isNull();
}
}

0 comments on commit a0bf776

Please sign in to comment.