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 metrics rpc conventions implement #4838

Merged
merged 12 commits into from
Dec 11, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.rpc;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;

/**
* filter rpc metrics unnecessary attributes.
*/
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
public class MetricsView {
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved

private static final Set<AttributeKey> recommended = buildRecommended();
private static final Set<AttributeKey> optional = buildOptional();

private static Set<AttributeKey> buildRecommended() {
// the list of Recommended metrics attributes is from
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/rpc.md#attributes
Set<AttributeKey> view = new HashSet<>();
view.add(SemanticAttributes.RPC_SYSTEM);
view.add(SemanticAttributes.RPC_SERVICE);
view.add(SemanticAttributes.RPC_METHOD);
return view;
}

private static Set<AttributeKey> buildOptional() {
// the list of Recommended metrics attributes is from
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/rpc.md#attributes
Set<AttributeKey> view = new HashSet<>();
view.add(SemanticAttributes.NET_PEER_IP);
view.add(SemanticAttributes.NET_PEER_NAME);
view.add(SemanticAttributes.NET_PEER_PORT);
view.add(SemanticAttributes.NET_TRANSPORT);
return view;
}

static Attributes applyRpcView(Attributes startAttributes, Attributes endAttributes) {
Attributes attributes = startAttributes.toBuilder().putAll(endAttributes).build();
AttributesBuilder filtered = Attributes.builder();
applyView(filtered, attributes, recommended);
applyView(filtered, attributes, optional);
return filtered.build();
}

@SuppressWarnings("unchecked")
private static void applyView(
AttributesBuilder filtered, Attributes attributes, Set<AttributeKey> view) {
attributes.forEach(
(BiConsumer<AttributeKey, Object>)
(key, value) -> {
if (view.contains(key)) {
filtered.put(key, value);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.rpc;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
import io.opentelemetry.instrumentation.api.instrumenter.RequestListener;
import io.opentelemetry.instrumentation.api.instrumenter.RequestMetrics;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* guide from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/rpc.md#rpc-client
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
*/
@UnstableApi
public class RpcClientMetrics implements RequestListener {
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved

private static final ContextKey<RpcClientMetrics.State> RPC_CLIENT_REQUEST_METRICS_STATE =
ContextKey.named("rpc-client-request-metrics-state");

private static final Logger logger = LoggerFactory.getLogger(RpcClientMetrics.class);

/**
* measures duration of outbound RPC.
*/
private final LongHistogram clientDurationHistogram;

private RpcClientMetrics(Meter meter) {
clientDurationHistogram = meter
.histogramBuilder("rpc.client.duration")
.setDescription("measures duration of outbound RPC")
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
.setUnit("milliseconds")
.ofLongs().build();
}

/**
* Returns a {@link RequestMetrics} which can be used to enable recording of {@link
* RpcClientMetrics} on an {@link
* io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder}
* method addRequestMetrics().
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
*/
@UnstableApi
public static RequestMetrics get() {
return RpcClientMetrics::new;
}

@Override
public Context start(Context context, Attributes startAttributes, long startNanos) {
return context.with(RPC_CLIENT_REQUEST_METRICS_STATE,
new AutoValue_RpcClientMetrics_State(startAttributes, startNanos));
}

@Override
public void end(Context context, Attributes endAttributes, long endNanos) {
State state = context.get(RPC_CLIENT_REQUEST_METRICS_STATE);
if (state == null) {
logger.debug(
"No state present when ending context {}. Cannot reset RPC request metrics.", context);
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
trask marked this conversation as resolved.
Show resolved Hide resolved
}
clientDurationHistogram.record(
TimeUnit.MILLISECONDS.convert(
endNanos - state.startTimeNanos(), TimeUnit.NANOSECONDS),
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
MetricsView.applyRpcView(state.startAttributes(), endAttributes), context);
}

@AutoValue
abstract static class State {

abstract Attributes startAttributes();

abstract long startTimeNanos();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.rpc;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
import io.opentelemetry.instrumentation.api.instrumenter.RequestListener;
import io.opentelemetry.instrumentation.api.instrumenter.RequestMetrics;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* guide from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/rpc.md#rpc-server
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
*/
@UnstableApi
public class RpcServerMetrics implements RequestListener {
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved

private static final ContextKey<RpcServerMetrics.State> RPC_SERVER_REQUEST_METRICS_STATE =
ContextKey.named("rpc-server-request-metrics-state");

private static final Logger logger = LoggerFactory.getLogger(RpcServerMetrics.class);

/**
* measures duration of inbound RPC.
*/
private final LongHistogram serverDurationHistogram;

private RpcServerMetrics(Meter meter) {
serverDurationHistogram = meter
.histogramBuilder("rpc.server.duration")
.setDescription("measures duration of inbound RPC")
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
.setUnit("milliseconds")
.ofLongs().build();
}

/**
* Returns a {@link RequestMetrics} which can be used to enable recording of {@link
* RpcServerMetrics} on an {@link
* io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder}
* method addRequestMetrics().
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
*/
@UnstableApi
public static RequestMetrics get() {
return RpcServerMetrics::new;
}

@Override
public Context start(Context context, Attributes startAttributes, long startNanos) {
return context.with(RPC_SERVER_REQUEST_METRICS_STATE,
new AutoValue_RpcServerMetrics_State(startAttributes, startNanos));
}

@Override
public void end(Context context, Attributes endAttributes, long endNanos) {
State state = context.get(RPC_SERVER_REQUEST_METRICS_STATE);
if (state == null) {
logger.debug(
"No state present when ending context {}. Cannot reset RPC request metrics.", context);
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
trask marked this conversation as resolved.
Show resolved Hide resolved
}
serverDurationHistogram.record(
TimeUnit.MILLISECONDS.convert(
endNanos - state.startTimeNanos(), TimeUnit.NANOSECONDS),
yangtaoran marked this conversation as resolved.
Show resolved Hide resolved
MetricsView.applyRpcView(state.startAttributes(), endAttributes), context);
}

@AutoValue
abstract static class State {

abstract Attributes startAttributes();

abstract long startTimeNanos();

}
}