33
33
import com .google .common .base .Stopwatch ;
34
34
import com .google .common .collect .ImmutableList ;
35
35
import com .google .common .collect .Range ;
36
+ import io .grpc .ManagedChannelBuilder ;
36
37
import io .grpc .Status ;
37
38
import io .opentelemetry .api .OpenTelemetry ;
38
39
import io .opentelemetry .api .common .Attributes ;
58
59
import org .junit .runners .JUnit4 ;
59
60
60
61
@ RunWith (JUnit4 .class )
61
- public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTest {
62
+ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractNettyMockServerTest {
62
63
63
64
private static final Statement SELECT_RANDOM = Statement .of ("SELECT * FROM random" );
64
65
@@ -69,7 +70,8 @@ public class OpenTelemetryBuiltInMetricsTracerTest extends AbstractMockServerTes
69
70
70
71
private static Map <String , String > attributes ;
71
72
72
- private static Attributes expectedBaseAttributes ;
73
+ private static Attributes expectedCommonBaseAttributes ;
74
+ private static Attributes expectedCommonRequestAttributes ;
73
75
74
76
private static final long MIN_LATENCY = 0 ;
75
77
@@ -89,10 +91,11 @@ public static void setup() {
89
91
String client_name = "spanner-java/" ;
90
92
openTelemetry = OpenTelemetrySdk .builder ().setMeterProvider (meterProvider .build ()).build ();
91
93
provider .reset ();
92
- provider .initialize ("test-project" , client_name , null , null );
94
+ // provider.getOpenTelemetry().getMeterProvider().
95
+ provider .initialize (openTelemetry , "test-project" , client_name , null , null );
93
96
attributes = provider .getClientAttributes ();
94
97
95
- expectedBaseAttributes =
98
+ expectedCommonBaseAttributes =
96
99
Attributes .builder ()
97
100
.put (BuiltInMetricsConstant .PROJECT_ID_KEY , "test-project" )
98
101
.put (BuiltInMetricsConstant .INSTANCE_CONFIG_ID_KEY , "unknown" )
@@ -103,6 +106,14 @@ public static void setup() {
103
106
.put (BuiltInMetricsConstant .CLIENT_UID_KEY , attributes .get ("client_uid" ))
104
107
.put (BuiltInMetricsConstant .CLIENT_HASH_KEY , attributes .get ("client_hash" ))
105
108
.build ();
109
+
110
+ expectedCommonRequestAttributes =
111
+ Attributes .builder ()
112
+ .put (BuiltInMetricsConstant .INSTANCE_ID_KEY , "i" )
113
+ .put (BuiltInMetricsConstant .DATABASE_KEY , "d" )
114
+ .put (BuiltInMetricsConstant .DIRECT_PATH_ENABLED_KEY , "false" )
115
+ .put (BuiltInMetricsConstant .DIRECT_PATH_USED_KEY , "false" )
116
+ .build ();
106
117
}
107
118
108
119
@ BeforeClass
@@ -137,19 +148,19 @@ public void createSpannerInstance() {
137
148
.setRetryDelayMultiplier (1.0 )
138
149
.setTotalTimeoutDuration (Duration .ofMinutes (10L ))
139
150
.build ()));
151
+ String endpoint = address .getHostString () + ":" + server .getPort ();
140
152
spanner =
141
- builder
153
+ SpannerOptions . newBuilder ()
142
154
.setProjectId ("test-project" )
143
- .setChannelProvider (channelProvider )
155
+ .setChannelConfigurator (ManagedChannelBuilder ::usePlaintext )
156
+ .setHost ("http://" + endpoint )
144
157
.setCredentials (NoCredentials .getInstance ())
145
158
.setSessionPoolOption (
146
159
SessionPoolOptions .newBuilder ()
147
160
.setWaitForMinSessionsDuration (Duration .ofSeconds (5L ))
148
161
.setFailOnSessionLeak ()
149
162
.setSkipVerifyingBeginTransactionForMuxRW (true )
150
163
.build ())
151
- // Setting this to false so that Spanner Options does not register Metrics Tracer
152
- // factory again.
153
164
.setBuiltInMetricsEnabled (false )
154
165
.setApiTracerFactory (metricsTracerFactory )
155
166
.build ()
@@ -167,8 +178,9 @@ public void testMetricsSingleUseQuery() {
167
178
168
179
long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
169
180
Attributes expectedAttributes =
170
- expectedBaseAttributes
181
+ expectedCommonBaseAttributes
171
182
.toBuilder ()
183
+ .putAll (expectedCommonRequestAttributes )
172
184
.put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
173
185
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
174
186
.build ();
@@ -190,6 +202,11 @@ public void testMetricsSingleUseQuery() {
190
202
MetricData attemptCountMetricData =
191
203
getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
192
204
assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
205
+
206
+ MetricData gfeLatencyMetricData =
207
+ getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
208
+ long gfeLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
209
+ assertThat (gfeLatencyValue ).isEqualTo (gfeLatencyValue );
193
210
}
194
211
195
212
@ Test
@@ -206,14 +223,15 @@ public void testMetricsWithGaxRetryUnaryRpc() {
206
223
stopwatch .elapsed (TimeUnit .MILLISECONDS );
207
224
208
225
Attributes expectedAttributesBeginTransactionOK =
209
- expectedBaseAttributes
226
+ expectedCommonBaseAttributes
210
227
.toBuilder ()
228
+ .putAll (expectedCommonRequestAttributes )
211
229
.put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
212
230
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
213
231
.build ();
214
232
215
233
Attributes expectedAttributesBeginTransactionFailed =
216
- expectedBaseAttributes
234
+ expectedCommonBaseAttributes
217
235
.toBuilder ()
218
236
.put (BuiltInMetricsConstant .STATUS_KEY , "UNAVAILABLE" )
219
237
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
0 commit comments