|
| 1 | +package com.sap.hcf.cf.logging.opentelemetry.agent.ext.exporter; |
| 2 | + |
| 3 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; |
| 4 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporterBuilder; |
| 5 | +import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter; |
| 6 | +import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder; |
| 7 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 8 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 9 | +import io.opentelemetry.sdk.autoconfigure.spi.metrics.ConfigurableMetricExporterProvider; |
| 10 | +import io.opentelemetry.sdk.common.export.RetryPolicy; |
| 11 | +import io.opentelemetry.sdk.metrics.Aggregation; |
| 12 | +import io.opentelemetry.sdk.metrics.InstrumentType; |
| 13 | +import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector; |
| 14 | +import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector; |
| 15 | +import io.opentelemetry.sdk.metrics.export.MetricExporter; |
| 16 | +import io.opentelemetry.sdk.metrics.internal.aggregator.AggregationUtil; |
| 17 | +import io.pivotal.cfenv.core.CfEnv; |
| 18 | +import io.pivotal.cfenv.core.CfService; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
| 25 | +import static io.opentelemetry.sdk.metrics.Aggregation.explicitBucketHistogram; |
| 26 | + |
| 27 | +public class DynatraceMetricsExporterProvider implements ConfigurableMetricExporterProvider { |
| 28 | + |
| 29 | + private static final Logger LOG = Logger.getLogger(DynatraceMetricsExporterProvider.class.getName()); |
| 30 | + public static final String CRED_DYNATRACE_APIURL = "apiurl"; |
| 31 | + public static final String DT_APIURL_METRICS_SUFFIX = "/v2/otlp/v1/metrics"; |
| 32 | + |
| 33 | + private final Function<ConfigProperties, CfService> serviceProvider; |
| 34 | + |
| 35 | + public DynatraceMetricsExporterProvider() { |
| 36 | + this(config -> new DynatraceServiceProvider(config, new CfEnv()).get()); |
| 37 | + } |
| 38 | + |
| 39 | + public DynatraceMetricsExporterProvider(Function<ConfigProperties, CfService> serviceProvider) { |
| 40 | + this.serviceProvider = serviceProvider; |
| 41 | + } |
| 42 | + |
| 43 | + private static String getCompression(ConfigProperties config) { |
| 44 | + String compression = config.getString("otel.exporter.dynatrace.metrics.compression"); |
| 45 | + return compression != null ? compression : config.getString("otel.exporter.dynatrace.compression", "gzip"); |
| 46 | + } |
| 47 | + |
| 48 | + private static Duration getTimeOut(ConfigProperties config) { |
| 49 | + Duration timeout = config.getDuration("otel.exporter.dynatrace.metrics.timeout"); |
| 50 | + return timeout != null ? timeout : config.getDuration("otel.exporter.dynatrace.timeout"); |
| 51 | + } |
| 52 | + |
| 53 | + private static DefaultAggregationSelector getDefaultAggregationSelector(ConfigProperties config) { |
| 54 | + String defaultHistogramAggregation = |
| 55 | + config.getString("otel.exporter.dynatrace.metrics.default.histogram.aggregation"); |
| 56 | + if (defaultHistogramAggregation == null) { |
| 57 | + return DefaultAggregationSelector.getDefault().with(InstrumentType.HISTOGRAM, Aggregation.defaultAggregation()); |
| 58 | + } |
| 59 | + if (AggregationUtil.aggregationName(Aggregation.base2ExponentialBucketHistogram()) |
| 60 | + .equalsIgnoreCase(defaultHistogramAggregation)) { |
| 61 | + return |
| 62 | + DefaultAggregationSelector.getDefault() |
| 63 | + .with(InstrumentType.HISTOGRAM, Aggregation.base2ExponentialBucketHistogram()); |
| 64 | + } else if (AggregationUtil.aggregationName(explicitBucketHistogram()) |
| 65 | + .equalsIgnoreCase(defaultHistogramAggregation)) { |
| 66 | + return DefaultAggregationSelector.getDefault().with(InstrumentType.HISTOGRAM, Aggregation.explicitBucketHistogram()); |
| 67 | + } else { |
| 68 | + throw new ConfigurationException( |
| 69 | + "Unrecognized default histogram aggregation: " + defaultHistogramAggregation); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getName() { |
| 75 | + return "dynatrace"; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + private static boolean isBlank(String text) { |
| 80 | + return text == null || text.trim().isEmpty(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public MetricExporter createExporter(ConfigProperties config) { |
| 85 | + CfService cfService = serviceProvider.apply(config); |
| 86 | + if (cfService == null) { |
| 87 | + LOG.info("No dynatrace service binding found. Skipping metrics exporter registration."); |
| 88 | + return NoopMetricExporter.getInstance(); |
| 89 | + } |
| 90 | + |
| 91 | + LOG.info("Creating metrics exporter for service binding " + cfService.getName() + " (" + cfService.getLabel() + ")"); |
| 92 | + |
| 93 | + String apiUrl = cfService.getCredentials().getString(CRED_DYNATRACE_APIURL); |
| 94 | + if (isBlank(apiUrl)) { |
| 95 | + LOG.warning("Credential \"" + CRED_DYNATRACE_APIURL + "\" not found. Skipping dynatrace exporter configuration"); |
| 96 | + return NoopMetricExporter.getInstance(); |
| 97 | + } |
| 98 | + String tokenName = config.getString("otel.javaagent.extension.sap.cf.binding.dynatrace.metrics.token-name"); |
| 99 | + if (isBlank(tokenName)) { |
| 100 | + LOG.warning("Configuration \"otel.javaagent.extension.sap.cf.binding.dynatrace.metrics.token-name\" not found. Skipping dynatrace exporter configuration"); |
| 101 | + return NoopMetricExporter.getInstance(); |
| 102 | + } |
| 103 | + String apiToken = cfService.getCredentials().getString(tokenName); |
| 104 | + if (isBlank(apiUrl)) { |
| 105 | + LOG.warning("Credential \"" + tokenName + "\" not found. Skipping dynatrace exporter configuration"); |
| 106 | + return NoopMetricExporter.getInstance(); |
| 107 | + } |
| 108 | + |
| 109 | + OtlpHttpMetricExporterBuilder builder = OtlpHttpMetricExporter.builder(); |
| 110 | + builder.setEndpoint(apiUrl + DT_APIURL_METRICS_SUFFIX) |
| 111 | + .setCompression(getCompression(config)) |
| 112 | + .addHeader("Authorization", "ApiToken " + apiToken) |
| 113 | + .setRetryPolicy(RetryPolicy.getDefault()) |
| 114 | + .setAggregationTemporalitySelector(AggregationTemporalitySelector.alwaysCumulative()) |
| 115 | + .setDefaultAggregationSelector(getDefaultAggregationSelector(config)); |
| 116 | + |
| 117 | + Duration timeOut = getTimeOut(config); |
| 118 | + if (timeOut != null) { |
| 119 | + builder.setTimeout(timeOut); |
| 120 | + } |
| 121 | + |
| 122 | + LOG.info("Created metrics exporter for service binding " + cfService.getName() + " (" + cfService.getLabel() + ")"); |
| 123 | + return builder.build(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments