Skip to content

Commit

Permalink
Add Apolloconfig Inst
Browse files Browse the repository at this point in the history
  • Loading branch information
freshchen committed Nov 29, 2024
1 parent f75200a commit 83ce10e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Void> INSTRUMENTER;

private static final AttributeKey<String> CONFIG_NS_ATTRIBUTE_KEY = stringKey("config.namespace");
Expand Down Expand Up @@ -51,17 +51,17 @@ public void onEnd(
@Nullable Throwable error) {}
};

SpanStatusExtractor<String, Void> spanStatusExtractor =
(spanStatusBuilder, request, unused, error) -> {
if (error != null) {
spanStatusBuilder.setStatus(StatusCode.ERROR);
}
};

INSTRUMENTER =
Instrumenter.<String, Void>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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -39,17 +37,15 @@ void test() {
}

private static void checkRepositoryChange(String namespace) {
String spanName = "Apollo Config Repository Change";
List<AttributeAssertion> 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 {
Expand Down

0 comments on commit 83ce10e

Please sign in to comment.