22
22
import com .google .cloud .spanner .Spanner ;
23
23
import com .google .cloud .spanner .SpannerOptions ;
24
24
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 ;
34
26
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 ;
49
27
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 ;
54
28
55
29
/**
56
30
* This sample demonstrates how to capture GFE latency using OpenCensus.
@@ -64,7 +38,6 @@ public static void main(String[] args) {
64
38
String databaseId = "my-database" ;
65
39
66
40
SpannerOptions options = SpannerOptions .newBuilder ()
67
- .setInterceptorProvider (() -> Collections .singletonList (interceptor ))
68
41
.build ();
69
42
Spanner spanner = options .getService ();
70
43
DatabaseClient dbClient = spanner
@@ -73,39 +46,15 @@ public static void main(String[] args) {
73
46
}
74
47
75
48
// [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 ();
100
52
101
- static ViewManager manager = Stats .getViewManager ();
53
+ // Capture GFE Latency and GFE Header missing count.
54
+ // SpannerRpcViews.registerGfeLatencyAndHeaderMissingCountViews();
102
55
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();
109
58
110
59
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
111
60
// Exporters use Application Default Credentials to authenticate.
@@ -128,51 +77,6 @@ static void captureGfeMetric(DatabaseClient dbClient) {
128
77
}
129
78
}
130
79
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]
145
81
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
- }
178
82
}
0 commit comments