-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rpc metrics conventions implement
add rpc metrics conventions implement fix AutoValue State fix AutoValue State update RpcClientMetrics Visibility update RpcClientMetrics Visibility update the filed serverDurationHistogram be final update the filed serverDurationHistogram be final update MetricsView code style update RpcClientMetrics code style update RpcServerMetrics code style update MetricsView doc update MetricsView doc
- Loading branch information
1 parent
735c1b0
commit bc546ee
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/MetricsView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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. | ||
*/ | ||
public class MetricsView { | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/RpcClientMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 | ||
*/ | ||
@UnstableApi | ||
public class RpcClientMetrics implements RequestListener { | ||
|
||
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") | ||
.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(). | ||
*/ | ||
@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); | ||
} | ||
clientDurationHistogram.record( | ||
TimeUnit.MILLISECONDS.convert( | ||
endNanos - state.startTimeNanos(), TimeUnit.NANOSECONDS), | ||
MetricsView.applyRpcView(state.startAttributes(), endAttributes), context); | ||
} | ||
|
||
@AutoValue | ||
abstract static class State { | ||
|
||
abstract Attributes startAttributes(); | ||
|
||
abstract long startTimeNanos(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...src/main/java/io/opentelemetry/instrumentation/api/instrumenter/rpc/RpcServerMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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 | ||
*/ | ||
@UnstableApi | ||
public class RpcServerMetrics implements RequestListener { | ||
|
||
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") | ||
.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(). | ||
*/ | ||
@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); | ||
} | ||
serverDurationHistogram.record( | ||
TimeUnit.MILLISECONDS.convert( | ||
endNanos - state.startTimeNanos(), TimeUnit.NANOSECONDS), | ||
MetricsView.applyRpcView(state.startAttributes(), endAttributes), context); | ||
} | ||
|
||
@AutoValue | ||
abstract static class State { | ||
|
||
abstract Attributes startAttributes(); | ||
|
||
abstract long startTimeNanos(); | ||
|
||
} | ||
} | ||
|
||
|