25
25
26
26
import com .google .api .gax .tracing .ApiTracer ;
27
27
import com .google .cloud .spanner .BuiltInMetricsConstant ;
28
+ import com .google .cloud .spanner .BuiltInOpenTelemetryMetricsRecorder ;
28
29
import com .google .cloud .spanner .CompositeTracer ;
29
30
import com .google .cloud .spanner .SpannerExceptionFactory ;
30
31
import com .google .cloud .spanner .SpannerRpcMetrics ;
@@ -94,12 +95,17 @@ class HeaderInterceptor implements ClientInterceptor {
94
95
private static final Level LEVEL = Level .INFO ;
95
96
private final SpannerRpcMetrics spannerRpcMetrics ;
96
97
98
+ private final BuiltInOpenTelemetryMetricsRecorder builtInOpenTelemetryMetricsRecorder ;
99
+
97
100
private final Supplier <Boolean > directPathEnabledSupplier ;
98
101
99
102
HeaderInterceptor (
100
- SpannerRpcMetrics spannerRpcMetrics , Supplier <Boolean > directPathEnabledSupplier ) {
103
+ SpannerRpcMetrics spannerRpcMetrics ,
104
+ BuiltInOpenTelemetryMetricsRecorder builtInOpenTelemetryMetricsRecorder ,
105
+ Supplier <Boolean > directPathEnabledSupplier ) {
101
106
this .spannerRpcMetrics = spannerRpcMetrics ;
102
107
this .directPathEnabledSupplier = directPathEnabledSupplier ;
108
+ this .builtInOpenTelemetryMetricsRecorder = builtInOpenTelemetryMetricsRecorder ;
103
109
}
104
110
105
111
@ Override
@@ -118,17 +124,22 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
118
124
TagContext tagContext = getTagContext (key , method .getFullMethodName (), databaseName );
119
125
Attributes attributes =
120
126
getMetricAttributes (key , method .getFullMethodName (), databaseName );
121
- Map <String , String > builtInMetricsAttributes =
122
- getBuiltInMetricAttributes (key , databaseName );
127
+ Map <String , String > commonBuiltInMetricAttributes =
128
+ getCommonBuiltInMetricAttributes (key , databaseName );
123
129
super .start (
124
130
new SimpleForwardingClientCallListener <RespT >(responseListener ) {
125
131
@ Override
126
132
public void onHeaders (Metadata metadata ) {
127
133
Boolean isDirectPathUsed =
128
134
isDirectPathUsed (getAttributes ().get (Grpc .TRANSPORT_ATTR_REMOTE_ADDR ));
129
135
addBuiltInMetricAttributes (
130
- compositeTracer , builtInMetricsAttributes , isDirectPathUsed );
131
- processHeader (metadata , tagContext , attributes , span );
136
+ compositeTracer , commonBuiltInMetricAttributes , isDirectPathUsed );
137
+ processHeader (
138
+ metadata ,
139
+ tagContext ,
140
+ attributes ,
141
+ span ,
142
+ getBuiltInMetricAttributes (commonBuiltInMetricAttributes , isDirectPathUsed ));
132
143
super .onHeaders (metadata );
133
144
}
134
145
},
@@ -142,7 +153,11 @@ public void onHeaders(Metadata metadata) {
142
153
}
143
154
144
155
private void processHeader (
145
- Metadata metadata , TagContext tagContext , Attributes attributes , Span span ) {
156
+ Metadata metadata ,
157
+ TagContext tagContext ,
158
+ Attributes attributes ,
159
+ Span span ,
160
+ Map <String , String > builtInMetricsAttributes ) {
146
161
MeasureMap measureMap = STATS_RECORDER .newMeasureMap ();
147
162
String serverTiming = metadata .get (SERVER_TIMING_HEADER_KEY );
148
163
if (serverTiming != null && serverTiming .startsWith (SERVER_TIMING_HEADER_PREFIX )) {
@@ -154,6 +169,7 @@ private void processHeader(
154
169
155
170
spannerRpcMetrics .recordGfeLatency (latency , attributes );
156
171
spannerRpcMetrics .recordGfeHeaderMissingCount (0L , attributes );
172
+ builtInOpenTelemetryMetricsRecorder .recordGFELatency (latency , builtInMetricsAttributes );
157
173
158
174
if (span != null ) {
159
175
span .setAttribute ("gfe_latency" , String .valueOf (latency ));
@@ -224,8 +240,8 @@ private Attributes getMetricAttributes(String key, String method, DatabaseName d
224
240
});
225
241
}
226
242
227
- private Map <String , String > getBuiltInMetricAttributes ( String key , DatabaseName databaseName )
228
- throws ExecutionException {
243
+ private Map <String , String > getCommonBuiltInMetricAttributes (
244
+ String key , DatabaseName databaseName ) throws ExecutionException {
229
245
return builtInAttributesCache .get (
230
246
key ,
231
247
() -> {
@@ -240,17 +256,21 @@ private Map<String, String> getBuiltInMetricAttributes(String key, DatabaseName
240
256
});
241
257
}
242
258
259
+ private Map <String , String > getBuiltInMetricAttributes (
260
+ Map <String , String > commonBuiltInMetricsAttributes , Boolean isDirectPathUsed ) {
261
+ Map <String , String > builtInMetricAttributes = new HashMap <>(commonBuiltInMetricsAttributes );
262
+ builtInMetricAttributes .put (
263
+ BuiltInMetricsConstant .DIRECT_PATH_USED_KEY .getKey (), Boolean .toString (isDirectPathUsed ));
264
+ return builtInMetricAttributes ;
265
+ }
266
+
243
267
private void addBuiltInMetricAttributes (
244
268
CompositeTracer compositeTracer ,
245
- Map <String , String > builtInMetricsAttributes ,
269
+ Map <String , String > commonBuiltInMetricsAttributes ,
246
270
Boolean isDirectPathUsed ) {
247
271
if (compositeTracer != null ) {
248
- // Direct Path used attribute
249
- Map <String , String > attributes = new HashMap <>(builtInMetricsAttributes );
250
- attributes .put (
251
- BuiltInMetricsConstant .DIRECT_PATH_USED_KEY .getKey (), Boolean .toString (isDirectPathUsed ));
252
-
253
- compositeTracer .addAttributes (attributes );
272
+ compositeTracer .addAttributes (
273
+ getBuiltInMetricAttributes (commonBuiltInMetricsAttributes , isDirectPathUsed ));
254
274
}
255
275
}
256
276
0 commit comments