Skip to content
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

feat: Open telemetry implementation #2770

Merged

Conversation

surbhigarg92
Copy link
Contributor

This PR adds support for OpenTelemetry Instrumentation for Traces and Metrics.

Add dependency for OpenTelemetrySDK and required exporters.

Create OpenTelemetry object with required MeterProvider and TracerProvider exporter . Inject OpenTelemetry object via SpannerOptions or register as Global

`
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setTracerProvider(tracerProvider)
.setMeterProvider(sdkMeterProvider)
.build;

SpannerOptions options = SpannerOptions.newBuilder().setOpenTelemetry(openTelemetry).build();
`

By default, OpenTelemetry traces are not enabled. To enable OpenTelemetry traces , call SpannerOptions.enableOpenTelemetryTraces() in startup of your application. Enabling OpenTelemetry traces will disable OpenCensus traces. Both OpenCensus and OpenTelemetry traces can not be enabled at the same time.

@surbhigarg92 surbhigarg92 requested review from a team as code owners January 5, 2024 09:25
@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. api: spanner Issues related to the googleapis/java-spanner API. labels Jan 5, 2024
@surbhigarg92 surbhigarg92 changed the title Open telemetry implementation feat: Open telemetry implementation Jan 5, 2024
README.md Outdated
@@ -158,6 +158,9 @@ the Cloud Spanner Java client.

## OpenCensus Metrics

> Note: OpenCensus project is deprecated. See [Sunsetting OpenCensus](https://opentelemetry.io/blog/2023/sunsetting-opencensus/).
We recommend migrating to OpenTelemetry, the successor project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a link here directly to documentation that explains how they should set that up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the complete documentation . PTAL

google-cloud-spanner/pom.xml Outdated Show resolved Hide resolved
public static void registerGfeLatencyAndHeaderMissingCountViews() {
viewManager.registerView(SPANNER_GFE_LATENCY_VIEW);
viewManager.registerView(SPANNER_GFE_HEADER_MISSING_COUNT_VIEW);
if (SpannerOptions.isEnabledOpenCensusMetrics()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also: What happens if someone switches this bit back and forth for different Spanner instances?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics its little different. OpenCensus Metrics and OpenTelmetry metrics can be enabled or disable separately. So enabling one will not interfere with other. Also customers don't have the option to disable OpenTelemetry metrics .

If customer creates 1 client and enables metrics and creates another spanner client. Customer would only get metrics for the second client which is expected.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.... I think it will depend on what is called in which order. But as this is OpenCensus, which only supports global configuration, I guess this is the best we can do.

@@ -66,6 +67,7 @@ public final class BatchClientImplTest {
@Before
public void setUp() {
initMocks(this);
SpannerOptions.enableOpenTelemetryTraces();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will enable OpenTelemetry traces for all tests that come after this test. Java does not guarantee the order in which tests are executed. Are we sure that all tests will behave correctly, regardless of the order that they are executed, if this option is enabled at any random point in time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where ever we have added OpenTelemetry or OpenCensus related test, there I am resetting this with the new value.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is a setting that you want to be enabled globally for all the tests in this class, you should put it in a class setup method. So like this:

@BeforeClass
public static void setupOpenTelemetry() {
  SpannerOptions.enableOpenTelemetryTraces();
}

And put the reset in a cleanup method that is called after all the tests in this class have finished.

@AfterClass
public static void resetOpenTelemetry() {
  SpannerOptions.resetActiveTracingFramework();
}

@surbhigarg92 surbhigarg92 force-pushed the OpenTelemetry_Implementation branch 19 times, most recently from a2cdecd to f6486a8 Compare January 17, 2024 16:47
Copy link
Collaborator

@olavloite olavloite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some nits on the tests. Especially the configuration set/reset strategy should be changed for the tests, so that a test always starts with setting its own desired configuration, and then at the end resets to the default. That will ensure that the configuration for one specific test does not unintentionally leak to another test.

README.md Outdated Show resolved Hide resolved
Comment on lines +1397 to +1431
public static void disableOpenCensusMetrics() {
SpannerOptions.enableOpenCensusMetrics = false;
}

@VisibleForTesting
static void enableOpenCensusMetrics() {
SpannerOptions.enableOpenCensusMetrics = true;
}

public static boolean isEnabledOpenCensusMetrics() {
return SpannerOptions.enableOpenCensusMetrics;
}

/** Enables OpenTelemetry metrics. Enable OpenTelemetry metrics before creating Spanner client. */
public static void enableOpenTelemetryMetrics() {
SpannerOptions.enableOpenTelemetryMetrics = true;
}

public static boolean isEnabledOpenTelemetryMetrics() {
return SpannerOptions.enableOpenTelemetryMetrics;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it in a separate PR, but I think that these static options should also be guarded by the same lock as the one that we use for activeTracingFramework.

public static void registerGfeLatencyAndHeaderMissingCountViews() {
viewManager.registerView(SPANNER_GFE_LATENCY_VIEW);
viewManager.registerView(SPANNER_GFE_HEADER_MISSING_COUNT_VIEW);
if (SpannerOptions.isEnabledOpenCensusMetrics()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.... I think it will depend on what is called in which order. But as this is OpenCensus, which only supports global configuration, I guess this is the best we can do.

Comment on lines +174 to +175
SpannerOptions.resetActiveTracingFramework();
SpannerOptions.enableOpenTelemetryTraces();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need this in a test? If so, then you should do it like this:

try {
  SpannerOptions.enableOpenTelemetryTraces();
} finally {
  SpannerOptions.resetActiveTracingFramework()
}

Comment on lines +199 to +200
SpannerOptions.resetActiveTracingFramework();
SpannerOptions.enableOpenCensusTraces();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also: We should prefer not to do this kind of set/reset in a separate test, and if we do, then it should preferably be in the other order:

  1. Set the desired configuration
  2. Reset to the default in a finally block

The above will ensure that we don't unintentionally leak a custom configuration to other tests. Now, OpenCensus traces are enabled for any test that follows this test. That can give unintended side effects, as much of this configuration is global.


MetricsRecord record = metricRegistry.pollRecord();
assertThat(record.getMetrics().size()).isEqualTo(0);
SpannerOptions.enableOpenCensusMetrics();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in a finally block


@Test
public void testOpenTelemetrySessionMetrics() throws Exception {
SpannerOptions.enableOpenTelemetryMetrics();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset this in a finally block (and check other places as well)

Comment on lines +171 to +172
SpannerOptions.resetActiveTracingFramework();
SpannerOptions.enableOpenCensusTraces();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Put the enable... in a @BeforeClass method
  2. Put the reset... in a @afterclass method

This will ensure that the configuration of this test class does not leak into any other tests.

@surbhigarg92 surbhigarg92 force-pushed the OpenTelemetry_Implementation branch 2 times, most recently from 27626e7 to 5ae1152 Compare February 7, 2024 07:01
@surbhigarg92 surbhigarg92 added the owlbot:run Add this label to trigger the Owlbot post processor. label Feb 7, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Feb 7, 2024
@surbhigarg92 surbhigarg92 added automerge Merge the pull request once unit tests and other checks pass. owlbot:run Add this label to trigger the Owlbot post processor. labels Feb 8, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Feb 8, 2024
@gcf-merge-on-green gcf-merge-on-green bot merged commit 244d6a8 into googleapis:main Feb 8, 2024
24 checks passed
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner API. size: xl Pull request size is extra large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants