|
| 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.meter; |
| 20 | + |
| 21 | +import io.bootique.annotation.BQConfig; |
| 22 | +import io.bootique.annotation.BQConfigProperty; |
| 23 | +import io.bootique.shutdown.ShutdownManager; |
| 24 | +import io.bootique.value.Duration; |
| 25 | +import io.opentelemetry.sdk.metrics.SdkMeterProvider; |
| 26 | +import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; |
| 27 | +import io.opentelemetry.sdk.metrics.export.MetricExporter; |
| 28 | +import io.opentelemetry.sdk.metrics.export.MetricReader; |
| 29 | +import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; |
| 30 | +import io.opentelemetry.sdk.resources.Resource; |
| 31 | +import jakarta.inject.Inject; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +/** |
| 37 | + * @since 4.0 |
| 38 | + */ |
| 39 | +@BQConfig |
| 40 | +public class SdkMeterProviderFactory { |
| 41 | + |
| 42 | + private final Resource resource; |
| 43 | + private final ShutdownManager shutdownManager; |
| 44 | + |
| 45 | + private Duration metricExportInterval; |
| 46 | + private List<MetricExporterFactory> metricExporters; |
| 47 | + |
| 48 | + |
| 49 | + @Inject |
| 50 | + public SdkMeterProviderFactory(Resource resource, ShutdownManager shutdownManager) { |
| 51 | + this.resource = resource; |
| 52 | + this.shutdownManager = shutdownManager; |
| 53 | + } |
| 54 | + |
| 55 | + @BQConfigProperty("Time interval between the start of two export attempts. The default is '1min'") |
| 56 | + public SdkMeterProviderFactory setMetricExportInterval(Duration metricExportInterval) { |
| 57 | + this.metricExportInterval = metricExportInterval; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + @BQConfigProperty |
| 62 | + public SdkMeterProviderFactory setMetricExporters(List<MetricExporterFactory> metricExporters) { |
| 63 | + this.metricExporters = metricExporters; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public SdkMeterProvider create() { |
| 68 | + SdkMeterProviderBuilder builder = SdkMeterProvider |
| 69 | + .builder() |
| 70 | + .setResource(resource); |
| 71 | + |
| 72 | + // TODO: |
| 73 | + // views |
| 74 | + // clock |
| 75 | + // exemplar filter |
| 76 | + |
| 77 | + createMetricReaders().forEach(builder::registerMetricReader); |
| 78 | + return shutdownManager.onShutdown(builder.build()); |
| 79 | + } |
| 80 | + |
| 81 | + private List<MetricReader> createMetricReaders() { |
| 82 | + return createMetricExporters().stream().map(this::createMetricReader).toList(); |
| 83 | + } |
| 84 | + |
| 85 | + private List<MetricExporter> createMetricExporters() { |
| 86 | + |
| 87 | + // unlike the agent whose default is "otlp", our default will be "console", so that the app could |
| 88 | + // work standalone out of the box. To suppress exporting, an explicit "none" exporter should be set |
| 89 | + |
| 90 | + // A single "none" exporter would suppress the default "console" exporter. Though unlike the agent, having a |
| 91 | + // "none" exporter mixed with others doesn't result in an exception. It will just be ignored |
| 92 | + |
| 93 | + List<MetricExporterFactory> exporters = this.metricExporters == null || this.metricExporters.isEmpty() |
| 94 | + ? List.of(new ConsoleMetricExporterFactory()) |
| 95 | + : this.metricExporters; |
| 96 | + |
| 97 | + return exporters.stream() |
| 98 | + .map(MetricExporterFactory::create) |
| 99 | + .filter(Objects::nonNull) |
| 100 | + .toList(); |
| 101 | + } |
| 102 | + |
| 103 | + private MetricReader createMetricReader(MetricExporter exporter) { |
| 104 | + return PeriodicMetricReader.builder(exporter) |
| 105 | + .setInterval(getMetricExportIntervalOrDefault()) |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + private java.time.Duration getMetricExportIntervalOrDefault() { |
| 110 | + return this.metricExportInterval != null |
| 111 | + ? this.metricExportInterval.getDuration() |
| 112 | + : java.time.Duration.ofMinutes(1); |
| 113 | + } |
| 114 | +} |
0 commit comments