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

docs: add metrics exporter to sample app #1809

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading