|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.sofarpc.v5_4; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 9 | +import static java.util.Arrays.asList; |
| 10 | +import static java.util.Collections.singletonList; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | + |
| 13 | +import com.google.auto.service.AutoService; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.HelperResourceBuilder; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; |
| 19 | +import java.util.List; |
| 20 | +import net.bytebuddy.description.type.TypeDescription; |
| 21 | +import net.bytebuddy.matcher.ElementMatcher; |
| 22 | + |
| 23 | +@AutoService(InstrumentationModule.class) |
| 24 | +public class SofaRpcInstrumentationModule extends InstrumentationModule |
| 25 | + implements ExperimentalInstrumentationModule { |
| 26 | + public SofaRpcInstrumentationModule() { |
| 27 | + super("sofa-rpc", "sofa-rpc-5.4"); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void registerHelperResources(HelperResourceBuilder helperResourceBuilder) { |
| 32 | + helperResourceBuilder.register( |
| 33 | + "META-INF/services/com.alipay.sofa.rpc.filter.Filter", |
| 34 | + "sofa-rpc-5.4/META-INF/com.alipay.sofa.rpc.filter.Filter"); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { |
| 39 | + // added in 5.4.0 |
| 40 | + return hasClassesNamed("com.alipay.sofa.rpc.transport.ClientHandler"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public List<String> exposedClassNames() { |
| 45 | + return asList( |
| 46 | + "io.opentelemetry.javaagent.instrumentation.sofarpc.v5_4.OpenTelemetryClientFilter", |
| 47 | + "io.opentelemetry.javaagent.instrumentation.sofarpc.v5_4.OpenTelemetryServerFilter"); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<TypeInstrumentation> typeInstrumentations() { |
| 52 | + return singletonList(new ResourceInjectingTypeInstrumentation()); |
| 53 | + } |
| 54 | + |
| 55 | + // A type instrumentation is needed to trigger resource injection. |
| 56 | + private static class ResourceInjectingTypeInstrumentation implements TypeInstrumentation { |
| 57 | + @Override |
| 58 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 59 | + return named("com.alipay.sofa.rpc.ext.ExtensionLoader"); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void transform(TypeTransformer transformer) { |
| 64 | + // Nothing to transform, this type instrumentation is only used for injecting resources. |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments