Skip to content

Commit

Permalink
docs: add metrics exporter to sample app (#1809)
Browse files Browse the repository at this point in the history
Add a metrics exporter to the sample application to show how to
export both traces and metrics when using the JDBC driver.
  • Loading branch information
olavloite authored Oct 28, 2024
1 parent efdd859 commit 9b89f20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions samples/spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>exporter-trace</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud.opentelemetry</groupId>
<artifactId>exporter-metrics</artifactId>
<version>0.33.0</version>
</dependency>

<dependency>
<groupId>com.google.collections</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
package com.google.cloud.spanner.sample;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.opentelemetry.metric.GoogleCloudMetricExporter;
import com.google.cloud.opentelemetry.metric.MetricConfiguration;
import com.google.cloud.opentelemetry.trace.TraceConfiguration;
import com.google.cloud.opentelemetry.trace.TraceExporter;
import com.google.cloud.spanner.SpannerOptions;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
Expand Down Expand Up @@ -51,6 +56,7 @@ public OpenTelemetry openTelemetry() {

// Enable OpenTelemetry tracing in Spanner.
SpannerOptions.enableOpenTelemetryTraces();
SpannerOptions.enableOpenTelemetryMetrics();

if (!hasDefaultCredentials()) {
// Create a no-op OpenTelemetry object if this environment does not have any default
Expand All @@ -63,6 +69,17 @@ public OpenTelemetry openTelemetry() {
TraceConfiguration traceConfiguration = traceConfigurationBuilder.setProjectId(project).build();
SpanExporter traceExporter = TraceExporter.createWithConfiguration(traceConfiguration);

MetricConfiguration.Builder metricConfigurationBuilder = MetricConfiguration.builder();
MetricConfiguration metricConfiguration =
metricConfigurationBuilder.setProjectId(project).build();
MetricExporter metricExporter =
GoogleCloudMetricExporter.createWithConfiguration(metricConfiguration);

SdkMeterProvider sdkMeterProvider =
SdkMeterProvider.builder()
.registerMetricReader(PeriodicMetricReader.builder(metricExporter).build())
.build();

// Create an OpenTelemetry object and register it as the global OpenTelemetry object. This
// will automatically be picked up by the Spanner libraries and used for tracing.
return OpenTelemetrySdk.builder()
Expand All @@ -81,6 +98,7 @@ public OpenTelemetry openTelemetry() {
.build())
.addSpanProcessor(BatchSpanProcessor.builder(traceExporter).build())
.build())
.setMeterProvider(sdkMeterProvider)
.buildAndRegisterGlobal();
}

Expand Down

0 comments on commit 9b89f20

Please sign in to comment.