|
| 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.semconv.http.HttpExperimentalAttributesExtractor; |
| 12 | +import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics; |
| 13 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; |
| 14 | +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; |
| 15 | +import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; |
| 16 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; |
| 17 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; |
| 18 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor; |
| 19 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder; |
| 20 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter; |
| 21 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics; |
| 22 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; |
| 23 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteBuilder; |
| 24 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor; |
| 25 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractorBuilder; |
| 26 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.function.Consumer; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class is internal and is hence not for public use. Its APIs are unstable and can change at |
| 36 | + * any time. |
| 37 | + */ |
| 38 | +public final class DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> { |
| 39 | + |
| 40 | + private final String instrumentationName; |
| 41 | + private final OpenTelemetry openTelemetry; |
| 42 | + |
| 43 | + private final List<AttributesExtractor<REQUEST, RESPONSE>> additionalExtractors = |
| 44 | + new ArrayList<>(); |
| 45 | + private final HttpServerAttributesExtractorBuilder<REQUEST, RESPONSE> |
| 46 | + httpAttributesExtractorBuilder; |
| 47 | + private final HttpSpanNameExtractorBuilder<REQUEST> httpSpanNameExtractorBuilder; |
| 48 | + private Function<SpanNameExtractor<REQUEST>, ? extends SpanNameExtractor<? super REQUEST>> |
| 49 | + spanNameExtractorTransformer = Function.identity(); |
| 50 | + private final HttpServerRouteBuilder<REQUEST> httpServerRouteBuilder; |
| 51 | + private final HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter; |
| 52 | + private final Optional<TextMapGetter<REQUEST>> headerSetter; |
| 53 | + private boolean emitExperimentalHttpServerMetrics = false; |
| 54 | + |
| 55 | + public DefaultHttpServerTelemetryBuilder( |
| 56 | + String instrumentationName, OpenTelemetry openTelemetry, HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter, |
| 57 | + Optional<TextMapGetter<REQUEST>> headerSetter) { |
| 58 | + this.instrumentationName = instrumentationName; |
| 59 | + this.openTelemetry = openTelemetry; |
| 60 | + httpAttributesExtractorBuilder = HttpServerAttributesExtractor.builder(attributesGetter); |
| 61 | + httpSpanNameExtractorBuilder = HttpSpanNameExtractor.builder(attributesGetter); |
| 62 | + httpServerRouteBuilder = HttpServerRoute.builder(attributesGetter); |
| 63 | + this.attributesGetter = attributesGetter; |
| 64 | + this.headerSetter = headerSetter; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented |
| 69 | + * items. |
| 70 | + */ |
| 71 | + @CanIgnoreReturnValue |
| 72 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> addAttributesExtractor( |
| 73 | + AttributesExtractor<REQUEST, RESPONSE> attributesExtractor) { |
| 74 | + additionalExtractors.add(attributesExtractor); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Configures the HTTP request headers that will be captured as span attributes. |
| 80 | + * |
| 81 | + * @param requestHeaders A list of HTTP header names. |
| 82 | + */ |
| 83 | + @CanIgnoreReturnValue |
| 84 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> setCapturedRequestHeaders(List<String> requestHeaders) { |
| 85 | + httpAttributesExtractorBuilder.setCapturedRequestHeaders(requestHeaders); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Configures the HTTP response headers that will be captured as span attributes. |
| 91 | + * |
| 92 | + * @param responseHeaders A list of HTTP header names. |
| 93 | + */ |
| 94 | + @CanIgnoreReturnValue |
| 95 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> setCapturedResponseHeaders(List<String> responseHeaders) { |
| 96 | + httpAttributesExtractorBuilder.setCapturedResponseHeaders(responseHeaders); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Configures the instrumentation to recognize an alternative set of HTTP request methods. |
| 102 | + * |
| 103 | + * <p>By default, this instrumentation defines "known" methods as the ones listed in <a |
| 104 | + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH |
| 105 | + * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>. |
| 106 | + * |
| 107 | + * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does |
| 108 | + * not supplement it. |
| 109 | + * |
| 110 | + * @param knownMethods A set of recognized HTTP request methods. |
| 111 | + * @see HttpServerAttributesExtractorBuilder#setKnownMethods(Set) |
| 112 | + */ |
| 113 | + @CanIgnoreReturnValue |
| 114 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> setKnownMethods(Set<String> knownMethods) { |
| 115 | + httpAttributesExtractorBuilder.setKnownMethods(knownMethods); |
| 116 | + httpSpanNameExtractorBuilder.setKnownMethods(knownMethods); |
| 117 | + httpServerRouteBuilder.setKnownMethods(knownMethods); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Configures the instrumentation to emit experimental HTTP server metrics. |
| 123 | + * |
| 124 | + * @param emitExperimentalHttpServerMetrics {@code true} if the experimental HTTP server metrics |
| 125 | + * are to be emitted. |
| 126 | + */ |
| 127 | + @CanIgnoreReturnValue |
| 128 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> setEmitExperimentalHttpServerMetrics( |
| 129 | + boolean emitExperimentalHttpServerMetrics) { |
| 130 | + this.emitExperimentalHttpServerMetrics = emitExperimentalHttpServerMetrics; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** Sets custom {@link SpanNameExtractor} via transform function. */ |
| 135 | + @CanIgnoreReturnValue |
| 136 | + public DefaultHttpServerTelemetryBuilder<REQUEST, RESPONSE> setSpanNameExtractor( |
| 137 | + Function<SpanNameExtractor<REQUEST>, ? extends SpanNameExtractor<? super REQUEST>> |
| 138 | + spanNameExtractorTransformer) { |
| 139 | + this.spanNameExtractorTransformer = spanNameExtractorTransformer; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public Instrumenter<REQUEST, RESPONSE> instrumenter() { |
| 144 | + return instrumenter(b -> {}); |
| 145 | + } |
| 146 | + |
| 147 | + public Instrumenter<REQUEST, RESPONSE> instrumenter( |
| 148 | + Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> instrumenterBuilderConsumer) { |
| 149 | + |
| 150 | + SpanNameExtractor<? super REQUEST> spanNameExtractor = |
| 151 | + spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build()); |
| 152 | + |
| 153 | + InstrumenterBuilder<REQUEST, RESPONSE> builder = |
| 154 | + Instrumenter.<REQUEST, RESPONSE>builder( |
| 155 | + openTelemetry, instrumentationName, spanNameExtractor) |
| 156 | + .setSpanStatusExtractor(HttpSpanStatusExtractor.create(attributesGetter)) |
| 157 | + .addAttributesExtractor(httpAttributesExtractorBuilder.build()) |
| 158 | + .addAttributesExtractors(additionalExtractors) |
| 159 | + .addContextCustomizer(httpServerRouteBuilder.build()) |
| 160 | + .addOperationMetrics(HttpServerMetrics.get()); |
| 161 | + if (emitExperimentalHttpServerMetrics) { |
| 162 | + builder |
| 163 | + .addAttributesExtractor(HttpExperimentalAttributesExtractor.create(attributesGetter)) |
| 164 | + .addOperationMetrics(HttpServerExperimentalMetrics.get()); |
| 165 | + } |
| 166 | + |
| 167 | + instrumenterBuilderConsumer.accept(builder); |
| 168 | + |
| 169 | + if (headerSetter.isPresent()) { |
| 170 | + return builder.buildServerInstrumenter(headerSetter.get()); |
| 171 | + } |
| 172 | + return builder.buildInstrumenter(SpanKindExtractor.alwaysServer()); |
| 173 | + } |
| 174 | + |
| 175 | + public OpenTelemetry getOpenTelemetry() { |
| 176 | + return openTelemetry; |
| 177 | + } |
| 178 | +} |
0 commit comments