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,18 +148,18 @@ 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
.build ())
150
- // Setting this to false so that Spanner Options does not register Metrics Tracer
151
- // factory again.
152
163
.setBuiltInMetricsEnabled (false )
153
164
.setApiTracerFactory (metricsTracerFactory )
154
165
.build ()
@@ -166,8 +177,9 @@ public void testMetricsSingleUseQuery() {
166
177
167
178
long elapsed = stopwatch .elapsed (TimeUnit .MILLISECONDS );
168
179
Attributes expectedAttributes =
169
- expectedBaseAttributes
180
+ expectedCommonBaseAttributes
170
181
.toBuilder ()
182
+ .putAll (expectedCommonRequestAttributes )
171
183
.put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
172
184
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.ExecuteStreamingSql" )
173
185
.build ();
@@ -189,6 +201,11 @@ public void testMetricsSingleUseQuery() {
189
201
MetricData attemptCountMetricData =
190
202
getMetricData (metricReader , BuiltInMetricsConstant .ATTEMPT_COUNT_NAME );
191
203
assertThat (getAggregatedValue (attemptCountMetricData , expectedAttributes )).isEqualTo (1 );
204
+
205
+ MetricData gfeLatencyMetricData =
206
+ getMetricData (metricReader , BuiltInMetricsConstant .GFE_LATENCIES_NAME );
207
+ long gfeLatencyValue = getAggregatedValue (attemptLatencyMetricData , expectedAttributes );
208
+ assertThat (gfeLatencyValue ).isEqualTo (gfeLatencyValue );
192
209
}
193
210
194
211
@ Test
@@ -205,14 +222,15 @@ public void testMetricsWithGaxRetryUnaryRpc() {
205
222
stopwatch .elapsed (TimeUnit .MILLISECONDS );
206
223
207
224
Attributes expectedAttributesBeginTransactionOK =
208
- expectedBaseAttributes
225
+ expectedCommonBaseAttributes
209
226
.toBuilder ()
227
+ .putAll (expectedCommonRequestAttributes )
210
228
.put (BuiltInMetricsConstant .STATUS_KEY , "OK" )
211
229
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
212
230
.build ();
213
231
214
232
Attributes expectedAttributesBeginTransactionFailed =
215
- expectedBaseAttributes
233
+ expectedCommonBaseAttributes
216
234
.toBuilder ()
217
235
.put (BuiltInMetricsConstant .STATUS_KEY , "UNAVAILABLE" )
218
236
.put (BuiltInMetricsConstant .METHOD_KEY , "Spanner.BeginTransaction" )
0 commit comments