|
| 1 | +/* |
| 2 | + * Licensed to ObjectStyle LLC under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ObjectStyle LLC licenses |
| 6 | + * this file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package io.bootique.otel.trace; |
| 20 | + |
| 21 | +import io.bootique.annotation.BQConfig; |
| 22 | +import io.bootique.annotation.BQConfigProperty; |
| 23 | +import io.bootique.shutdown.ShutdownManager; |
| 24 | +import io.opentelemetry.sdk.resources.Resource; |
| 25 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 26 | +import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder; |
| 27 | +import io.opentelemetry.sdk.trace.SpanProcessor; |
| 28 | +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; |
| 29 | +import io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder; |
| 30 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 31 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 32 | +import jakarta.inject.Inject; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +/** |
| 38 | + * @since 4.0 |
| 39 | + */ |
| 40 | +@BQConfig |
| 41 | +public class SdkTracerProviderFactory { |
| 42 | + |
| 43 | + private final Resource resource; |
| 44 | + private final ShutdownManager shutdownManager; |
| 45 | + |
| 46 | + private List<SpanExporterFactory> tracesExporters; |
| 47 | + |
| 48 | + @Inject |
| 49 | + public SdkTracerProviderFactory(Resource resource, ShutdownManager shutdownManager) { |
| 50 | + this.resource = resource; |
| 51 | + this.shutdownManager = shutdownManager; |
| 52 | + } |
| 53 | + |
| 54 | + @BQConfigProperty |
| 55 | + public SdkTracerProviderFactory setTracesExporters(List<SpanExporterFactory> tracesExporters) { |
| 56 | + this.tracesExporters = tracesExporters; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public SdkTracerProvider create() { |
| 61 | + |
| 62 | + SdkTracerProviderBuilder builder = SdkTracerProvider |
| 63 | + .builder() |
| 64 | + .setResource(resource); |
| 65 | + |
| 66 | + // TODO: span limits |
| 67 | + // TODO: meter provider |
| 68 | + // TODO: sampler |
| 69 | + |
| 70 | + createSpanProcessors().forEach(builder::addSpanProcessor); |
| 71 | + |
| 72 | + return shutdownManager.onShutdown(builder.build()); |
| 73 | + } |
| 74 | + |
| 75 | + private List<SpanProcessor> createSpanProcessors() { |
| 76 | + |
| 77 | + List<SpanProcessor> processors = new ArrayList<>(2); |
| 78 | + |
| 79 | + List<SpanExporterSupplier> batchedExporters = exporterSuppliers() |
| 80 | + .stream() |
| 81 | + |
| 82 | + // add a simple processor for console exporter, feed the rest into a single batch processor |
| 83 | + .peek(e -> { |
| 84 | + if (!e.shouldBatch()) { |
| 85 | + processors.add(createSimpleSpanProcessor(e)); |
| 86 | + } |
| 87 | + }) |
| 88 | + .filter(SpanExporterSupplier::shouldBatch) |
| 89 | + .toList(); |
| 90 | + |
| 91 | + if (!batchedExporters.isEmpty()) { |
| 92 | + processors.add(createBatchSpanProcessor(batchedExporters)); |
| 93 | + } |
| 94 | + |
| 95 | + return processors; |
| 96 | + } |
| 97 | + |
| 98 | + private List<SpanExporterSupplier> exporterSuppliers() { |
| 99 | + |
| 100 | + // unlike the agent whose default is "otlp", our default will be "console", so that the app could |
| 101 | + // work standalone out of the box. To suppress exporting, an explicit "none" exporter should be set |
| 102 | + |
| 103 | + // A single "none" exporter would suppress the default "console" exporter. Though unlike the agent, having a |
| 104 | + // "none" exporter mixed with others doesn't result in an exception. It will just be ignored |
| 105 | + |
| 106 | + List<SpanExporterFactory> tracesExporters = this.tracesExporters == null || this.tracesExporters.isEmpty() |
| 107 | + ? List.of(new ConsoleSpanExporterFactory()) |
| 108 | + : this.tracesExporters; |
| 109 | + |
| 110 | + return tracesExporters.stream() |
| 111 | + .map(SpanExporterFactory::create) |
| 112 | + .filter(s -> !s.isNone()) |
| 113 | + .toList(); |
| 114 | + } |
| 115 | + |
| 116 | + private SpanProcessor createSimpleSpanProcessor(SpanExporterSupplier exporterSupplier) { |
| 117 | + // TODO: .setMeterProvider(() -> meterProvider) |
| 118 | + |
| 119 | + // presumably we don't need to shut down the exporter, as SpanProcessor would do it for us |
| 120 | + SpanProcessor processor = SimpleSpanProcessor.builder(exporterSupplier.spanExporter().get()).build(); |
| 121 | + return shutdownManager.onShutdown(processor); |
| 122 | + } |
| 123 | + |
| 124 | + private BatchSpanProcessor createBatchSpanProcessor(List<SpanExporterSupplier> exporterSuppliers) { |
| 125 | + |
| 126 | + List<SpanExporter> exporters = exporterSuppliers.stream().map(s -> s.spanExporter().get()).toList(); |
| 127 | + SpanExporter composite = SpanExporter.composite(exporters); |
| 128 | + |
| 129 | + // as of OTel 1.59.0 unlike SimpleSpanProcessor, the batch processor will not shut down the exporters, so we |
| 130 | + // need to do it ourselves |
| 131 | + BatchSpanProcessorBuilder builder = BatchSpanProcessor.builder(shutdownManager.onShutdown(composite)); |
| 132 | + |
| 133 | + // TODO: |
| 134 | + // support .setMeterProvider(..) |
| 135 | + // support props for the builder: |
| 136 | + // otel.bsp.schedule.delay |
| 137 | + // otel.bsp.max.queue.size |
| 138 | + // otel.bsp.max.export.batch.size |
| 139 | + // otel.bsp.export.timeout |
| 140 | + |
| 141 | + return shutdownManager.onShutdown(builder.build()); |
| 142 | + } |
| 143 | +} |
0 commit comments