From 83ce10eb52a2b03f86f19346abeeebf8cc8e218f Mon Sep 17 00:00:00 2001 From: freshchen <961011595@qq.com> Date: Fri, 29 Nov 2024 19:37:29 +0800 Subject: [PATCH] Add Apolloconfig Inst https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12787 --- .../v1_0/ApolloConfigSingletons.java | 22 +++++++++---------- .../v1_0/ApolloRepositoryChangeTest.java | 16 +++++--------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloConfigSingletons.java b/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloConfigSingletons.java index 5d514cf6402d..c57acc05bdda 100644 --- a/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloConfigSingletons.java +++ b/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloConfigSingletons.java @@ -16,12 +16,12 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; import javax.annotation.Nullable; public final class ApolloConfigSingletons { - private static final String NAME = "io.opentelemetry.apolloconfig-apolloclient-1.0"; + private static final String INSTRUMENTATION_NAME = + "io.opentelemetry.apolloconfig-apolloclient-1.0"; private static final Instrumenter INSTRUMENTER; private static final AttributeKey CONFIG_NS_ATTRIBUTE_KEY = stringKey("config.namespace"); @@ -51,17 +51,17 @@ public void onEnd( @Nullable Throwable error) {} }; - SpanStatusExtractor spanStatusExtractor = - (spanStatusBuilder, request, unused, error) -> { - if (error != null) { - spanStatusBuilder.setStatus(StatusCode.ERROR); - } - }; - INSTRUMENTER = Instrumenter.builder( - GlobalOpenTelemetry.get(), NAME, (event) -> "Apollo Config Repository Change") - .setSpanStatusExtractor(spanStatusExtractor) + GlobalOpenTelemetry.get(), + INSTRUMENTATION_NAME, + (event) -> "Apollo Config Repository Change") + .setSpanStatusExtractor( + (spanStatusBuilder, request, unused, error) -> { + if (error != null) { + spanStatusBuilder.setStatus(StatusCode.ERROR); + } + }) .addAttributesExtractor(attributesExtractor) .buildInstrumenter(SpanKindExtractor.alwaysClient()); } diff --git a/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloRepositoryChangeTest.java b/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloRepositoryChangeTest.java index 0d9d09ae3e72..2e6b612645cb 100644 --- a/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloRepositoryChangeTest.java +++ b/instrumentation/apolloconfig-apolloclient-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apolloconfig/apolloclient/v1_0/ApolloRepositoryChangeTest.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.apolloconfig.apolloclient.v1_0; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static java.util.Collections.singletonList; import com.ctrip.framework.apollo.enums.ConfigSourceType; import com.ctrip.framework.apollo.internals.AbstractConfigRepository; @@ -15,9 +16,6 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; -import java.util.ArrayList; -import java.util.List; import java.util.Properties; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -28,7 +26,7 @@ class ApolloRepositoryChangeTest { private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); @Test - void test() { + void fireRepositoryChangeTest() { String namespace = "application"; TestConfigRepository testConfigRepository = new TestConfigRepository(namespace); @@ -39,17 +37,15 @@ void test() { } private static void checkRepositoryChange(String namespace) { - String spanName = "Apollo Config Repository Change"; - List attributeAssertions = new ArrayList<>(); - attributeAssertions.add(equalTo(AttributeKey.stringKey("config.namespace"), namespace)); - testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( span -> span.hasKind(SpanKind.CLIENT) - .hasName(spanName) - .hasAttributesSatisfyingExactly(attributeAssertions))); + .hasName("Apollo Config Repository Change") + .hasAttributesSatisfyingExactly( + singletonList( + equalTo(AttributeKey.stringKey("config.namespace"), namespace))))); } static class TestConfigRepository extends AbstractConfigRepository {