Skip to content

Commit

Permalink
opentelemetry: Rename and stabilize API OpenTelemetryModule
Browse files Browse the repository at this point in the history
OpenTelemetryModule is renamed to GrpcOpenTelemetry. The Builder is now
`final`, although that should only impact mocks as it had a private
constructor.

Fixes #10591
  • Loading branch information
ejona86 committed May 9, 2024
1 parent 8808d63 commit 1e731be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.grpc.ExperimentalApi;
import io.grpc.InternalConfigurator;
import io.grpc.InternalConfiguratorRegistry;
import io.grpc.InternalManagedChannelBuilder;
Expand All @@ -45,13 +44,12 @@
/**
* The entrypoint for OpenTelemetry metrics functionality in gRPC.
*
* <p>OpenTelemetryModule uses {@link io.opentelemetry.api.OpenTelemetry} APIs for instrumentation.
* <p>GrpcOpenTelemetry uses {@link io.opentelemetry.api.OpenTelemetry} APIs for instrumentation.
* When no SDK is explicitly added no telemetry data will be collected. See
* {@link io.opentelemetry.sdk.OpenTelemetrySdk} for information on how to construct the SDK.
*
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/10591")
public final class OpenTelemetryModule {
public final class GrpcOpenTelemetry {

private static final Supplier<Stopwatch> STOPWATCH_SUPPLIER = new Supplier<Stopwatch>() {
@Override
Expand All @@ -74,7 +72,7 @@ public static Builder newBuilder() {
return new Builder();
}

private OpenTelemetryModule(Builder builder) {
private GrpcOpenTelemetry(Builder builder) {
this.openTelemetrySdk = checkNotNull(builder.openTelemetrySdk, "openTelemetrySdk");
this.meterProvider = checkNotNull(openTelemetrySdk.getMeterProvider(), "meterProvider");
this.meter = this.meterProvider
Expand Down Expand Up @@ -125,20 +123,20 @@ MetricSink getSink() {
}

/**
* Registers OpenTelemetryModule globally, applying its configuration to all subsequently created
* Registers GrpcOpenTelemetry globally, applying its configuration to all subsequently created
* gRPC channels and servers.
*/
public void registerGlobal() {
InternalConfiguratorRegistry.setConfigurators(Collections.singletonList(
new InternalConfigurator() {
@Override
public void configureChannelBuilder(ManagedChannelBuilder<?> channelBuilder) {
OpenTelemetryModule.this.configureChannelBuilder(channelBuilder);
GrpcOpenTelemetry.this.configureChannelBuilder(channelBuilder);
}

@Override
public void configureServerBuilder(ServerBuilder<?> serverBuilder) {
OpenTelemetryModule.this.configureServerBuilder(serverBuilder);
GrpcOpenTelemetry.this.configureServerBuilder(serverBuilder);
}
}));
}
Expand Down Expand Up @@ -268,7 +266,7 @@ static boolean isMetricEnabled(String metricName, Map<String, Boolean> enableMet


/**
* Builder for configuring {@link OpenTelemetryModule}.
* Builder for configuring {@link GrpcOpenTelemetry}.
*/
public static class Builder {
private OpenTelemetry openTelemetrySdk = OpenTelemetry.noop();
Expand Down Expand Up @@ -328,11 +326,11 @@ public Builder disableAllMetrics() {
}

/**
* Returns a new {@link OpenTelemetryModule} built with the configuration of this {@link
* Returns a new {@link GrpcOpenTelemetry} built with the configuration of this {@link
* Builder}.
*/
public OpenTelemetryModule build() {
return new OpenTelemetryModule(this);
public GrpcOpenTelemetry build() {
return new GrpcOpenTelemetry(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class OpenTelemetryModuleTest {
public class GrpcOpenTelemetryTest {
private final InMemoryMetricReader inMemoryMetricReader = InMemoryMetricReader.create();
private final SdkMeterProvider meterProvider =
SdkMeterProvider.builder().registerMetricReader(inMemoryMetricReader).build();
Expand All @@ -42,7 +42,7 @@ public void build() {
OpenTelemetrySdk sdk =
OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();

OpenTelemetryModule openTelemetryModule = OpenTelemetryModule.newBuilder()
GrpcOpenTelemetry openTelemetryModule = GrpcOpenTelemetry.newBuilder()
.sdk(sdk)
.addOptionalLabel("version")
.build();
Expand All @@ -58,7 +58,7 @@ public void build() {

@Test
public void builderDefaults() {
OpenTelemetryModule module = OpenTelemetryModule.newBuilder().build();
GrpcOpenTelemetry module = GrpcOpenTelemetry.newBuilder().build();

assertThat(module.getOpenTelemetryInstance()).isNotNull();
assertThat(module.getOpenTelemetryInstance()).isSameInstanceAs(noopOpenTelemetry);
Expand All @@ -77,11 +77,11 @@ public void builderDefaults() {

@Test
public void enableDisableMetrics() {
OpenTelemetryModule.Builder builder = OpenTelemetryModule.newBuilder();
GrpcOpenTelemetry.Builder builder = GrpcOpenTelemetry.newBuilder();
builder.enableMetrics(Arrays.asList("metric1", "metric4"));
builder.disableMetrics(Arrays.asList("metric2", "metric3"));

OpenTelemetryModule module = builder.build();
GrpcOpenTelemetry module = builder.build();

assertThat(module.getEnableMetrics().get("metric1")).isTrue();
assertThat(module.getEnableMetrics().get("metric4")).isTrue();
Expand All @@ -91,12 +91,12 @@ public void enableDisableMetrics() {

@Test
public void disableAllMetrics() {
OpenTelemetryModule.Builder builder = OpenTelemetryModule.newBuilder();
GrpcOpenTelemetry.Builder builder = GrpcOpenTelemetry.newBuilder();
builder.enableMetrics(Arrays.asList("metric1", "metric4"));
builder.disableMetrics(Arrays.asList("metric2", "metric3"));
builder.disableAllMetrics();

OpenTelemetryModule module = builder.build();
GrpcOpenTelemetry module = builder.build();

assertThat(module.getEnableMetrics()).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void setUp() throws Exception {

@Test
public void testClientInterceptors() {
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = newOpenTelemetryMetricsModule(resource);
grpcServerRule.getServiceRegistry().addService(
Expand Down Expand Up @@ -214,7 +214,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
@Test
public void clientBasicMetrics() {
String target = "target:///";
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = newOpenTelemetryMetricsModule(resource);
OpenTelemetryMetricsModule.CallAttemptsTracerFactory callAttemptsTracerFactory =
Expand Down Expand Up @@ -353,7 +353,7 @@ public void clientBasicMetrics() {
@Test
public void recordAttemptMetrics() {
String target = "dns:///example.com";
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = newOpenTelemetryMetricsModule(resource);
OpenTelemetryMetricsModule.CallAttemptsTracerFactory callAttemptsTracerFactory =
Expand Down Expand Up @@ -778,7 +778,7 @@ public void recordAttemptMetrics() {
@Test
public void clientStreamNeverCreatedStillRecordMetrics() {
String target = "dns:///foo.example.com";
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = newOpenTelemetryMetricsModule(resource);
OpenTelemetryMetricsModule.CallAttemptsTracerFactory callAttemptsTracerFactory =
Expand Down Expand Up @@ -882,7 +882,7 @@ public void clientStreamNeverCreatedStillRecordMetrics() {
@Test
public void clientLocalityMetrics_present() {
String target = "target:///";
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = new OpenTelemetryMetricsModule(
fakeClock.getStopwatchSupplier(), resource, Arrays.asList("grpc.lb.locality"));
Expand Down Expand Up @@ -950,7 +950,7 @@ public void clientLocalityMetrics_present() {
@Test
public void clientLocalityMetrics_missing() {
String target = "target:///";
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = new OpenTelemetryMetricsModule(
fakeClock.getStopwatchSupplier(), resource, Arrays.asList("grpc.lb.locality"));
Expand Down Expand Up @@ -1013,7 +1013,7 @@ public void clientLocalityMetrics_missing() {

@Test
public void serverBasicMetrics() {
OpenTelemetryMetricsResource resource = OpenTelemetryModule.createMetricInstruments(testMeter,
OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
enabledMetricsMap, disableDefaultMetrics);
OpenTelemetryMetricsModule module = newOpenTelemetryMetricsModule(resource);
ServerStreamTracer.Factory tracerFactory = module.getServerTracerFactory();
Expand Down

0 comments on commit 1e731be

Please sign in to comment.