Skip to content

Commit

Permalink
Add metric support for grpc (open-telemetry#5923)
Browse files Browse the repository at this point in the history
* Add metric support for grpc

* Spotless
  • Loading branch information
jack-berg authored and RashmiRam committed May 23, 2022
1 parent 7cbab4b commit 0f222db
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcClientMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.rpc.RpcServerMetrics;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetClientAttributesGetter;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.GrpcNetServerAttributesGetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
Expand Down Expand Up @@ -126,11 +128,13 @@ public GrpcTelemetry build() {

clientInstrumenterBuilder
.addAttributesExtractor(RpcClientAttributesExtractor.create(rpcAttributesGetter))
.addAttributesExtractor(NetClientAttributesExtractor.create(netClientAttributesGetter));
.addAttributesExtractor(NetClientAttributesExtractor.create(netClientAttributesGetter))
.addRequestMetrics(RpcClientMetrics.get());
serverInstrumenterBuilder
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
.addAttributesExtractor(
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()));
NetServerAttributesExtractor.create(new GrpcNetServerAttributesGetter()))
.addRequestMetrics(RpcServerMetrics.get());

if (peerService != null) {
clientInstrumenterBuilder.addAttributesExtractor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.util.ThrowingRunnable;
import io.opentelemetry.sdk.testing.assertj.EventDataAssert;
import io.opentelemetry.sdk.testing.assertj.MetricAssertions;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -214,6 +216,56 @@ public void onCompleted() {
SemanticAttributes.RPC_GRPC_STATUS_CODE,
(long) Status.Code.OK.value()))
.hasEventsSatisfyingExactly(events.toArray(new Consumer[0]))));
testing()
.waitAndAssertMetrics(
"io.opentelemetry.grpc-1.6",
"rpc.server.duration",
metrics ->
metrics.anySatisfy(
metric ->
MetricAssertions.assertThat(metric)
.hasUnit("ms")
.hasDoubleHistogram()
.points()
.anySatisfy(
point ->
MetricAssertions.assertThat(point)
.hasAttributes(
Attributes.builder()
.put(SemanticAttributes.NET_TRANSPORT, "ip_tcp")
.put(SemanticAttributes.RPC_METHOD, "Conversation")
.put(
SemanticAttributes.RPC_SERVICE,
"example.Greeter")
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
.build()))));
testing()
.waitAndAssertMetrics(
"io.opentelemetry.grpc-1.6",
"rpc.client.duration",
metrics ->
metrics.anySatisfy(
metric ->
MetricAssertions.assertThat(metric)
.hasUnit("ms")
.hasDoubleHistogram()
.points()
.allSatisfy(
point ->
MetricAssertions.assertThat(point)
.hasAttributes(
Attributes.builder()
.put(SemanticAttributes.NET_PEER_NAME, "localhost")
.put(
SemanticAttributes.NET_PEER_PORT,
server.getPort())
.put(SemanticAttributes.NET_TRANSPORT, "ip_tcp")
.put(SemanticAttributes.RPC_METHOD, "Conversation")
.put(
SemanticAttributes.RPC_SERVICE,
"example.Greeter")
.put(SemanticAttributes.RPC_SYSTEM, "grpc")
.build()))));
}

private ManagedChannel createChannel(Server server) throws Exception {
Expand Down
Loading

0 comments on commit 0f222db

Please sign in to comment.