Skip to content

Commit

Permalink
Rename http.resend_count to http.request.resend_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Oct 17, 2023
1 parent 5203f8e commit 90f247a
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.InternalNetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalNetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.network.internal.InternalServerAttributesExtractor;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST

int resendCount = resendCountIncrementer.applyAsInt(parentContext);
if (resendCount > 0) {
attributes.put(SemanticAttributes.HTTP_RESEND_COUNT, resendCount);
attributes.put(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class HttpClientAttributesExtractorBuilder<REQUEST, RESPONSE> {
List<String> capturedRequestHeaders = emptyList();
List<String> capturedResponseHeaders = emptyList();
Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
ToIntFunction<Context> resendCountIncrementer = HttpClientResendCount::getAndIncrement;
ToIntFunction<Context> resendCountIncrementer = HttpClientRequestResendCount::getAndIncrement;

HttpClientAttributesExtractorBuilder(
HttpClientAttributesGetter<REQUEST, RESPONSE> httpAttributesGetter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/** A helper that keeps track of the count of the HTTP request resend attempts. */
public final class HttpClientResendCount {
public final class HttpClientRequestResendCount {

private static final ContextKey<HttpClientResendCount> KEY =
private static final ContextKey<HttpClientRequestResendCount> KEY =
ContextKey.named("opentelemetry-http-client-resend-key");
private static final AtomicIntegerFieldUpdater<HttpClientResendCount> resendsUpdater =
AtomicIntegerFieldUpdater.newUpdater(HttpClientResendCount.class, "resends");
private static final AtomicIntegerFieldUpdater<HttpClientRequestResendCount> resendsUpdater =
AtomicIntegerFieldUpdater.newUpdater(HttpClientRequestResendCount.class, "resends");

/**
* Initializes the HTTP request resend counter.
Expand All @@ -29,20 +29,20 @@ public static Context initialize(Context context) {
if (context.get(KEY) != null) {
return context;
}
return context.with(KEY, new HttpClientResendCount());
return context.with(KEY, new HttpClientRequestResendCount());
}

/**
* Returns the count of the already made attempts to send an HTTP request; 0 if this is the first
* send attempt.
*/
public static int get(Context context) {
HttpClientResendCount resend = context.get(KEY);
HttpClientRequestResendCount resend = context.get(KEY);
return resend == null ? 0 : resend.resends;
}

static int getAndIncrement(Context context) {
HttpClientResendCount resend = context.get(KEY);
HttpClientRequestResendCount resend = context.get(KEY);
if (resend == null) {
return 0;
}
Expand All @@ -52,5 +52,5 @@ static int getAndIncrement(Context context) {
@SuppressWarnings("unused") // it actually is used by the resendsUpdater
private volatile int resends = 0;

private HttpClientResendCount() {}
private HttpClientRequestResendCount() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.api.instrumenter.http.internal;

import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;
Expand All @@ -20,5 +21,8 @@ public final class HttpAttributes {

public static final AttributeKey<String> ERROR_TYPE = stringKey("error.type");

public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT =
longKey("http.request.resend_count");

private HttpAttributes() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -149,7 +150,7 @@ void normal() {
asList("123", "456")),
entry(SemanticAttributes.NET_PEER_NAME, "github.com"),
entry(SemanticAttributes.NET_PEER_PORT, 123L),
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.context.Context;
import org.junit.jupiter.api.Test;

class HttpClientRequestResendCountTest {

@Test
void resendCountShouldBeZeroWhenNotInitialized() {
assertThat(HttpClientRequestResendCount.getAndIncrement(Context.root())).isEqualTo(0);
assertThat(HttpClientRequestResendCount.getAndIncrement(Context.root())).isEqualTo(0);
}

@Test
void incrementResendCount() {
Context context = HttpClientRequestResendCount.initialize(Context.root());

assertThat(HttpClientRequestResendCount.getAndIncrement(context)).isEqualTo(0);
assertThat(HttpClientRequestResendCount.getAndIncrement(context)).isEqualTo(1);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -146,7 +147,7 @@ void normal() {
entry(SemanticAttributes.NET_PEER_PORT, 123L),
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
entry(SemanticAttributes.SERVER_PORT, 123L),
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void normal() {
asList("123", "456")),
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
entry(SemanticAttributes.SERVER_PORT, 123L),
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientPeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientRequestResendCount;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.ConnectionErrorSpanInterceptor;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpAttributesGetter;
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.OkHttpInstrumenterFactory;
Expand Down Expand Up @@ -41,7 +41,8 @@ public final class OkHttp3Singletons {

public static final Interceptor CONTEXT_INTERCEPTOR =
chain -> {
try (Scope ignored = HttpClientResendCount.initialize(Context.current()).makeCurrent()) {
try (Scope ignored =
HttpClientRequestResendCount.initialize(Context.current()).makeCurrent()) {
return chain.proceed(chain.request());
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientRequestResendCount;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
Expand All @@ -23,7 +23,7 @@ public Response intercept(Chain chain) throws IOException {
parentContext = Context.current();
}
// include the resend counter
Context context = HttpClientResendCount.initialize(parentContext);
Context context = HttpClientRequestResendCount.initialize(parentContext);
try (Scope ignored = context.makeCurrent()) {
return chain.proceed(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientRequestResendCount;
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
import java.io.IOException;
import java.time.Instant;
Expand Down Expand Up @@ -42,7 +42,7 @@ public Response intercept(Chain chain) throws IOException {
throw t;
} finally {
// only create a span when there wasn't any HTTP request
if (HttpClientResendCount.get(parentContext) == 0) {
if (HttpClientRequestResendCount.get(parentContext) == 0) {
if (instrumenter.shouldStart(parentContext, request)) {
InstrumenterUtil.startAndEnd(
instrumenter, parentContext, request, response, error, startTime, Instant.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0.ReactorContextKeys.CONTEXTS_HOLDER_KEY;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientRequestResendCount;
import io.opentelemetry.instrumentation.netty.v4_1.NettyClientTelemetry;
import java.util.function.BiConsumer;
import java.util.function.Function;
Expand Down Expand Up @@ -118,7 +118,7 @@ private static final class EndOperationWithRequestError
public void accept(HttpClientRequest request, Throwable error) {
instrumentationContexts.endClientSpan(null, error);

if (HttpClientResendCount.get(instrumentationContexts.getParentContext()) == 0) {
if (HttpClientRequestResendCount.get(instrumentationContexts.getParentContext()) == 0) {
// request is an instance of FailedHttpClientRequest, which does not implement a correct
// resourceUrl() method -- we have to work around that
request = FailedRequestWithUrlMaker.create(config, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0.ReactorNettySingletons.instrumenter;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientRequestResendCount;
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
import io.opentelemetry.instrumentation.api.internal.Timer;
import io.opentelemetry.instrumentation.api.util.VirtualField;
Expand Down Expand Up @@ -36,7 +36,7 @@ final class InstrumentationContexts {
private final Queue<RequestAndContext> clientContexts = new LinkedBlockingQueue<>();

void initialize(Context parentContext) {
Context parentContextWithResends = HttpClientResendCount.initialize(parentContext);
Context parentContextWithResends = HttpClientRequestResendCount.initialize(parentContext);
// make sure initialization happens only once
if (parentContextUpdater.compareAndSet(this, null, parentContextWithResends)) {
timer = Timer.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void basicRequestWith1Redirect() throws Exception {

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down Expand Up @@ -319,7 +319,7 @@ void basicRequestWith2Redirects() throws Exception {

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down Expand Up @@ -378,7 +378,7 @@ void circularRedirects() {

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
IntStream.range(0, options.getMaxRedirects())
.mapToObj(i -> makeCircularRedirectAssertForLolLevelTrace(uri, method, i))
.collect(Collectors.toList()));
Expand Down Expand Up @@ -425,7 +425,7 @@ void redirectToSecuredCopiesAuthHeader() throws Exception {

if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
Expand Down Expand Up @@ -1123,9 +1123,9 @@ SpanDataAssert assertClientSpan(

if (resendCount != null) {
assertThat(attrs)
.containsEntry(SemanticAttributes.HTTP_RESEND_COUNT, (long) resendCount);
.containsEntry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, (long) resendCount);
} else {
assertThat(attrs).doesNotContainKey(SemanticAttributes.HTTP_RESEND_COUNT);
assertThat(attrs).doesNotContainKey(HttpAttributes.HTTP_REQUEST_RESEND_COUNT);
}
});
}
Expand Down

0 comments on commit 90f247a

Please sign in to comment.