-
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 16 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,7 @@ | |
| 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.otlp.aws.logs.OtlpAwsLogsExporterBuilder; | ||
| import software.amazon.opentelemetry.javaagent.providers.exporter.otlp.aws.traces.OtlpAwsSpanExporterBuilder; | ||
|
|
||
|
|
@@ -86,6 +87,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 +107,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 +137,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 +166,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 +177,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 +205,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 +417,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 +456,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 +502,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 +531,41 @@ 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()) { | ||
| 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) | ||
| && headers.containsKey(AWS_EMF_METRICS_NAMESPACE)) { | ||
| String namespace = headers.get(AWS_EMF_METRICS_NAMESPACE); | ||
| 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()); | ||
| } | ||
| logger.warning( | ||
| String.format( | ||
| "Improper configuration: Please configure the environment variable OTEL_EXPORTER_OTLP_LOGS_HEADERS to have values for %s, %s, and %s", | ||
| AWS_OTLP_LOGS_GROUP_HEADER, | ||
| AWS_OTLP_LOGS_STREAM_HEADER, | ||
| AWS_EMF_METRICS_NAMESPACE)); | ||
|
|
||
| } else { | ||
| logger.warning( | ||
| String.format( | ||
| "Improper 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) { | ||
|
|
||
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