|
51 | 51 | import io.grpc.opentelemetry.OpenTelemetryMetricsModule.CallAttemptsTracerFactory;
|
52 | 52 | import io.grpc.opentelemetry.internal.OpenTelemetryConstants;
|
53 | 53 | import io.grpc.testing.GrpcServerRule;
|
| 54 | +import io.opentelemetry.api.common.AttributeKey; |
54 | 55 | import io.opentelemetry.api.metrics.Meter;
|
55 | 56 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
|
56 | 57 | import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
|
@@ -1070,6 +1071,140 @@ public void clientLocalityMetrics_missing() {
|
1070 | 1071 | point -> point.hasAttributes(clientAttributes))));
|
1071 | 1072 | }
|
1072 | 1073 |
|
| 1074 | + @Test |
| 1075 | + public void clientBackendServiceMetrics_present() { |
| 1076 | + String target = "target:///"; |
| 1077 | + OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter, |
| 1078 | + enabledMetricsMap, disableDefaultMetrics); |
| 1079 | + OpenTelemetryMetricsModule module = new OpenTelemetryMetricsModule( |
| 1080 | + fakeClock.getStopwatchSupplier(), resource, Arrays.asList("grpc.lb.backend_service"), |
| 1081 | + emptyList()); |
| 1082 | + OpenTelemetryMetricsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = |
| 1083 | + new CallAttemptsTracerFactory(module, target, method.getFullMethodName(), emptyList()); |
| 1084 | + |
| 1085 | + ClientStreamTracer tracer = |
| 1086 | + callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata()); |
| 1087 | + tracer.addOptionalLabel("grpc.lb.foo", "unimportant"); |
| 1088 | + tracer.addOptionalLabel("grpc.lb.backend_service", "should-be-overwritten"); |
| 1089 | + tracer.addOptionalLabel("grpc.lb.backend_service", "the-moon"); |
| 1090 | + tracer.addOptionalLabel("grpc.lb.foo", "thats-no-moon"); |
| 1091 | + tracer.streamClosed(Status.OK); |
| 1092 | + callAttemptsTracerFactory.callEnded(Status.OK); |
| 1093 | + |
| 1094 | + io.opentelemetry.api.common.Attributes attributes = io.opentelemetry.api.common.Attributes.of( |
| 1095 | + TARGET_KEY, target, |
| 1096 | + METHOD_KEY, method.getFullMethodName()); |
| 1097 | + |
| 1098 | + io.opentelemetry.api.common.Attributes clientAttributes |
| 1099 | + = io.opentelemetry.api.common.Attributes.of( |
| 1100 | + TARGET_KEY, target, |
| 1101 | + METHOD_KEY, method.getFullMethodName(), |
| 1102 | + STATUS_KEY, Status.Code.OK.toString()); |
| 1103 | + |
| 1104 | + io.opentelemetry.api.common.Attributes clientAttributesWithBackendService |
| 1105 | + = clientAttributes.toBuilder() |
| 1106 | + .put(AttributeKey.stringKey("grpc.lb.backend_service"), "the-moon") |
| 1107 | + .build(); |
| 1108 | + |
| 1109 | + assertThat(openTelemetryTesting.getMetrics()) |
| 1110 | + .satisfiesExactlyInAnyOrder( |
| 1111 | + metric -> |
| 1112 | + assertThat(metric) |
| 1113 | + .hasName(CLIENT_ATTEMPT_COUNT_INSTRUMENT_NAME) |
| 1114 | + .hasLongSumSatisfying( |
| 1115 | + longSum -> longSum.hasPointsSatisfying( |
| 1116 | + point -> point.hasAttributes(attributes))), |
| 1117 | + metric -> |
| 1118 | + assertThat(metric) |
| 1119 | + .hasName(CLIENT_ATTEMPT_DURATION_INSTRUMENT_NAME) |
| 1120 | + .hasHistogramSatisfying( |
| 1121 | + histogram -> histogram.hasPointsSatisfying( |
| 1122 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1123 | + metric -> |
| 1124 | + assertThat(metric) |
| 1125 | + .hasName(CLIENT_ATTEMPT_SENT_TOTAL_COMPRESSED_MESSAGE_SIZE) |
| 1126 | + .hasHistogramSatisfying( |
| 1127 | + histogram -> histogram.hasPointsSatisfying( |
| 1128 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1129 | + metric -> |
| 1130 | + assertThat(metric) |
| 1131 | + .hasName(CLIENT_ATTEMPT_RECV_TOTAL_COMPRESSED_MESSAGE_SIZE) |
| 1132 | + .hasHistogramSatisfying( |
| 1133 | + histogram -> histogram.hasPointsSatisfying( |
| 1134 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1135 | + metric -> |
| 1136 | + assertThat(metric) |
| 1137 | + .hasName(CLIENT_CALL_DURATION) |
| 1138 | + .hasHistogramSatisfying( |
| 1139 | + histogram -> histogram.hasPointsSatisfying( |
| 1140 | + point -> point.hasAttributes(clientAttributes)))); |
| 1141 | + } |
| 1142 | + |
| 1143 | + @Test |
| 1144 | + public void clientBackendServiceMetrics_missing() { |
| 1145 | + String target = "target:///"; |
| 1146 | + OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter, |
| 1147 | + enabledMetricsMap, disableDefaultMetrics); |
| 1148 | + OpenTelemetryMetricsModule module = new OpenTelemetryMetricsModule( |
| 1149 | + fakeClock.getStopwatchSupplier(), resource, Arrays.asList("grpc.lb.backend_service"), |
| 1150 | + emptyList()); |
| 1151 | + OpenTelemetryMetricsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = |
| 1152 | + new CallAttemptsTracerFactory(module, target, method.getFullMethodName(), emptyList()); |
| 1153 | + |
| 1154 | + ClientStreamTracer tracer = |
| 1155 | + callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata()); |
| 1156 | + tracer.streamClosed(Status.OK); |
| 1157 | + callAttemptsTracerFactory.callEnded(Status.OK); |
| 1158 | + |
| 1159 | + io.opentelemetry.api.common.Attributes attributes = io.opentelemetry.api.common.Attributes.of( |
| 1160 | + TARGET_KEY, target, |
| 1161 | + METHOD_KEY, method.getFullMethodName()); |
| 1162 | + |
| 1163 | + io.opentelemetry.api.common.Attributes clientAttributes |
| 1164 | + = io.opentelemetry.api.common.Attributes.of( |
| 1165 | + TARGET_KEY, target, |
| 1166 | + METHOD_KEY, method.getFullMethodName(), |
| 1167 | + STATUS_KEY, Status.Code.OK.toString()); |
| 1168 | + |
| 1169 | + io.opentelemetry.api.common.Attributes clientAttributesWithBackendService |
| 1170 | + = clientAttributes.toBuilder() |
| 1171 | + .put(AttributeKey.stringKey("grpc.lb.backend_service"), "") |
| 1172 | + .build(); |
| 1173 | + |
| 1174 | + assertThat(openTelemetryTesting.getMetrics()) |
| 1175 | + .satisfiesExactlyInAnyOrder( |
| 1176 | + metric -> |
| 1177 | + assertThat(metric) |
| 1178 | + .hasName(CLIENT_ATTEMPT_COUNT_INSTRUMENT_NAME) |
| 1179 | + .hasLongSumSatisfying( |
| 1180 | + longSum -> longSum.hasPointsSatisfying( |
| 1181 | + point -> point.hasAttributes(attributes))), |
| 1182 | + metric -> |
| 1183 | + assertThat(metric) |
| 1184 | + .hasName(CLIENT_ATTEMPT_DURATION_INSTRUMENT_NAME) |
| 1185 | + .hasHistogramSatisfying( |
| 1186 | + histogram -> histogram.hasPointsSatisfying( |
| 1187 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1188 | + metric -> |
| 1189 | + assertThat(metric) |
| 1190 | + .hasName(CLIENT_ATTEMPT_SENT_TOTAL_COMPRESSED_MESSAGE_SIZE) |
| 1191 | + .hasHistogramSatisfying( |
| 1192 | + histogram -> histogram.hasPointsSatisfying( |
| 1193 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1194 | + metric -> |
| 1195 | + assertThat(metric) |
| 1196 | + .hasName(CLIENT_ATTEMPT_RECV_TOTAL_COMPRESSED_MESSAGE_SIZE) |
| 1197 | + .hasHistogramSatisfying( |
| 1198 | + histogram -> histogram.hasPointsSatisfying( |
| 1199 | + point -> point.hasAttributes(clientAttributesWithBackendService))), |
| 1200 | + metric -> |
| 1201 | + assertThat(metric) |
| 1202 | + .hasName(CLIENT_CALL_DURATION) |
| 1203 | + .hasHistogramSatisfying( |
| 1204 | + histogram -> histogram.hasPointsSatisfying( |
| 1205 | + point -> point.hasAttributes(clientAttributes)))); |
| 1206 | + } |
| 1207 | + |
1073 | 1208 | @Test
|
1074 | 1209 | public void serverBasicMetrics() {
|
1075 | 1210 | OpenTelemetryMetricsResource resource = GrpcOpenTelemetry.createMetricInstruments(testMeter,
|
|
0 commit comments