|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.javaagent.providers; |
| 17 | + |
| 18 | +import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; |
| 19 | +import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil; |
| 20 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
| 21 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; |
| 22 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 23 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 24 | +import io.opentelemetry.sdk.metrics.Aggregation; |
| 25 | +import io.opentelemetry.sdk.metrics.InstrumentType; |
| 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 java.time.Duration; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.logging.Level; |
| 33 | +import java.util.logging.Logger; |
| 34 | + |
| 35 | +/** |
| 36 | + * You can control when these customizations are applied using both the properties - |
| 37 | + * otel.jmx.enabled and otel.aws.jmx.exporter.metrics.endpoint or the environment variable |
| 38 | + * OTEL_JMX_ENABLED_CONFIG and AWS_JMX_EXPORTER_ENDPOINT_CONFIG. These flags are disabled by |
| 39 | + * default. |
| 40 | + */ |
| 41 | +public class AwsJMXMetricsCustomizerProvider implements AutoConfigurationCustomizerProvider { |
| 42 | + private static final Duration DEFAULT_METRIC_EXPORT_INTERVAL = Duration.ofMinutes(1); |
| 43 | + private static final Logger logger = |
| 44 | + Logger.getLogger(AwsJMXMetricsCustomizerProvider.class.getName()); |
| 45 | + |
| 46 | + private static final String OTEL_JMX_ENABLED_CONFIG = "otel.jmx.enabled"; |
| 47 | + private static final String AWS_JMX_EXPORTER_ENDPOINT_CONFIG = |
| 48 | + "otel.aws.jmx.exporter.metrics.endpoint"; |
| 49 | + |
| 50 | + public void customize(AutoConfigurationCustomizer autoConfiguration) { |
| 51 | + autoConfiguration.addMeterProviderCustomizer(this::customizeMeterProvider); |
| 52 | + } |
| 53 | + |
| 54 | + private boolean isOtelJMXEnabled(ConfigProperties configProps) { |
| 55 | + return configProps.getBoolean(OTEL_JMX_ENABLED_CONFIG, true) |
| 56 | + && configProps.getString(AWS_JMX_EXPORTER_ENDPOINT_CONFIG, "") != ""; |
| 57 | + } |
| 58 | + |
| 59 | + private SdkMeterProviderBuilder customizeMeterProvider( |
| 60 | + SdkMeterProviderBuilder sdkMeterProviderBuilder, ConfigProperties configProps) { |
| 61 | + |
| 62 | + if (isOtelJMXEnabled(configProps)) { |
| 63 | + Set<String> registeredScopeNames = new HashSet<>(1); |
| 64 | + String jmxRuntimeScopeName = "io.opentelemetry.jmx"; |
| 65 | + registeredScopeNames.add(jmxRuntimeScopeName); |
| 66 | + |
| 67 | + SDKMeterProviderBuilder.configureMetricFilter( |
| 68 | + configProps, sdkMeterProviderBuilder, registeredScopeNames, logger); |
| 69 | + |
| 70 | + MetricExporter metricsExporter = JMXExporterProvider.INSTANCE.createExporter(configProps); |
| 71 | + MetricReader metricReader = |
| 72 | + ScopeBasedPeriodicMetricReader.create(metricsExporter, registeredScopeNames) |
| 73 | + .setInterval( |
| 74 | + SDKMeterProviderBuilder.getMetricExportInterval( |
| 75 | + configProps, DEFAULT_METRIC_EXPORT_INTERVAL, logger)) |
| 76 | + .build(); |
| 77 | + sdkMeterProviderBuilder.registerMetricReader(metricReader); |
| 78 | + |
| 79 | + logger.info("AWS JMX metric collection enabled"); |
| 80 | + } |
| 81 | + return sdkMeterProviderBuilder; |
| 82 | + } |
| 83 | + |
| 84 | + private enum JMXExporterProvider { |
| 85 | + INSTANCE; |
| 86 | + |
| 87 | + public MetricExporter createExporter(ConfigProperties configProps) { |
| 88 | + String protocol = |
| 89 | + OtlpConfigUtil.getOtlpProtocol(OtlpConfigUtil.DATA_TYPE_METRICS, configProps); |
| 90 | + logger.log(Level.FINE, String.format("AWS JMX metrics export protocol: %s", protocol)); |
| 91 | + |
| 92 | + String otelJMXEndpoint; |
| 93 | + if (protocol.equals(OtlpConfigUtil.PROTOCOL_HTTP_PROTOBUF)) { |
| 94 | + otelJMXEndpoint = configProps.getString(AWS_JMX_EXPORTER_ENDPOINT_CONFIG); |
| 95 | + logger.log( |
| 96 | + Level.FINE, String.format("AWS JMX metrics export endpoint: %s", otelJMXEndpoint)); |
| 97 | + return OtlpHttpMetricExporter.builder() |
| 98 | + .setEndpoint(otelJMXEndpoint) |
| 99 | + .setDefaultAggregationSelector(this::getAggregation) |
| 100 | + .build(); |
| 101 | + } |
| 102 | + throw new ConfigurationException("Unsupported AWS JMX metrics export protocol: " + protocol); |
| 103 | + } |
| 104 | + |
| 105 | + private Aggregation getAggregation(InstrumentType instrumentType) { |
| 106 | + if (instrumentType == InstrumentType.HISTOGRAM) { |
| 107 | + return Aggregation.base2ExponentialBucketHistogram(); |
| 108 | + } |
| 109 | + return Aggregation.defaultAggregation(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments