-
Notifications
You must be signed in to change notification settings - Fork 873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Apolloconfig Inst #12794
base: main
Are you sure you want to change the base?
Add Apolloconfig Inst #12794
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
plugins { | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("com.ctrip.framework.apollo") | ||
module.set("apollo-client") | ||
versions.set("[1.0.0,)") | ||
assertInverse.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("com.google.auto.value:auto-value-annotations") | ||
annotationProcessor("com.google.auto.value:auto-value") | ||
|
||
library("com.ctrip.framework.apollo:apollo-client:1.0.0") | ||
|
||
testImplementation("com.ctrip.framework.apollo:apollo-client:1.1.0") | ||
testImplementation(project(":testing-common")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need to add explicit dependency to |
||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
jvmArgs("-Dotel.instrumentation.apolloconfig-apolloclient.enabled=true") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apolloconfig.apolloclient.v1_0; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class ApolloConfigInstrumentationModule extends InstrumentationModule { | ||
public ApolloConfigInstrumentationModule() { | ||
freshchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super("apolloconfig-apolloclient", "apolloconfig-apolloclient-1.0"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new ApolloRepositoryChangeInstrumentation()); | ||
} | ||
|
||
@Override | ||
public boolean defaultEnabled(ConfigProperties config) { | ||
return false; | ||
} | ||
} |
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.apolloclient.v1_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 javax.annotation.Nullable; | ||||
|
||||
public final class ApolloConfigSingletons { | ||||
|
||||
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"); | ||||
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) {} | ||||
}; | ||||
|
||||
INSTRUMENTER = | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was discussed at the sig meeting yesterday and the consensus was that events might be more appropriate for this use case. What do you think about creating an event instead of a span? If you plan to watch the recording of the sig meeting then this was discussed at the end of the meeting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't quite understand the definition of events before. Is it this https://opentelemetry.io/docs/specs/otel/logs/event-api/ Is there a link about the meeting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See Line 242 in 57c7cf2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your careful review. I took a quick look at the meeting and found some deviations. It is true that this framework is mainly popular in China. I will add some detailed explanations in the issue tomorrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I added some information, hope it helps. #12787 (comment) |
||||
Instrumenter.<String, Void>builder( | ||||
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.alwaysInternal()); | ||||
} | ||||
|
||||
public static Instrumenter<String, Void> instrumenter() { | ||||
return INSTRUMENTER; | ||||
} | ||||
|
||||
private ApolloConfigSingletons() {} | ||||
} |
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.apolloclient.v1_0; | ||||
|
||||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; | ||||
import static io.opentelemetry.javaagent.instrumentation.apolloconfig.apolloclient.v1_0.ApolloConfigSingletons.REPOSITORY_CHANGE_REPEAT_CONTEXT_KEY; | ||||
import static io.opentelemetry.javaagent.instrumentation.apolloconfig.apolloclient.v1_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) { | ||||
transformer.applyAdviceToMethod( | ||||
named("fireRepositoryChange"), this.getClass().getName() + "$ApolloRepositoryChangeAdvice"); | ||||
} | ||||
|
||||
@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); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually we use Line 78 in 57c7cf2
|
||||
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); | ||||
} | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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; | ||
import com.ctrip.framework.apollo.internals.ConfigRepository; | ||
import com.ctrip.framework.apollo.internals.RepositoryChangeListener; | ||
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 java.util.Properties; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class ApolloRepositoryChangeTest { | ||
|
||
@RegisterExtension | ||
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Test | ||
void fireRepositoryChangeTest() { | ||
String namespace = "application"; | ||
freshchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
TestConfigRepository testConfigRepository = new TestConfigRepository(namespace); | ||
testConfigRepository.addChangeListener(new TestRepositoryChangeListener()); | ||
testConfigRepository.sync(); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> | ||
span.hasKind(SpanKind.INTERNAL) | ||
.hasName("Apollo Config Repository Change") | ||
.hasAttributesSatisfyingExactly( | ||
singletonList( | ||
equalTo(AttributeKey.stringKey("config.namespace"), namespace))))); | ||
} | ||
|
||
static class TestConfigRepository extends AbstractConfigRepository { | ||
|
||
final String namespace; | ||
|
||
public TestConfigRepository(String namespace) { | ||
this.namespace = namespace; | ||
} | ||
|
||
@Override | ||
protected void sync() { | ||
this.fireRepositoryChange(this.namespace, new Properties()); | ||
} | ||
|
||
@Override | ||
public Properties getConfig() { | ||
return new Properties(); | ||
} | ||
|
||
@Override | ||
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {} | ||
|
||
@Override | ||
public ConfigSourceType getSourceType() { | ||
return ConfigSourceType.NONE; | ||
} | ||
} | ||
|
||
static class TestRepositoryChangeListener implements RepositoryChangeListener { | ||
|
||
@Override | ||
public void onRepositoryChange(String namespace, Properties newProperties) { | ||
new AbstractConfigRepository() { | ||
@Override | ||
public Properties getConfig() { | ||
return newProperties; | ||
} | ||
|
||
@Override | ||
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {} | ||
|
||
@Override | ||
public ConfigSourceType getSourceType() { | ||
return ConfigSourceType.NONE; | ||
} | ||
|
||
@Override | ||
protected void sync() { | ||
this.fireRepositoryChange(namespace, new Properties()); | ||
} | ||
}.sync(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use
testCompileOnly("com.ctrip.framework.apollo:apollo-client:1.1.0")
that way tests would run with1.0.0
. Then you will have to makeTestConfigRepository
andTestRepositoryChangeListener
private to avoid junit looking for test methods there.