Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metric support for grpc #5923

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
trask marked this conversation as resolved.
Show resolved Hide resolved
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