Skip to content

Commit

Permalink
add tag ditto_channel to round trip timer;
Browse files Browse the repository at this point in the history
determine channel tag in TraceUtils based on the channel header or query parameter;

Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Oct 8, 2021
1 parent b4ec72d commit 522e7cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.PreparedTimer;

Expand All @@ -32,6 +33,8 @@ public final class TraceUtils {

private static final String HTTP_ROUNDTRIP_METRIC_NAME = "roundtrip_http";
private static final String FILTER_AUTH_METRIC_NAME = "filter_auth";
private static final String LIVE_CHANNEL_NAME = "live";
private static final String TWIN_CHANNEL_NAME = "twin";

private TraceUtils() {
throw new AssertionError();
Expand All @@ -46,18 +49,20 @@ private TraceUtils() {
public static PreparedTimer newHttpRoundTripTimer(final HttpRequest request) {
final String requestMethod = request.method().name();
final String requestPath = request.getUri().toRelative().path();
final String channel = determineChannel(request);

final TraceInformation traceInformation = determineTraceInformation(requestPath);

return newExpiringTimer(HTTP_ROUNDTRIP_METRIC_NAME)
.tags(traceInformation.getTags())
.tag(TracingTags.REQUEST_METHOD, requestMethod);
.tag(TracingTags.REQUEST_METHOD, requestMethod)
.tag(TracingTags.CHANNEL, channel);
}

/**
* Prepares an {@link PreparedTimer} with default {@link #FILTER_AUTH_METRIC_NAME} and tags.
*
* @param authenticationType The name of the authentication type (i.e. jwt, ..)
* @param authenticationType The name of the authentication type (i.e. jwt, ...)
* @return The prepared {@link PreparedTimer}
*/
public static PreparedTimer newAuthFilterTimer(final CharSequence authenticationType) {
Expand All @@ -67,7 +72,7 @@ public static PreparedTimer newAuthFilterTimer(final CharSequence authentication
/**
* Prepares an {@link PreparedTimer} with default {@link #FILTER_AUTH_METRIC_NAME} and tags.
*
* @param authenticationType The name of the authentication type (i.e. jwt,...)
* @param authenticationType The name of the authentication type (i.e. jwt, ...)
* @param request The HttpRequest used to extract required tags.
* @return The prepared {@link PreparedTimer}
*/
Expand Down Expand Up @@ -106,12 +111,25 @@ private static TraceInformation determineTraceInformation(final String requestPa
return traceUriGenerator.apply(requestPath);
}

private static String determineChannel(final HttpRequest request) {
// determine channel based on header or query parameter
final boolean liveHeaderPresent = request.getHeader(DittoHeaderDefinition.CHANNEL.getKey())
.filter(channelHeader -> LIVE_CHANNEL_NAME.equals(channelHeader.value()))
.isPresent();
final boolean liveQueryPresent = request.getUri().query().get(DittoHeaderDefinition.CHANNEL.getKey())
.filter(LIVE_CHANNEL_NAME::equals)
.isPresent();
// messages are always live commands
final boolean messageRequest = request.getUri().path().contains("messages");

return (liveHeaderPresent || liveQueryPresent || messageRequest) ? LIVE_CHANNEL_NAME : TWIN_CHANNEL_NAME;
}

/**
* Replaces all characters that are invalid for metrics (at least for Prometheus metrics).
*/
public static String metricizeTraceUri(final String traceUri) {
return traceUri.replaceAll("[./:-]", TRACING_FILTER_DELIMITER);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public final class TracingTags {
// General
public static final String CORRELATION_ID = PREFIX + "correlationId";
public static final String SIGNAL_TYPE = PREFIX + "signal.type";
public static final String CHANNEL = PREFIX + "channel";

// connectivity tags
public static final String CONNECTION_ID = PREFIX + "connection.id";
Expand Down

0 comments on commit 522e7cf

Please sign in to comment.