|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.incubator.builder.internal; |
| 7 | + |
| 8 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 9 | +import io.opentelemetry.api.OpenTelemetry; |
| 10 | +import io.opentelemetry.context.propagation.TextMapGetter; |
| 11 | +import io.opentelemetry.instrumentation.api.incubator.config.internal.CommonConfig; |
| 12 | +import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpExperimentalAttributesExtractor; |
| 13 | +import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics; |
| 14 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 15 | +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; |
| 16 | +import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; |
| 17 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; |
| 18 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 19 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; |
| 20 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor; |
| 21 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder; |
| 22 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter; |
| 23 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics; |
| 24 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; |
| 25 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteBuilder; |
| 26 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor; |
| 27 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractorBuilder; |
| 28 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.function.Consumer; |
| 33 | +import java.util.function.Function; |
| 34 | +import java.util.function.Supplier; |
| 35 | +import javax.annotation.Nullable; |
| 36 | + |
| 37 | +/** |
| 38 | + * This class is internal and is hence not for public use. Its APIs are unstable and can change at |
| 39 | + * any time. |
| 40 | + */ |
| 41 | +public final class DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> { |
| 42 | + |
| 43 | + private final String instrumentationName; |
| 44 | + private final OpenTelemetry openTelemetry; |
| 45 | + |
| 46 | + private final List<AttributesExtractor<? super REQUEST, ? super RESPONSE>> additionalExtractors = |
| 47 | + new ArrayList<>(); |
| 48 | + private Function< |
| 49 | + SpanStatusExtractor<? super REQUEST, ? super RESPONSE>, |
| 50 | + ? extends SpanStatusExtractor<? super REQUEST, ? super RESPONSE>> |
| 51 | + statusExtractorTransformer = Function.identity(); |
| 52 | + private final HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> |
| 53 | + httpAttributesExtractorBuilder; |
| 54 | + private final HttpSpanNameExtractorBuilder<REQUEST> httpSpanNameExtractorBuilder; |
| 55 | + |
| 56 | + @Nullable private TextMapGetter<REQUEST> headerGetter; |
| 57 | + private Function<SpanNameExtractor<REQUEST>, ? extends SpanNameExtractor<? super REQUEST>> |
| 58 | + spanNameExtractorTransformer = Function.identity(); |
| 59 | + private final HttpServerRouteBuilder<REQUEST> httpServerRouteBuilder; |
| 60 | + private final HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter; |
| 61 | + private boolean emitExperimentalHttpServerMetrics = false; |
| 62 | + |
| 63 | + public DefaultHttpServerInstrumenterBuilder( |
| 64 | + String instrumentationName, |
| 65 | + OpenTelemetry openTelemetry, |
| 66 | + HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter) { |
| 67 | + this.instrumentationName = instrumentationName; |
| 68 | + this.openTelemetry = openTelemetry; |
| 69 | + httpAttributesExtractorBuilder = HttpServerAttributesExtractor.builder(attributesGetter); |
| 70 | + httpSpanNameExtractorBuilder = HttpSpanNameExtractor.builder(attributesGetter); |
| 71 | + httpServerRouteBuilder = HttpServerRoute.builder(attributesGetter); |
| 72 | + this.attributesGetter = attributesGetter; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented |
| 77 | + * items. |
| 78 | + */ |
| 79 | + @CanIgnoreReturnValue |
| 80 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> addAttributesExtractor( |
| 81 | + AttributesExtractor<? super REQUEST, ? super RESPONSE> attributesExtractor) { |
| 82 | + additionalExtractors.add(attributesExtractor); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + @CanIgnoreReturnValue |
| 87 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setStatusExtractor( |
| 88 | + Function< |
| 89 | + SpanStatusExtractor<? super REQUEST, ? super RESPONSE>, |
| 90 | + ? extends SpanStatusExtractor<? super REQUEST, ? super RESPONSE>> |
| 91 | + statusExtractor) { |
| 92 | + this.statusExtractorTransformer = statusExtractor; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Configures the HTTP request headers that will be captured as span attributes. |
| 98 | + * |
| 99 | + * @param requestHeaders A list of HTTP header names. |
| 100 | + */ |
| 101 | + @CanIgnoreReturnValue |
| 102 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setCapturedRequestHeaders( |
| 103 | + List<String> requestHeaders) { |
| 104 | + httpAttributesExtractorBuilder.setCapturedRequestHeaders(requestHeaders); |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Configures the HTTP response headers that will be captured as span attributes. |
| 110 | + * |
| 111 | + * @param responseHeaders A list of HTTP header names. |
| 112 | + */ |
| 113 | + @CanIgnoreReturnValue |
| 114 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setCapturedResponseHeaders( |
| 115 | + List<String> responseHeaders) { |
| 116 | + httpAttributesExtractorBuilder.setCapturedResponseHeaders(responseHeaders); |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Configures the instrumentation to recognize an alternative set of HTTP request methods. |
| 122 | + * |
| 123 | + * <p>By default, this instrumentation defines "known" methods as the ones listed in <a |
| 124 | + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH |
| 125 | + * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>. |
| 126 | + * |
| 127 | + * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does |
| 128 | + * not supplement it. |
| 129 | + * |
| 130 | + * @param knownMethods A set of recognized HTTP request methods. |
| 131 | + * @see HttpServerAttributesExtractorBuilder#setKnownMethods(Set) |
| 132 | + */ |
| 133 | + @CanIgnoreReturnValue |
| 134 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setKnownMethods( |
| 135 | + Set<String> knownMethods) { |
| 136 | + httpAttributesExtractorBuilder.setKnownMethods(knownMethods); |
| 137 | + httpSpanNameExtractorBuilder.setKnownMethods(knownMethods); |
| 138 | + httpServerRouteBuilder.setKnownMethods(knownMethods); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + @CanIgnoreReturnValue |
| 143 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setHeaderGetter( |
| 144 | + @Nullable TextMapGetter<REQUEST> headerGetter) { |
| 145 | + this.headerGetter = headerGetter; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Configures the instrumentation to emit experimental HTTP server metrics. |
| 151 | + * |
| 152 | + * @param emitExperimentalHttpServerMetrics {@code true} if the experimental HTTP server metrics |
| 153 | + * are to be emitted. |
| 154 | + */ |
| 155 | + @CanIgnoreReturnValue |
| 156 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> |
| 157 | + setEmitExperimentalHttpServerMetrics(boolean emitExperimentalHttpServerMetrics) { |
| 158 | + this.emitExperimentalHttpServerMetrics = emitExperimentalHttpServerMetrics; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + /** Sets custom {@link SpanNameExtractor} via transform function. */ |
| 163 | + @CanIgnoreReturnValue |
| 164 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setSpanNameExtractor( |
| 165 | + Function<SpanNameExtractor<REQUEST>, ? extends SpanNameExtractor<? super REQUEST>> |
| 166 | + spanNameExtractorTransformer) { |
| 167 | + this.spanNameExtractorTransformer = spanNameExtractorTransformer; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + public Instrumenter<REQUEST, RESPONSE> build() { |
| 172 | + InstrumenterBuilder<REQUEST, RESPONSE> builder = builder(); |
| 173 | + |
| 174 | + if (headerGetter != null) { |
| 175 | + return builder.buildServerInstrumenter(headerGetter); |
| 176 | + } |
| 177 | + return builder.buildInstrumenter(SpanKindExtractor.alwaysServer()); |
| 178 | + } |
| 179 | + |
| 180 | + private InstrumenterBuilder<REQUEST, RESPONSE> builder() { |
| 181 | + SpanNameExtractor<? super REQUEST> spanNameExtractor = |
| 182 | + spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build()); |
| 183 | + |
| 184 | + InstrumenterBuilder<REQUEST, RESPONSE> builder = |
| 185 | + Instrumenter.<REQUEST, RESPONSE>builder( |
| 186 | + openTelemetry, instrumentationName, spanNameExtractor) |
| 187 | + .setSpanStatusExtractor( |
| 188 | + statusExtractorTransformer.apply(HttpSpanStatusExtractor.create(attributesGetter))) |
| 189 | + .addAttributesExtractor(httpAttributesExtractorBuilder.build()) |
| 190 | + .addAttributesExtractors(additionalExtractors) |
| 191 | + .addContextCustomizer(httpServerRouteBuilder.build()) |
| 192 | + .addOperationMetrics(HttpServerMetrics.get()); |
| 193 | + if (emitExperimentalHttpServerMetrics) { |
| 194 | + builder |
| 195 | + .addAttributesExtractor(HttpExperimentalAttributesExtractor.create(attributesGetter)) |
| 196 | + .addOperationMetrics(HttpServerExperimentalMetrics.get()); |
| 197 | + } |
| 198 | + |
| 199 | + return builder; |
| 200 | + } |
| 201 | + |
| 202 | + @CanIgnoreReturnValue |
| 203 | + public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> configure(CommonConfig config) { |
| 204 | + set(config::getKnownHttpRequestMethods, this::setKnownMethods); |
| 205 | + set(config::getServerRequestHeaders, this::setCapturedRequestHeaders); |
| 206 | + set(config::getServerResponseHeaders, this::setCapturedResponseHeaders); |
| 207 | + set( |
| 208 | + config::shouldEmitExperimentalHttpServerTelemetry, |
| 209 | + this::setEmitExperimentalHttpServerMetrics); |
| 210 | + return this; |
| 211 | + } |
| 212 | + |
| 213 | + private static <T> void set(Supplier<T> supplier, Consumer<T> consumer) { |
| 214 | + T t = supplier.get(); |
| 215 | + if (t != null) { |
| 216 | + consumer.accept(t); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments