-
Notifications
You must be signed in to change notification settings - Fork 67
feat: [Java] EMF Exporter Implementation #1209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bcf5eb2
f6ba80a
0b2c14c
cd1475d
5276407
3ec3aa5
7cd68df
e6a5283
8067773
b686dc1
af3b570
48e9453
0e3e77a
48ecda1
fcbd7a0
746e989
4236220
b7d0506
82e0b46
369501a
84dbe92
216841e
d1ccad6
a95a598
178085c
64e6202
8923525
05ad879
07a0a80
12a070a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,8 @@ | |
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import javax.annotation.concurrent.Immutable; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.AwsCloudWatchEmfExporter; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.ConsoleEmfExporter; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.otlp.aws.logs.OtlpAwsLogsExporterBuilder; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.otlp.aws.traces.OtlpAwsSpanExporterBuilder; | ||
|
|
||
|
|
@@ -86,6 +88,9 @@ | |
| @Immutable | ||
| public final class AwsApplicationSignalsCustomizerProvider | ||
| implements AutoConfigurationCustomizerProvider { | ||
| // https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-envvars.html | ||
| static final String AWS_REGION = "aws.region"; | ||
| static final String AWS_DEFAULT_REGION = "aws.default.region"; | ||
| static final String AWS_LAMBDA_FUNCTION_NAME_CONFIG = "AWS_LAMBDA_FUNCTION_NAME"; | ||
| static final String LAMBDA_APPLICATION_SIGNALS_REMOTE_ENVIRONMENT = | ||
| "LAMBDA_APPLICATION_SIGNALS_REMOTE_ENVIRONMENT"; | ||
|
|
@@ -103,6 +108,7 @@ public final class AwsApplicationSignalsCustomizerProvider | |
| // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-OTLPEndpoint.html#CloudWatch-LogsEndpoint | ||
| static final String AWS_OTLP_LOGS_GROUP_HEADER = "x-aws-log-group"; | ||
| static final String AWS_OTLP_LOGS_STREAM_HEADER = "x-aws-log-stream"; | ||
| static final String AWS_EMF_METRICS_NAMESPACE = "x-aws-metric-namespace"; | ||
|
|
||
| private static final String DEPRECATED_SMP_ENABLED_CONFIG = "otel.smp.enabled"; | ||
| private static final String DEPRECATED_APP_SIGNALS_ENABLED_CONFIG = | ||
|
|
@@ -132,7 +138,7 @@ public final class AwsApplicationSignalsCustomizerProvider | |
| private static final String OTEL_BSP_MAX_EXPORT_BATCH_SIZE_CONFIG = | ||
| "otel.bsp.max.export.batch.size"; | ||
|
|
||
| private static final String OTEL_METRICS_EXPORTER = "otel.metrics.exporter"; | ||
| static final String OTEL_METRICS_EXPORTER = "otel.metrics.exporter"; | ||
| static final String OTEL_LOGS_EXPORTER = "otel.logs.exporter"; | ||
| static final String OTEL_TRACES_EXPORTER = "otel.traces.exporter"; | ||
| static final String OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "otel.exporter.otlp.traces.protocol"; | ||
|
|
@@ -161,6 +167,7 @@ public final class AwsApplicationSignalsCustomizerProvider | |
| private static final int LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10; | ||
|
|
||
| private Sampler sampler; | ||
| private boolean isEmfExporterEnabled = false; | ||
|
|
||
| public void customize(AutoConfigurationCustomizer autoConfiguration) { | ||
| autoConfiguration.addPropertiesCustomizer(this::customizeProperties); | ||
|
|
@@ -171,6 +178,15 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { | |
| autoConfiguration.addMeterProviderCustomizer(this::customizeMeterProvider); | ||
| autoConfiguration.addSpanExporterCustomizer(this::customizeSpanExporter); | ||
| autoConfiguration.addLogRecordExporterCustomizer(this::customizeLogsExporter); | ||
| autoConfiguration.addMetricExporterCustomizer(this::customizeMetricExporter); | ||
| } | ||
|
|
||
| private static Optional<String> getAwsRegionFromConfig(ConfigProperties configProps) { | ||
| String region = configProps.getString(AWS_REGION); | ||
| if (region != null) { | ||
| return Optional.of(region); | ||
| } | ||
| return Optional.ofNullable(configProps.getString(AWS_DEFAULT_REGION)); | ||
| } | ||
|
|
||
| static boolean isLambdaEnvironment() { | ||
|
|
@@ -190,10 +206,18 @@ private boolean isApplicationSignalsRuntimeEnabled(ConfigProperties configProps) | |
| && configProps.getBoolean(APPLICATION_SIGNALS_RUNTIME_ENABLED_CONFIG, true); | ||
| } | ||
|
|
||
| private Map<String, String> customizeProperties(ConfigProperties configProps) { | ||
| Map<String, String> customizeProperties(ConfigProperties configProps) { | ||
| Map<String, String> propsOverride = new HashMap<>(); | ||
| boolean isLambdaEnvironment = isLambdaEnvironment(); | ||
|
|
||
| // Check if awsemf was specified and remove it from OTEL_METRICS_EXPORTER | ||
| Optional<String> filteredExporters = | ||
| AwsApplicationSignalsConfigUtils.removeEmfExporterIfEnabled(configProps); | ||
| if (filteredExporters.isPresent()) { | ||
| this.isEmfExporterEnabled = true; | ||
| propsOverride.put(OTEL_METRICS_EXPORTER, filteredExporters.get()); | ||
| } | ||
|
|
||
| // Enable AWS Resource Providers | ||
| propsOverride.put(OTEL_RESOURCE_PROVIDERS_AWS_ENABLED, "true"); | ||
|
|
||
|
|
@@ -394,7 +418,6 @@ private SdkTracerProviderBuilder customizeTracerProviderBuilder( | |
|
|
||
| private SdkMeterProviderBuilder customizeMeterProvider( | ||
| SdkMeterProviderBuilder sdkMeterProviderBuilder, ConfigProperties configProps) { | ||
|
|
||
| if (isApplicationSignalsRuntimeEnabled(configProps)) { | ||
| Set<String> registeredScopeNames = new HashSet<>(1); | ||
| String jmxRuntimeScopeName = "io.opentelemetry.jmx"; | ||
|
|
@@ -434,7 +457,7 @@ SpanExporter customizeSpanExporter(SpanExporter spanExporter, ConfigProperties c | |
| } | ||
| } | ||
|
|
||
| if (AwsApplicationSignalsConfigValidator.isSigV4EnabledTraces(configProps)) { | ||
| if (AwsApplicationSignalsConfigUtils.isSigV4EnabledTraces(configProps)) { | ||
| // can cast here since we've checked that the configuration for OTEL_TRACES_EXPORTER is otlp | ||
| // and OTEL_EXPORTER_OTLP_TRACES_PROTOCOL is http/protobuf | ||
| // so the given spanExporter will be an instance of OtlpHttpSpanExporter | ||
|
|
@@ -480,7 +503,7 @@ private boolean isOtlpSpanExporter(SpanExporter spanExporter) { | |
|
|
||
| LogRecordExporter customizeLogsExporter( | ||
| LogRecordExporter logsExporter, ConfigProperties configProps) { | ||
| if (AwsApplicationSignalsConfigValidator.isSigV4EnabledLogs(configProps)) { | ||
| if (AwsApplicationSignalsConfigUtils.isSigV4EnabledLogs(configProps)) { | ||
| // can cast here since we've checked that the configuration for OTEL_LOGS_EXPORTER is otlp and | ||
| // OTEL_EXPORTER_OTLP_LOGS_PROTOCOL is http/protobuf | ||
| // so the given logsExporter will be an instance of OtlpHttpLogRecorderExporter | ||
|
|
@@ -509,6 +532,45 @@ LogRecordExporter customizeLogsExporter( | |
| return logsExporter; | ||
| } | ||
|
|
||
| MetricExporter customizeMetricExporter( | ||
| MetricExporter metricExporter, ConfigProperties configProps) { | ||
| if (isEmfExporterEnabled) { | ||
| Map<String, String> headers = | ||
| AwsApplicationSignalsConfigUtils.parseOtlpHeaders( | ||
| configProps.getString(OTEL_EXPORTER_OTLP_LOGS_HEADERS)); | ||
| Optional<String> awsRegion = getAwsRegionFromConfig(configProps); | ||
|
|
||
| if (awsRegion.isPresent()) { | ||
| String namespace = headers.get(AWS_EMF_METRICS_NAMESPACE); | ||
|
|
||
| if (headers.containsKey(AWS_OTLP_LOGS_GROUP_HEADER) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does customer have to set all 3 headers?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like from Python implementation they don't need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed validation for namespace header. |
||
| && headers.containsKey(AWS_OTLP_LOGS_STREAM_HEADER)) { | ||
| String logGroup = headers.get(AWS_OTLP_LOGS_GROUP_HEADER); | ||
| String logStream = headers.get(AWS_OTLP_LOGS_STREAM_HEADER); | ||
| return new AwsCloudWatchEmfExporter(namespace, logGroup, logStream, awsRegion.get()); | ||
| } | ||
| if (isLambdaEnvironment()) { | ||
| return new ConsoleEmfExporter(namespace); | ||
| } | ||
| logger.warning( | ||
| String.format( | ||
| "Improper EMF Exporter configuration: Please configure the environment variable %s to have values for %s, %s, and %s", | ||
| OTEL_EXPORTER_OTLP_LOGS_HEADERS, | ||
| AWS_OTLP_LOGS_GROUP_HEADER, | ||
| AWS_OTLP_LOGS_STREAM_HEADER, | ||
| AWS_EMF_METRICS_NAMESPACE)); | ||
|
|
||
| } else { | ||
| logger.warning( | ||
| String.format( | ||
| "Improper EMF Exporter configuration: AWS region not found in environment variables please set %s or %s", | ||
| AWS_REGION, AWS_DEFAULT_REGION)); | ||
| } | ||
| } | ||
|
|
||
| return metricExporter; | ||
| } | ||
|
|
||
| static AwsXrayAdaptiveSamplingConfig parseConfigString(String config) | ||
| throws JsonProcessingException { | ||
| if (config == null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics; | ||
|
|
||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
| import java.util.logging.Logger; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.common.BaseEmfExporter; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.common.emitter.CloudWatchLogsClientEmitter; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.aws.metrics.common.emitter.LogEventEmitter; | ||
|
|
||
| /** | ||
| * EMF metrics exporter for sending data directly to CloudWatch Logs. | ||
| * | ||
| * <p>This exporter converts OTel metrics into CloudWatch EMF logs which are then sent to CloudWatch | ||
| * Logs. CloudWatch Logs automatically extracts the metrics from the EMF logs. | ||
| * | ||
| * <p><a | ||
| * href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html">...</a> | ||
| */ | ||
| public class AwsCloudWatchEmfExporter extends BaseEmfExporter<CloudWatchLogsClient> { | ||
| private static final Logger logger = Logger.getLogger(AwsCloudWatchEmfExporter.class.getName()); | ||
|
|
||
| /** | ||
| * Initialize the CloudWatch EMF exporter. | ||
| * | ||
| * @param namespace CloudWatch namespace for metrics (default: "default") | ||
| * @param logGroupName CloudWatch log group name | ||
| * @param logStreamName CloudWatch log stream name (auto-generated if null) | ||
| * @param awsRegion AWS region | ||
| */ | ||
| public AwsCloudWatchEmfExporter( | ||
| String namespace, String logGroupName, String logStreamName, String awsRegion) { | ||
| super(namespace, new CloudWatchLogsClientEmitter(logGroupName, logStreamName, awsRegion)); | ||
| } | ||
|
|
||
| /** | ||
| * Initialize the CloudWatch EMF exporter with a custom emitter. | ||
| * | ||
| * @param namespace CloudWatch namespace for metrics | ||
| * @param emitter Custom log emitter | ||
| */ | ||
| public AwsCloudWatchEmfExporter(String namespace, LogEventEmitter<CloudWatchLogsClient> emitter) { | ||
| super(namespace, emitter); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode flush() { | ||
| this.emitter.flushEvents(); | ||
| logger.fine("AwsCloudWatchEmfExporter force flushes the buffered metrics"); | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableResultCode shutdown() { | ||
| this.flush(); | ||
| logger.fine("AwsCloudWatchEmfExporter shutdown called"); | ||
| return CompletableResultCode.ofSuccess(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think about simplify the logic, improve readability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added some comments to clarify the method logic