Rename http.resend_count to http.request.resend_count (#9700)

This commit is contained in:
Mateusz Rzeszutek 2023-10-17 20:03:48 +02:00 committed by GitHub
parent 60b65488cb
commit ea8f3d0e24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 65 additions and 57 deletions

View File

@ -10,6 +10,7 @@ import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorU
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;
@ -117,7 +118,7 @@ public final class HttpClientAttributesExtractor<REQUEST, RESPONSE>
int resendCount = resendCountIncrementer.applyAsInt(parentContext);
if (resendCount > 0) {
attributes.put(SemanticAttributes.HTTP_RESEND_COUNT, resendCount);
attributes.put(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount);
}
}

View File

@ -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,

View File

@ -11,12 +11,12 @@ import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
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.
@ -29,7 +29,7 @@ public final class HttpClientResendCount {
if (context.get(KEY) != null) {
return context;
}
return context.with(KEY, new HttpClientResendCount());
return context.with(KEY, new HttpClientRequestResendCount());
}
/**
@ -37,12 +37,12 @@ public final class HttpClientResendCount {
* 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;
}
@ -52,5 +52,5 @@ public final class HttpClientResendCount {
@SuppressWarnings("unused") // it actually is used by the resendsUpdater
private volatile int resends = 0;
private HttpClientResendCount() {}
private HttpClientRequestResendCount() {}
}

View File

@ -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;
@ -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() {}
}

View File

@ -17,6 +17,7 @@ import io.opentelemetry.api.common.Attributes;
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;
@ -149,7 +150,7 @@ class HttpClientAttributesExtractorTest {
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);

View File

@ -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);
}
}

View File

@ -1,28 +0,0 @@
/*
* 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 HttpClientResendCountTest {
@Test
void resendCountShouldBeZeroWhenNotInitialized() {
assertThat(HttpClientResendCount.getAndIncrement(Context.root())).isEqualTo(0);
assertThat(HttpClientResendCount.getAndIncrement(Context.root())).isEqualTo(0);
}
@Test
void incrementResendCount() {
Context context = HttpClientResendCount.initialize(Context.root());
assertThat(HttpClientResendCount.getAndIncrement(context)).isEqualTo(0);
assertThat(HttpClientResendCount.getAndIncrement(context)).isEqualTo(1);
}
}

View File

@ -16,6 +16,7 @@ import io.opentelemetry.api.common.Attributes;
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;
@ -146,7 +147,7 @@ class HttpClientAttributesExtractorBothSemconvTest {
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);

View File

@ -164,7 +164,7 @@ class HttpClientAttributesExtractorStableSemconvTest {
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);

View File

@ -12,7 +12,7 @@ import io.opentelemetry.context.Context;
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;
@ -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());
}
};

View File

@ -7,7 +7,7 @@ package io.opentelemetry.instrumentation.okhttp.v3_0;
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;
@ -23,7 +23,7 @@ final class ContextInterceptor implements Interceptor {
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);
}

View File

@ -7,7 +7,7 @@ package io.opentelemetry.instrumentation.okhttp.v3_0.internal;
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;
@ -42,7 +42,7 @@ public final class ConnectionErrorSpanInterceptor implements Interceptor {
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());

View File

@ -8,7 +8,7 @@ package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0;
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;
@ -118,7 +118,7 @@ public final class HttpResponseReceiverInstrumenter {
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);

View File

@ -8,7 +8,7 @@ package io.opentelemetry.javaagent.instrumentation.reactornetty.v1_0;
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;
@ -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();

View File

@ -279,7 +279,7 @@ public abstract class AbstractHttpClientTest<REQUEST> implements HttpClientTypeA
if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
@ -319,7 +319,7 @@ public abstract class AbstractHttpClientTest<REQUEST> implements HttpClientTypeA
if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
@ -378,7 +378,7 @@ public abstract class AbstractHttpClientTest<REQUEST> implements HttpClientTypeA
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()));
@ -425,7 +425,7 @@ public abstract class AbstractHttpClientTest<REQUEST> implements HttpClientTypeA
if (options.isLowLevelInstrumentation()) {
testing.waitAndAssertSortedTraces(
comparingRootSpanAttribute(SemanticAttributes.HTTP_RESEND_COUNT),
comparingRootSpanAttribute(HttpAttributes.HTTP_REQUEST_RESEND_COUNT),
trace -> {
trace.hasSpansSatisfyingExactly(
span ->
@ -1123,9 +1123,9 @@ public abstract class AbstractHttpClientTest<REQUEST> implements HttpClientTypeA
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);
}
});
}