|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.armeria.v1_3; |
| 7 | + |
| 8 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 9 | +import com.linecorp.armeria.client.ClientRequestContext; |
| 10 | +import com.linecorp.armeria.common.logging.RequestLog; |
| 11 | +import io.opentelemetry.api.OpenTelemetry; |
| 12 | +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder; |
| 13 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 14 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 15 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; |
| 16 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder; |
| 17 | +import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaInstrumenterBuilderFactory; |
| 18 | +import io.opentelemetry.instrumentation.armeria.v1_3.internal.ArmeriaInstrumenterBuilderUtil; |
| 19 | +import io.opentelemetry.instrumentation.armeria.v1_3.internal.Experimental; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +public final class ArmeriaClientTelemetryBuilder { |
| 25 | + |
| 26 | + private final DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog> builder; |
| 27 | + |
| 28 | + static { |
| 29 | + ArmeriaInstrumenterBuilderUtil.setClientBuilderExtractor(builder -> builder.builder); |
| 30 | + Experimental.setSetEmitExperimentalClientTelemetry( |
| 31 | + (builder, emit) -> builder.builder.setEmitExperimentalHttpClientMetrics(emit)); |
| 32 | + Experimental.setSetClientPeerService( |
| 33 | + (builder, peerService) -> builder.builder.setPeerService(peerService)); |
| 34 | + } |
| 35 | + |
| 36 | + ArmeriaClientTelemetryBuilder(OpenTelemetry openTelemetry) { |
| 37 | + builder = ArmeriaInstrumenterBuilderFactory.getClientBuilder(openTelemetry); |
| 38 | + } |
| 39 | + |
| 40 | + /** Sets the status extractor for client spans. */ |
| 41 | + @CanIgnoreReturnValue |
| 42 | + public ArmeriaClientTelemetryBuilder setStatusExtractor( |
| 43 | + Function< |
| 44 | + SpanStatusExtractor<? super ClientRequestContext, ? super RequestLog>, |
| 45 | + ? extends SpanStatusExtractor<? super ClientRequestContext, ? super RequestLog>> |
| 46 | + statusExtractor) { |
| 47 | + builder.setStatusExtractor(statusExtractor); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Adds an extra {@link AttributesExtractor} to invoke to set attributes to instrumented items. |
| 53 | + * The {@link AttributesExtractor} will be executed after all default extractors. |
| 54 | + */ |
| 55 | + @CanIgnoreReturnValue |
| 56 | + public ArmeriaClientTelemetryBuilder addAttributesExtractor( |
| 57 | + AttributesExtractor<? super ClientRequestContext, ? super RequestLog> attributesExtractor) { |
| 58 | + builder.addAttributesExtractor(attributesExtractor); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Configures the HTTP client request headers that will be captured as span attributes. |
| 64 | + * |
| 65 | + * @param requestHeaders A list of HTTP header names. |
| 66 | + */ |
| 67 | + @CanIgnoreReturnValue |
| 68 | + public ArmeriaClientTelemetryBuilder setCapturedRequestHeaders(List<String> requestHeaders) { |
| 69 | + builder.setCapturedRequestHeaders(requestHeaders); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Configures the HTTP client response headers that will be captured as span attributes. |
| 75 | + * |
| 76 | + * @param responseHeaders A list of HTTP header names. |
| 77 | + */ |
| 78 | + @CanIgnoreReturnValue |
| 79 | + public ArmeriaClientTelemetryBuilder setCapturedResponseHeaders(List<String> responseHeaders) { |
| 80 | + builder.setCapturedResponseHeaders(responseHeaders); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Configures the instrumentation to recognize an alternative set of HTTP request methods. |
| 86 | + * |
| 87 | + * <p>By default, this instrumentation defines "known" methods as the ones listed in <a |
| 88 | + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH |
| 89 | + * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>. |
| 90 | + * |
| 91 | + * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does |
| 92 | + * not supplement it. |
| 93 | + * |
| 94 | + * @param knownMethods A set of recognized HTTP request methods. |
| 95 | + * @see HttpClientAttributesExtractorBuilder#setKnownMethods(Set) |
| 96 | + */ |
| 97 | + @CanIgnoreReturnValue |
| 98 | + public ArmeriaClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) { |
| 99 | + builder.setKnownMethods(knownMethods); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + /** Sets custom client {@link SpanNameExtractor} via transform function. */ |
| 104 | + @CanIgnoreReturnValue |
| 105 | + public ArmeriaClientTelemetryBuilder setSpanNameExtractor( |
| 106 | + Function< |
| 107 | + SpanNameExtractor<? super ClientRequestContext>, |
| 108 | + ? extends SpanNameExtractor<? super ClientRequestContext>> |
| 109 | + clientSpanNameExtractor) { |
| 110 | + builder.setSpanNameExtractor(clientSpanNameExtractor); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public ArmeriaClientTelemetry build() { |
| 115 | + return new ArmeriaClientTelemetry(builder.build()); |
| 116 | + } |
| 117 | +} |
0 commit comments