-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
instrumentation/apolloconfig/apolloconfig-2.0.0/javaagent/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("com.ctrip.framework.apollo") | ||
module.set("apollo-client") | ||
versions.set("[2.0.0,2.3.0]") | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("com.google.auto.value:auto-value-annotations") | ||
annotationProcessor("com.google.auto.value:auto-value") | ||
|
||
library("com.ctrip.framework.apollo:apollo-client:2.0.0") | ||
|
||
testImplementation(project(":instrumentation:apolloconfig:apolloconfig-2.0.0:testing")) | ||
|
||
latestDepTestLibrary("com.ctrip.framework.apollo:apollo-client:2.0.+") | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
// required on jdk17 | ||
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") | ||
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") | ||
} |
24 changes: 24 additions & 0 deletions
24
...etry/javaagent/instrumentation/apolloconfig/v2_0_0/ApolloConfigInstrumentationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class ApolloConfigInstrumentationModule extends InstrumentationModule { | ||
public ApolloConfigInstrumentationModule() { | ||
super("apolloconfig", "apolloconfig-2.0.0"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new ApolloRepositoryChangeInstrumentation()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...o/opentelemetry/javaagent/instrumentation/apolloconfig/v2_0_0/ApolloConfigSingletons.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0; | ||
|
||
import static io.opentelemetry.api.common.AttributeKey.stringKey; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.api.trace.StatusCode; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
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 class ApolloConfigSingletons { | ||
|
||
private static final String NAME = "io.opentelemetry.javaagent.apolloconfig-2.0.0"; | ||
private static final Instrumenter<String, Void> INSTRUMENTER; | ||
|
||
private static final AttributeKey<String> CONFIG_NS_ATTRIBUTE_KEY = stringKey("config.namespace"); | ||
public static final ContextKey<String> REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY = | ||
ContextKey.named("apollo-config-repository-change-repeat"); | ||
|
||
static { | ||
AttributesExtractor<String, Void> attributesExtractor = | ||
new AttributesExtractor<String, Void>() { | ||
|
||
@Override | ||
public void onStart( | ||
AttributesBuilder attributes, Context parentContext, String namespace) { | ||
if (namespace == null) { | ||
return; | ||
} | ||
|
||
attributes.put(CONFIG_NS_ATTRIBUTE_KEY, namespace); | ||
} | ||
|
||
@Override | ||
public void onEnd( | ||
AttributesBuilder attributes, | ||
Context context, | ||
String namespace, | ||
@Nullable Void unused, | ||
@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) | ||
.addAttributesExtractor(attributesExtractor) | ||
.buildInstrumenter(SpanKindExtractor.alwaysClient()); | ||
} | ||
|
||
public static Instrumenter<String, Void> instrumenter() { | ||
return INSTRUMENTER; | ||
} | ||
|
||
private ApolloConfigSingletons() {} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../javaagent/instrumentation/apolloconfig/v2_0_0/ApolloRepositoryChangeInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0; | ||
|
||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; | ||
import static io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0.ApolloConfigSingletons.REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY; | ||
import static io.opentelemetry.javaagent.instrumentation.apolloconfig.v2_0_0.ApolloConfigSingletons.instrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class ApolloRepositoryChangeInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("com.ctrip.framework.apollo.internals.AbstractConfigRepository"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
String adviceName = this.getClass().getName() + "$ApolloRepositoryChangeAdvice"; | ||
transformer.applyAdviceToMethod(named("fireRepositoryChange"), adviceName); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ApolloRepositoryChangeAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0) String namespace, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
Context parentContext = currentContext(); | ||
String repeat = parentContext.get(REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY); | ||
if (repeat != null) { | ||
return; | ||
} | ||
if (!instrumenter().shouldStart(parentContext, namespace)) { | ||
return; | ||
} | ||
|
||
context = instrumenter().start(parentContext, namespace); | ||
context = context.with(REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY, "1"); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit( | ||
@Advice.Argument(value = 0) String namespace, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
if (scope == null) { | ||
return; | ||
} | ||
scope.close(); | ||
instrumenter().end(context, namespace, null, throwable); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
instrumentation/apolloconfig/apolloconfig-2.0.0/testing/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":testing-common")) | ||
|
||
compileOnly("com.ctrip.framework.apollo:apollo-client:2.0.0") | ||
} |
84 changes: 84 additions & 0 deletions
84
...java/io/opentelemetry/instrumentation/apolloconfig/v2_0_0/ApolloRepositoryChangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.apolloconfig.v2_0_0; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
|
||
import com.ctrip.framework.apollo.enums.ConfigSourceType; | ||
import com.ctrip.framework.apollo.internals.AbstractConfigRepository; | ||
import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
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; | ||
|
||
public abstract class ApolloRepositoryChangeTest { | ||
|
||
@RegisterExtension | ||
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Test | ||
void test() { | ||
String namespace = "application"; | ||
TestConfigRepository testConfigRepository = new TestConfigRepository(namespace, true); | ||
testConfigRepository.sync(); | ||
|
||
checkRepositoryChange(namespace); | ||
} | ||
|
||
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))); | ||
} | ||
|
||
static class TestConfigRepository extends AbstractConfigRepository { | ||
|
||
final String namespace; | ||
final Boolean hasNext; | ||
|
||
public TestConfigRepository(String namespace, Boolean hasNext) { | ||
this.namespace = namespace; | ||
this.hasNext = Boolean.TRUE; | ||
} | ||
|
||
@Override | ||
protected void sync() { | ||
this.fireRepositoryChange(this.namespace, new Properties()); | ||
if (hasNext) { | ||
TestConfigRepository sub = new TestConfigRepository(namespace, false); | ||
sub.sync(); | ||
} | ||
} | ||
|
||
@Override | ||
public Properties getConfig() { | ||
return new Properties(); | ||
} | ||
|
||
@Override | ||
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {} | ||
|
||
@Override | ||
public ConfigSourceType getSourceType() { | ||
return ConfigSourceType.NONE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters