From a233a38f66ffc9cf8f582e81cd2fde6fccdb87e7 Mon Sep 17 00:00:00 2001 From: SylvainJuge <763082+SylvainJuge@users.noreply.github.com> Date: Thu, 16 May 2024 09:58:26 +0200 Subject: [PATCH] simplify cloud resources (#260) --- ...icAutoConfigurationCustomizerprovider.java | 27 ----- .../co/elastic/otel/ElasticExtension.java | 39 ------ .../elastic/otel/ElasticMetricExporter.java | 16 +-- .../co/elastic/otel/ElasticSpanExporter.java | 6 - .../META-INF/LICENSE.txt | 41 +++++++ licenses/more-licences.md | 41 +++---- resources/build.gradle.kts | 10 +- .../resources/ElasticResourceProvider.java | 112 ------------------ .../resources/ResourcesAutoConfiguration.java | 57 +++++++++ .../ResourcesAutoConfigurationTest.java | 76 ++++++++++++ 10 files changed, 199 insertions(+), 226 deletions(-) create mode 100644 licenses/HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt delete mode 100644 resources/src/main/java/co/elastic/otel/resources/ElasticResourceProvider.java create mode 100644 resources/src/main/java/co/elastic/otel/resources/ResourcesAutoConfiguration.java create mode 100644 resources/src/test/java/co/elastic/otel/resources/ResourcesAutoConfigurationTest.java diff --git a/custom/src/main/java/co/elastic/otel/ElasticAutoConfigurationCustomizerprovider.java b/custom/src/main/java/co/elastic/otel/ElasticAutoConfigurationCustomizerprovider.java index 147d2c2d..c6ca6977 100644 --- a/custom/src/main/java/co/elastic/otel/ElasticAutoConfigurationCustomizerprovider.java +++ b/custom/src/main/java/co/elastic/otel/ElasticAutoConfigurationCustomizerprovider.java @@ -18,7 +18,6 @@ */ package co.elastic.otel; -import co.elastic.otel.resources.ElasticResourceProvider; import com.google.auto.service.AutoService; import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; @@ -37,16 +36,6 @@ public class ElasticAutoConfigurationCustomizerprovider public void customize(AutoConfigurationCustomizer autoConfiguration) { autoConfiguration - .addResourceCustomizer( - (resource, configProperties) -> { - // create the resource provider ourselves we can store a reference to it - // and that will only get "fast" resources attributes when invoked - ElasticResourceProvider resourceProvider = new ElasticResourceProvider(); - - ElasticExtension.INSTANCE.registerResourceProvider( - resourceProvider, configProperties); - return resource.merge(resourceProvider.createResource(configProperties)); - }) .addTracerProviderCustomizer( (sdkTracerProviderBuilder, configProperties) -> // span processor registration @@ -59,20 +48,6 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { // disabling embedded resource providers - // App server, loaded through SPI by default - disabledConfig.add( - "io.opentelemetry.contrib.resourceproviders.AppServerServiceNameProvider"); - - // GCP, SPI loading disabled but ensures disabled even if configured otherwise - disabledConfig.add("io.opentelemetry.contrib.gcp.resource.GCPResourceProvider"); - - // AWS, SPI loading disabled but ensures disabled even if configured otherwise - disabledConfig.add("io.opentelemetry.contrib.aws.resource.BeanstalkResourceProvider"); - disabledConfig.add("io.opentelemetry.contrib.aws.resource.Ec2ResourceProvider"); - disabledConfig.add("io.opentelemetry.contrib.aws.resource.EcsResourceProvider"); - disabledConfig.add("io.opentelemetry.contrib.aws.resource.EksResourceProvider"); - disabledConfig.add("io.opentelemetry.contrib.aws.resource.LambdaResourceProvider"); - // disable upstream distro name & version provider disabledConfig.add( "io.opentelemetry.javaagent.tooling.DistroVersionResourceProvider"); @@ -80,8 +55,6 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { Map config = new HashMap<>(); config.put(DISABLED_RESOURCE_PROVIDERS, String.join(",", disabledConfig)); - // disable loading expensive resource providers when invoked - config.put(ElasticResourceProvider.SKIP_EXPENSIVE_RESOURCE_PROVIDERS, "true"); return config; }) .addSpanExporterCustomizer( diff --git a/custom/src/main/java/co/elastic/otel/ElasticExtension.java b/custom/src/main/java/co/elastic/otel/ElasticExtension.java index 22951a27..b4aa7454 100644 --- a/custom/src/main/java/co/elastic/otel/ElasticExtension.java +++ b/custom/src/main/java/co/elastic/otel/ElasticExtension.java @@ -19,36 +19,19 @@ package co.elastic.otel; import co.elastic.otel.common.util.ExecutorUtils; -import co.elastic.otel.resources.ElasticResourceProvider; import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.context.internal.shaded.WeakConcurrentMap; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.export.SpanExporter; -import java.util.Objects; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.logging.Level; -import java.util.logging.Logger; public class ElasticExtension { - private static final Logger logger = Logger.getLogger(ElasticExtension.class.getName()); - public static final ElasticExtension INSTANCE = new ElasticExtension(); private final ElasticBreakdownMetrics breakdownMetrics; private final ElasticSpanProcessor spanProcessor; private final ExecutorService asyncInitExecutor; private ElasticSpanExporter spanExporter; - private WeakConcurrentMap cachedResources = - new WeakConcurrentMap.WithInlinedExpunction<>(); - private Resource extraResource; - private Future resourceFuture; private ElasticExtension() { this.breakdownMetrics = new ElasticBreakdownMetrics(); @@ -73,28 +56,6 @@ public SpanExporter wrapSpanExporter(SpanExporter toWrap) { return spanExporter; } - public void registerResourceProvider( - ElasticResourceProvider resourceProvider, ConfigProperties config) { - this.resourceFuture = asyncInitExecutor.submit(() -> resourceProvider.getExtraResource(config)); - } - - public Resource wrapResource(Resource resource) { - // because original resources are immutable - Resource result = cachedResources.get(resource); - if (result != null) { - return result; - } - Objects.requireNonNull(resourceFuture); - try { - extraResource = resourceFuture.get(5, TimeUnit.SECONDS); - result = resource.merge(extraResource); - cachedResources.put(resource, result); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - logger.log(Level.WARNING, "unable capture resource attributes", e); - } - return result; - } - public void shutdown() { ExecutorUtils.shutdownAndWaitTermination(asyncInitExecutor); } diff --git a/custom/src/main/java/co/elastic/otel/ElasticMetricExporter.java b/custom/src/main/java/co/elastic/otel/ElasticMetricExporter.java index a627e18d..d556c889 100644 --- a/custom/src/main/java/co/elastic/otel/ElasticMetricExporter.java +++ b/custom/src/main/java/co/elastic/otel/ElasticMetricExporter.java @@ -18,17 +18,13 @@ */ package co.elastic.otel; -import co.elastic.otel.metrics.DelegatingMetricData; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.metrics.Aggregation; import io.opentelemetry.sdk.metrics.InstrumentType; import io.opentelemetry.sdk.metrics.data.AggregationTemporality; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.export.MetricExporter; -import io.opentelemetry.sdk.resources.Resource; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; public class ElasticMetricExporter implements MetricExporter { @@ -50,17 +46,7 @@ public Aggregation getDefaultAggregation(InstrumentType instrumentType) { @Override public CompletableResultCode export(Collection metrics) { - List toSend = new ArrayList<>(metrics.size()); - for (MetricData metric : metrics) { - toSend.add( - new DelegatingMetricData(metric) { - @Override - public Resource getResource() { - return ElasticExtension.INSTANCE.wrapResource(super.getResource()); - } - }); - } - return delegate.export(toSend); + return delegate.export(metrics); } @Override diff --git a/custom/src/main/java/co/elastic/otel/ElasticSpanExporter.java b/custom/src/main/java/co/elastic/otel/ElasticSpanExporter.java index 0f200786..ee48fcf9 100644 --- a/custom/src/main/java/co/elastic/otel/ElasticSpanExporter.java +++ b/custom/src/main/java/co/elastic/otel/ElasticSpanExporter.java @@ -23,7 +23,6 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.data.DelegatingSpanData; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.SpanExporter; @@ -66,11 +65,6 @@ public CompletableResultCode export(Collection spans) { public Attributes getAttributes() { return newAttributes; } - - @Override - public Resource getResource() { - return ElasticExtension.INSTANCE.wrapResource(span.getResource()); - } }); } } diff --git a/licenses/HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt b/licenses/HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt new file mode 100644 index 00000000..401ccfb0 --- /dev/null +++ b/licenses/HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt @@ -0,0 +1,41 @@ +The code in this repository code was Written by Gil Tene, Michael Barker, +and Matt Warren, and released to the public domain, as explained at +http://creativecommons.org/publicdomain/zero/1.0/ + +For users of this code who wish to consume it under the "BSD" license +rather than under the public domain or CC0 contribution text mentioned +above, the code found under this directory is *also* provided under the +following license (commonly referred to as the BSD 2-Clause License). This +license does not detract from the above stated release of the code into +the public domain, and simply represents an additional license granted by +the Author. + +----------------------------------------------------------------------------- +** Beginning of "BSD 2-Clause License" text. ** + + Copyright (c) 2012, 2013, 2014, 2015, 2016 Gil Tene + Copyright (c) 2014 Michael Barker + Copyright (c) 2014 Matt Warren + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + THE POSSIBILITY OF SUCH DAMAGE. diff --git a/licenses/more-licences.md b/licenses/more-licences.md index f5a71bc3..dc0b1602 100644 --- a/licenses/more-licences.md +++ b/licenses/more-licences.md @@ -58,99 +58,94 @@ > - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java](https://github.com/open-telemetry/opentelemetry-java) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) -**14** **Group:** `io.opentelemetry.contrib` **Name:** `opentelemetry-aws-resources` **Version:** `1.34.0-alpha` -> - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java-contrib](https://github.com/open-telemetry/opentelemetry-java-contrib) -> - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) - -**15** **Group:** `io.opentelemetry.contrib` **Name:** `opentelemetry-gcp-resources` **Version:** `1.34.0-alpha` -> - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java-contrib](https://github.com/open-telemetry/opentelemetry-java-contrib) -> - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) - -**16** **Group:** `io.opentelemetry.contrib` **Name:** `opentelemetry-resource-providers` **Version:** `1.34.0-alpha` +**14** **Group:** `io.opentelemetry.contrib` **Name:** `opentelemetry-resource-providers` **Version:** `1.34.0-alpha` > - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java-contrib](https://github.com/open-telemetry/opentelemetry-java-contrib) > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) -**17** **Group:** `io.opentelemetry.javaagent` **Name:** `opentelemetry-javaagent-extension-api` **Version:** `2.3.0-alpha` +**15** **Group:** `io.opentelemetry.javaagent` **Name:** `opentelemetry-javaagent-extension-api` **Version:** `2.3.0-alpha` > - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java-instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) -**18** **Group:** `io.opentelemetry.javaagent` **Name:** `opentelemetry-javaagent-tooling` **Version:** `2.3.0-alpha` +**16** **Group:** `io.opentelemetry.javaagent` **Name:** `opentelemetry-javaagent-tooling` **Version:** `2.3.0-alpha` > - **POM Project URL**: [https://github.com/open-telemetry/opentelemetry-java-instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) -**19** **Group:** `io.opentelemetry.semconv` **Name:** `opentelemetry-semconv` **Version:** `1.25.0-alpha` +**17** **Group:** `io.opentelemetry.semconv` **Name:** `opentelemetry-semconv` **Version:** `1.25.0-alpha` > - **POM Project URL**: [https://github.com/open-telemetry/semantic-conventions-java](https://github.com/open-telemetry/semantic-conventions-java) > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) -**20** **Group:** `io.opentelemetry.semconv` **Name:** `opentelemetry-semconv-incubating` **Version:** `1.25.0-alpha` +**18** **Group:** `io.opentelemetry.semconv` **Name:** `opentelemetry-semconv-incubating` **Version:** `1.25.0-alpha` > - **POM Project URL**: [https://github.com/open-telemetry/semantic-conventions-java](https://github.com/open-telemetry/semantic-conventions-java) > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) -**21** **Group:** `net.bytebuddy` **Name:** `byte-buddy-dep` **Version:** `1.14.13` +**19** **Group:** `net.bytebuddy` **Name:** `byte-buddy-dep` **Version:** `1.14.13` > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) > - **Embedded license files**: [byte-buddy-dep-1.14.13.jar/META-INF/LICENSE](byte-buddy-dep-1.14.13.jar/META-INF/LICENSE) - [byte-buddy-dep-1.14.13.jar/META-INF/NOTICE](byte-buddy-dep-1.14.13.jar/META-INF/NOTICE) -**22** **Group:** `org.jctools` **Name:** `jctools-core` **Version:** `4.0.3` +**20** **Group:** `org.jctools` **Name:** `jctools-core` **Version:** `4.0.3` > - **Manifest License**: Apache License, Version 2.0 (Not Packaged) > - **POM Project URL**: [https://github.com/JCTools](https://github.com/JCTools) > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) -**23** **Group:** `org.ow2.asm` **Name:** `asm` **Version:** `9.6` +**21** **Group:** `org.ow2.asm` **Name:** `asm` **Version:** `9.6` > - **Manifest Project URL**: [http://asm.ow2.org](http://asm.ow2.org) > - **Manifest License**: The 3-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://asm.ow2.io/](http://asm.ow2.io/) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) > - **POM License**: The 3-Clause BSD License - [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause) -**24** **Group:** `org.ow2.asm` **Name:** `asm-commons` **Version:** `9.6` +**22** **Group:** `org.ow2.asm` **Name:** `asm-commons` **Version:** `9.6` > - **Manifest Project URL**: [http://asm.ow2.org](http://asm.ow2.org) > - **Manifest License**: The 3-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://asm.ow2.io/](http://asm.ow2.io/) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) > - **POM License**: The 3-Clause BSD License - [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause) -**25** **Group:** `tools.profiler` **Name:** `async-profiler` **Version:** `3.0` +**23** **Group:** `tools.profiler` **Name:** `async-profiler` **Version:** `3.0` > - **POM Project URL**: [https://profiler.tools](https://profiler.tools) > - **POM License**: Apache License, Version 2.0 - [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) ## Creative Commons Legal Code -**26** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.1.12` +**24** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.2.1` > - **Manifest License**: The 2-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://hdrhistogram.github.io/HdrHistogram/](http://hdrhistogram.github.io/HdrHistogram/) > - **POM License**: Creative Commons Legal Code - [https://creativecommons.org/publicdomain/zero/1.0/legalcode](https://creativecommons.org/publicdomain/zero/1.0/legalcode) > - **POM License**: PUBLIC DOMAIN - [http://creativecommons.org/publicdomain/zero/1.0/](http://creativecommons.org/publicdomain/zero/1.0/) > - **POM License**: The 2-Clause BSD License - [https://opensource.org/licenses/BSD-2-Clause](https://opensource.org/licenses/BSD-2-Clause) +> - **Embedded license files**: [HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt](HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt) ## PUBLIC DOMAIN -**27** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.1.12` +**25** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.2.1` > - **Manifest License**: The 2-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://hdrhistogram.github.io/HdrHistogram/](http://hdrhistogram.github.io/HdrHistogram/) > - **POM License**: Creative Commons Legal Code - [https://creativecommons.org/publicdomain/zero/1.0/legalcode](https://creativecommons.org/publicdomain/zero/1.0/legalcode) > - **POM License**: PUBLIC DOMAIN - [http://creativecommons.org/publicdomain/zero/1.0/](http://creativecommons.org/publicdomain/zero/1.0/) > - **POM License**: The 2-Clause BSD License - [https://opensource.org/licenses/BSD-2-Clause](https://opensource.org/licenses/BSD-2-Clause) +> - **Embedded license files**: [HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt](HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt) ## The 2-Clause BSD License -**28** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.1.12` +**26** **Group:** `org.hdrhistogram` **Name:** `HdrHistogram` **Version:** `2.2.1` > - **Manifest License**: The 2-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://hdrhistogram.github.io/HdrHistogram/](http://hdrhistogram.github.io/HdrHistogram/) > - **POM License**: Creative Commons Legal Code - [https://creativecommons.org/publicdomain/zero/1.0/legalcode](https://creativecommons.org/publicdomain/zero/1.0/legalcode) > - **POM License**: PUBLIC DOMAIN - [http://creativecommons.org/publicdomain/zero/1.0/](http://creativecommons.org/publicdomain/zero/1.0/) > - **POM License**: The 2-Clause BSD License - [https://opensource.org/licenses/BSD-2-Clause](https://opensource.org/licenses/BSD-2-Clause) +> - **Embedded license files**: [HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt](HdrHistogram-2.2.1.jar/META-INF/LICENSE.txt) ## The 3-Clause BSD License -**29** **Group:** `org.ow2.asm` **Name:** `asm` **Version:** `9.6` +**27** **Group:** `org.ow2.asm` **Name:** `asm` **Version:** `9.6` > - **Manifest Project URL**: [http://asm.ow2.org](http://asm.ow2.org) > - **Manifest License**: The 3-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://asm.ow2.io/](http://asm.ow2.io/) > - **POM License**: Apache License, Version 2.0 - [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) > - **POM License**: The 3-Clause BSD License - [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause) -**30** **Group:** `org.ow2.asm` **Name:** `asm-commons` **Version:** `9.6` +**28** **Group:** `org.ow2.asm` **Name:** `asm-commons` **Version:** `9.6` > - **Manifest Project URL**: [http://asm.ow2.org](http://asm.ow2.org) > - **Manifest License**: The 3-Clause BSD License (Not Packaged) > - **POM Project URL**: [http://asm.ow2.io/](http://asm.ow2.io/) diff --git a/resources/build.gradle.kts b/resources/build.gradle.kts index cfbe1069..2bba8d2b 100644 --- a/resources/build.gradle.kts +++ b/resources/build.gradle.kts @@ -1,17 +1,19 @@ plugins { java - id("com.github.johnrengelman.shadow") alias(catalog.plugins.taskinfo) } dependencies { compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") + testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") - // those are already included in the upstream agent - compileOnly(catalog.gcpContribResources) - compileOnly(catalog.awsContribResources) + // auto-service + annotationProcessor(libs.autoservice.processor) + compileOnly(libs.autoservice.annotations) // not included in the upstream agent implementation(catalog.contribResources) + + testImplementation(libs.assertj.core) } diff --git a/resources/src/main/java/co/elastic/otel/resources/ElasticResourceProvider.java b/resources/src/main/java/co/elastic/otel/resources/ElasticResourceProvider.java deleted file mode 100644 index 607c433c..00000000 --- a/resources/src/main/java/co/elastic/otel/resources/ElasticResourceProvider.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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 co.elastic.otel.resources; - -import io.opentelemetry.contrib.aws.resource.BeanstalkResourceProvider; -import io.opentelemetry.contrib.aws.resource.Ec2ResourceProvider; -import io.opentelemetry.contrib.aws.resource.EcsResourceProvider; -import io.opentelemetry.contrib.aws.resource.EksResourceProvider; -import io.opentelemetry.contrib.aws.resource.LambdaResourceProvider; -import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider; -import io.opentelemetry.contrib.resourceproviders.AppServerServiceNameProvider; -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; -import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; -import io.opentelemetry.sdk.resources.Resource; -import java.util.Arrays; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Provider for resource attributes, implements {@link ResourceProvider} but is not invoked through - * SPI interface but explicitly. - */ -public class ElasticResourceProvider implements ResourceProvider { - - private static final Logger logger = Logger.getLogger(ElasticResourceProvider.class.getName()); - - public static String SKIP_EXPENSIVE_RESOURCE_PROVIDERS = - "elastic.internal.skip_expensive_resource_providers"; - - public ElasticResourceProvider() {} - - @Override - public Resource createResource(ConfigProperties config) { - if (config.getBoolean(SKIP_EXPENSIVE_RESOURCE_PROVIDERS, false)) { - return getBaseResource(config); - } - return getBaseResource(config).merge(getExtraResource(config)); - } - - private Resource getBaseResource(ConfigProperties config) { - // application server providers : file parsing only thus fast - return invokeResourceProvider(new AppServerServiceNameProvider(), config); - } - - /** - * @return extra resource attributes that are expected to take some time due to making requests to - * external systems - */ - public Resource getExtraResource(ConfigProperties config) { - List providers = - Arrays.asList( - // -- AWS -- - // ec2 relies on http calls without pre-checks - new Ec2ResourceProvider(), - // beanstalk relies on json config file parsing - new BeanstalkResourceProvider(), - // relies on https call without pre-checks + TLS setup (thus quite expensive) - new EksResourceProvider(), - // relies on http call with url provided through env var used as pre-check - new EcsResourceProvider(), - // relies on env variables only - new LambdaResourceProvider(), - // -- GCP -- - new GCPResourceProvider()); - Resource resource = Resource.empty(); - for (ResourceProvider provider : providers) { - resource = resource.merge(invokeResourceProvider(provider, config)); - } - return resource; - } - - private static Resource invokeResourceProvider( - ResourceProvider provider, ConfigProperties config) { - - try { - logger.log( - Level.FINE, String.format("before invoke resource provider %s", provider.getClass())); - Resource result = provider.createResource(config); - logger.log( - Level.FINE, String.format("after invoke resource provider %s", provider.getClass())); - if (Resource.empty().equals(result)) { - logger.log( - Level.FINE, - String.format( - "resource provider did not provide any attribute: %s", provider.getClass())); - } - return result; - } catch (RuntimeException e) { - logger.log( - Level.WARNING, - String.format("error while invoking resource provider: %s", provider.getClass())); - return Resource.empty(); - } - } -} diff --git a/resources/src/main/java/co/elastic/otel/resources/ResourcesAutoConfiguration.java b/resources/src/main/java/co/elastic/otel/resources/ResourcesAutoConfiguration.java new file mode 100644 index 00000000..2056eb0e --- /dev/null +++ b/resources/src/main/java/co/elastic/otel/resources/ResourcesAutoConfiguration.java @@ -0,0 +1,57 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License 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 co.elastic.otel.resources; + +import com.google.auto.service.AutoService; +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import java.util.HashMap; +import java.util.Map; + +@AutoService(AutoConfigurationCustomizerProvider.class) +public class ResourcesAutoConfiguration implements AutoConfigurationCustomizerProvider { + + private static final String[] PROVIDERS = {"aws", "gcp"}; + + @Override + public void customize(AutoConfigurationCustomizer autoConfiguration) { + autoConfiguration.addPropertiesCustomizer(ResourcesAutoConfiguration::customize); + } + + @Override + public int order() { + // execute between the last user-provided provider and + // io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizer + return Integer.MAX_VALUE - 1; + } + + // visible for testing + static Map customize(ConfigProperties config) { + + Map result = new HashMap<>(); + for (String provider : PROVIDERS) { + String key = String.format("otel.resource.providers.%s.enabled", provider); + boolean value = config.getBoolean(key, true); + result.put(key, Boolean.toString(value)); + } + + return result; + } +} diff --git a/resources/src/test/java/co/elastic/otel/resources/ResourcesAutoConfigurationTest.java b/resources/src/test/java/co/elastic/otel/resources/ResourcesAutoConfigurationTest.java new file mode 100644 index 00000000..b16eb853 --- /dev/null +++ b/resources/src/test/java/co/elastic/otel/resources/ResourcesAutoConfigurationTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License 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 co.elastic.otel.resources; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class ResourcesAutoConfigurationTest { + + public static final String GCP_ENABLED = "otel.resource.providers.gcp.enabled"; + public static final String AWS_ENABLED = "otel.resource.providers.aws.enabled"; + + @Test + void elastic_defaults() { + // default everything should be enabled + + Map explicitConfig = Collections.emptyMap(); + Map expectedResult = new HashMap<>(); + expectedResult.put(GCP_ENABLED, "true"); + expectedResult.put(AWS_ENABLED, "true"); + + testConfig(explicitConfig, expectedResult); + } + + @Test + void explicitly_enabled() { + Map explicitConfig = new HashMap<>(); + explicitConfig.put(GCP_ENABLED, "true"); + + Map expectedResult = new HashMap<>(); + expectedResult.put(GCP_ENABLED, "true"); + expectedResult.put(AWS_ENABLED, "true"); + + testConfig(explicitConfig, expectedResult); + } + + @Test + void explicitly_disabled() { + Map explicitConfig = new HashMap<>(); + explicitConfig.put(GCP_ENABLED, "false"); + + Map expectedResult = new HashMap<>(); + expectedResult.put(GCP_ENABLED, "false"); + expectedResult.put(AWS_ENABLED, "true"); + + testConfig(explicitConfig, expectedResult); + } + + private static void testConfig( + Map explicitConfig, Map expectedResult) { + DefaultConfigProperties empty = DefaultConfigProperties.createFromMap(explicitConfig); + Map result = ResourcesAutoConfiguration.customize(empty); + assertThat(result).containsAllEntriesOf(expectedResult); + } +}