Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@

package com.example.metricssample.spanner;

import com.google.api.gax.tracing.OpenTelemetryMetricsFactory;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.api.gax.tracing.MetricsTracerFactory;
import com.google.api.gax.tracing.OpentelemetryMetricsRecorder;
import com.google.cloud.opentelemetry.metric.GoogleCloudMetricExporter;
import com.google.cloud.opentelemetry.metric.MetricConfiguration;
import com.google.cloud.opentelemetry.metric.MetricDescriptorStrategy;
import com.google.cloud.spanner.*;
import com.google.cloud.spanner.spi.v1.SpannerRpcViews;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
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 org.springframework.web.bind.annotation.*;

import java.util.*;
Expand All @@ -14,8 +27,8 @@
public class SpannerController {
private Spanner spanner;
private DatabaseClient dbClient;
private String instanceId = "testspanner";
private String databaseId = "test-metrics";
private String instanceId = "myinstance";
private String databaseId = "mydatabase";
private String table = "Players";

private final OpenTelemetry openTelemetry;
Expand All @@ -27,15 +40,50 @@ public class SpannerController {

spanner = options.getService();
// And then create the Spanner database client.
String projectId = options.getProjectId();
String projectId = "spanner-demo-326919";
dbClient = spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));

// Register GFELatency and GFE Header Missing Count Views
SpannerRpcViews.registerGfeLatencyAndHeaderMissingCountViews();
}

private OpenTelemetryMetricsFactory createOpenTelemetryTracerFactory() {
return new OpenTelemetryMetricsFactory(openTelemetry, "java-spanner", "6.13.0");
private static ApiTracerFactory createOpenTelemetryTracerFactory() {

MetricExporter metricExporter = GoogleCloudMetricExporter.createWithConfiguration(
MetricConfiguration.builder()
// Configure the cloud project id. Note: this is autodiscovered by default.
.setProjectId("spanner-demo-326919")
.setPrefix("custom.googleapis.com")
// Configure a strategy for how/when to configure metric descriptors.
.setDescriptorStrategy(MetricDescriptorStrategy.SEND_ONCE)
.build());

// Periodic Metric Reader configuration
PeriodicMetricReader metricReader =
PeriodicMetricReader.builder(metricExporter)
.setInterval(java.time.Duration.ofSeconds(5))
.build();

// OpenTelemetry SDK Configuration
Resource resource = Resource.builder().build();
SdkMeterProvider sdkMeterProvider =
SdkMeterProvider.builder().registerMetricReader(metricReader).setResource(resource).build();

OpenTelemetry openTelemetry =
OpenTelemetrySdk.builder().setMeterProvider(sdkMeterProvider).build();

// Meter Creation
Meter meter =
openTelemetry
.meterBuilder("gax")
.setInstrumentationVersion(GaxProperties.getGaxVersion())
.build();

// OpenTelemetry Metrics Recorder
OpentelemetryMetricsRecorder otelMetricsRecorder = new OpentelemetryMetricsRecorder(meter);

// Finally, create the Tracer Factory
return new MetricsTracerFactory(otelMetricsRecorder);
}

@GetMapping(path = "/", produces = "application/json")
Expand Down