Skip to content

Commit 5b064ec

Browse files
authored
samples: update gfe_latency sample (GoogleCloudPlatform#6177)
* Update GFELatency sample * Adding opening tag back * Moving GFELatency only method to the top
1 parent 9ef23bb commit 5b064ec

File tree

2 files changed

+10
-106
lines changed

2 files changed

+10
-106
lines changed

spanner/opencensus/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<maven.compiler.target>1.8</maven.compiler.target>
1010
<maven.compiler.source>1.8</maven.compiler.source>
1111
<opencensus.version>0.30.0</opencensus.version>
12-
<spanner.version>2.0.2</spanner.version>
12+
<spanner.version>6.13.0</spanner.version>
1313
</properties>
1414

1515
<!--

spanner/opencensus/src/main/java/com/example/spanner/opencensus/CaptureGfeMetric.java

Lines changed: 9 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,9 @@
2222
import com.google.cloud.spanner.Spanner;
2323
import com.google.cloud.spanner.SpannerOptions;
2424
import com.google.cloud.spanner.Statement;
25-
import io.grpc.CallOptions;
26-
import io.grpc.Channel;
27-
import io.grpc.ClientCall;
28-
import io.grpc.ClientInterceptor;
29-
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
30-
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
31-
import io.grpc.Metadata;
32-
import io.grpc.MethodDescriptor;
33-
import io.opencensus.common.Scope;
25+
import com.google.cloud.spanner.spi.v1.SpannerRpcViews;
3426
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
35-
import io.opencensus.stats.Aggregation;
36-
import io.opencensus.stats.Aggregation.Distribution;
37-
import io.opencensus.stats.BucketBoundaries;
38-
import io.opencensus.stats.Measure.MeasureLong;
39-
import io.opencensus.stats.Stats;
40-
import io.opencensus.stats.StatsRecorder;
41-
import io.opencensus.stats.View;
42-
import io.opencensus.stats.View.Name;
43-
import io.opencensus.stats.ViewManager;
44-
import io.opencensus.tags.TagContext;
45-
import io.opencensus.tags.TagKey;
46-
import io.opencensus.tags.TagValue;
47-
import io.opencensus.tags.Tagger;
48-
import io.opencensus.tags.Tags;
4927
import java.io.IOException;
50-
import java.util.Arrays;
51-
import java.util.Collections;
52-
import java.util.regex.Matcher;
53-
import java.util.regex.Pattern;
5428

5529
/**
5630
* This sample demonstrates how to capture GFE latency using OpenCensus.
@@ -64,7 +38,6 @@ public static void main(String[] args) {
6438
String databaseId = "my-database";
6539

6640
SpannerOptions options = SpannerOptions.newBuilder()
67-
.setInterceptorProvider(() -> Collections.singletonList(interceptor))
6841
.build();
6942
Spanner spanner = options.getService();
7043
DatabaseClient dbClient = spanner
@@ -73,39 +46,15 @@ public static void main(String[] args) {
7346
}
7447

7548
// [START spanner_opencensus_capture_gfe_metric]
76-
private static final String MILLISECOND = "ms";
77-
private static final TagKey key = TagKey.create("grpc_client_method");
78-
79-
// GFE t4t7 latency extracted from server-timing header.
80-
public static final MeasureLong SPANNER_GFE_LATENCY =
81-
MeasureLong.create(
82-
"cloud.google.com/java/spanner/gfe_latency",
83-
"Latency between Google's network receives an RPC and reads back the first byte of the"
84-
+ " response",
85-
MILLISECOND);
86-
87-
static final Aggregation AGGREGATION_WITH_MILLIS_HISTOGRAM =
88-
Distribution.create(BucketBoundaries.create(Arrays.asList(
89-
0.0, 0.01, 0.05, 0.1, 0.3, 0.6, 0.8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 13.0,
90-
16.0, 20.0, 25.0, 30.0, 40.0, 50.0, 65.0, 80.0, 100.0, 130.0, 160.0, 200.0, 250.0,
91-
300.0, 400.0, 500.0, 650.0, 800.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0, 50000.0,
92-
100000.0)));
93-
static final View GFE_LATENCY_VIEW = View
94-
.create(Name.create("cloud.google.com/java/spanner/gfe_latency"),
95-
"Latency between Google's network receives an RPC and reads back the first byte of the"
96-
+ " response",
97-
SPANNER_GFE_LATENCY,
98-
AGGREGATION_WITH_MILLIS_HISTOGRAM,
99-
Collections.singletonList(key));
49+
static void captureGfeMetric(DatabaseClient dbClient) {
50+
// Capture GFE Latency.
51+
SpannerRpcViews.registerGfeLatencyView();
10052

101-
static ViewManager manager = Stats.getViewManager();
53+
// Capture GFE Latency and GFE Header missing count.
54+
// SpannerRpcViews.registerGfeLatencyAndHeaderMissingCountViews();
10255

103-
private static final Tagger tagger = Tags.getTagger();
104-
private static final StatsRecorder STATS_RECORDER = Stats.getStatsRecorder();
105-
106-
static void captureGfeMetric(DatabaseClient dbClient) {
107-
// Register GFE view.
108-
manager.registerView(GFE_LATENCY_VIEW);
56+
// Capture only GFE Header missing count.
57+
// SpannerRpcViews.registerGfeHeaderMissingCountView();
10958

11059
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
11160
// Exporters use Application Default Credentials to authenticate.
@@ -128,51 +77,6 @@ static void captureGfeMetric(DatabaseClient dbClient) {
12877
}
12978
}
13079

131-
private static final HeaderClientInterceptor interceptor = new HeaderClientInterceptor();
132-
private static final Metadata.Key<String> SERVER_TIMING_HEADER_KEY =
133-
Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER);
134-
// Every response from Cloud Spanner, there will be an additional header that contains the total
135-
// elapsed time on GFE. The format is "server-timing: gfet4t7; dur=[GFE latency in ms]".
136-
private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?<dur>\\d+)");
137-
138-
// ClientInterceptor to intercept the outgoing RPCs in order to retrieve the GFE header.
139-
private static class HeaderClientInterceptor implements ClientInterceptor {
140-
141-
@Override
142-
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
143-
CallOptions callOptions, Channel next) {
144-
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
80+
// [END spanner_opencensus_capture_gfe_metric]
14581

146-
@Override
147-
public void start(Listener<RespT> responseListener, Metadata headers) {
148-
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
149-
@Override
150-
public void onHeaders(Metadata metadata) {
151-
processHeader(metadata, method.getFullMethodName());
152-
super.onHeaders(metadata);
153-
}
154-
}, headers);
155-
}
156-
};
157-
}
158-
159-
// Process header, extract duration value and record it using OpenCensus.
160-
private static void processHeader(Metadata metadata, String method) {
161-
if (metadata.get(SERVER_TIMING_HEADER_KEY) != null) {
162-
String serverTiming = metadata.get(SERVER_TIMING_HEADER_KEY);
163-
Matcher matcher = SERVER_TIMING_HEADER_PATTERN.matcher(serverTiming);
164-
if (matcher.find()) {
165-
long latency = Long.parseLong(matcher.group("dur"));
166-
167-
TagContext tctx = tagger.emptyBuilder().put(key, TagValue.create(method)).build();
168-
try (Scope ss = tagger.withTagContext(tctx)) {
169-
STATS_RECORDER.newMeasureMap()
170-
.put(SPANNER_GFE_LATENCY, latency)
171-
.record();
172-
}
173-
}
174-
}
175-
}
176-
// [END spanner_opencensus_capture_gfe_metric]
177-
}
17882
}

0 commit comments

Comments
 (0)