- Notifications
You must be signed in to change notification settings - Fork 135
feat: Open telemetry implementation #2770
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
Conversation
c850e91 to 863dc63 Compare README.md Outdated
| ## 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. |
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.
Can we add a link here directly to documentation that explains how they should set that up?
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.
I added the complete documentation . PTAL
google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java Show resolved Hide resolved
google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseClientImpl.java Outdated Show resolved Hide resolved
google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java Outdated Show resolved Hide resolved
...-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/SpannerInterceptorProvider.java 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()) { |
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.
Here also: What happens if someone switches this bit back and forth for different Spanner instances?
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.
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.
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.
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.
| @Before | ||
| public void setUp() { | ||
| initMocks(this); | ||
| SpannerOptions.enableOpenTelemetryTraces(); |
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.
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?
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.
Where ever we have added OpenTelemetry or OpenCensus related test, there I am resetting this with the new value.
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.
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(); } google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/SpannerRpcMetricsTest.java Outdated Show resolved Hide resolved
a2cdecd to f6486a8 Compare
olavloite left a comment
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.
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.
google-cloud-spanner/src/main/java/com/google/cloud/spanner/OpenCensusSpan.java Show resolved Hide resolved
google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java Show resolved Hide resolved
| 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; | ||
| } |
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.
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()) { |
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.
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.
| SpannerOptions.resetActiveTracingFramework(); | ||
| SpannerOptions.enableOpenTelemetryTraces(); |
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.
Are you sure you need this in a test? If so, then you should do it like this:
try { SpannerOptions.enableOpenTelemetryTraces(); } finally { SpannerOptions.resetActiveTracingFramework() } | SpannerOptions.resetActiveTracingFramework(); | ||
| SpannerOptions.enableOpenCensusTraces(); |
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.
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:
- Set the desired configuration
- 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(); |
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.
Put in a finally block
| | ||
| @Test | ||
| public void testOpenTelemetrySessionMetrics() throws Exception { | ||
| SpannerOptions.enableOpenTelemetryMetrics(); |
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.
Reset this in a finally block (and check other places as well)
| SpannerOptions.resetActiveTracingFramework(); | ||
| SpannerOptions.enableOpenCensusTraces(); |
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.
- Put the enable... in a @BeforeClass method
- Put the reset... in a @afterclass method
This will ensure that the configuration of this test class does not leak into any other tests.
27626e7 to 5ae1152 Compare Co-authored-by: Knut Olav Løite <koloite@gmail.com>
e290810 to a612357 Compare
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.