Skip to content

Commit f4e4ce6

Browse files
authored
Publish features as pre-packaged opentelemetry agent extension (#205)
1 parent 02f7e62 commit f4e4ce6

File tree

11 files changed

+161
-33
lines changed

11 files changed

+161
-33
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ jobs:
5050
path: |
5151
./agent/build/libs/elastic-otel-javaagent-*.jar
5252
53+
- name: Agent extension artifact
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: elastic-otel-agentextension
57+
path: |
58+
./agentextension/build/libs/elastic-otel-agentextension-*.jar
59+
5360
test:
5461
runs-on: ubuntu-latest
5562
needs:

agentextension/build.gradle.kts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id("java")
3+
id("elastic-otel.sign-and-publish-conventions")
4+
id("com.github.johnrengelman.shadow")
5+
}
6+
7+
description = "Bundles all elastic extensions in a fat-jar to be used" +
8+
" with the vanilla agent via the otel.javaagent.extensions config option"
9+
base.archivesName.set("elastic-otel-agentextension")
10+
11+
val shadowDependencies: Configuration by configurations.creating
12+
13+
dependencies {
14+
shadowDependencies(project(":custom"))
15+
}
16+
17+
18+
tasks {
19+
20+
jar {
21+
enabled = false
22+
dependsOn(shadowJar)
23+
}
24+
25+
shadowJar {
26+
configurations = listOf(shadowDependencies)
27+
mergeServiceFiles()
28+
archiveClassifier.set("")
29+
30+
// include licenses and notices in jar
31+
dependsOn(project(":agent").tasks.named("updateLicensesAndNotice"))
32+
from(rootDir) {
33+
into("META-INF")
34+
35+
include("LICENSE")
36+
include("NOTICE")
37+
include("licenses/**")
38+
}
39+
}
40+
}

custom/src/test/java/co/elastic/otel/ElasticDistroResourceProviderTest.java renamed to agentextension/src/main/java/co/elastic/otel/JavadocPlaceholder.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,5 @@
1818
*/
1919
package co.elastic.otel;
2020

21-
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
22-
23-
import io.opentelemetry.sdk.resources.Resource;
24-
import org.junit.jupiter.api.Test;
25-
26-
class ElasticDistroResourceProviderTest {
27-
28-
@Test
29-
void elasticDistroNameAndVersion() {
30-
ElasticDistroResourceProvider provider = new ElasticDistroResourceProvider();
31-
Resource resource = provider.createResource(null);
32-
33-
assertThat(resource.getAttributes())
34-
.hasSize(2)
35-
.containsEntry("telemetry.distro.name", "elastic")
36-
.containsKey("telemetry.distro.version");
37-
}
38-
}
21+
/** This class is here as a placeholder so that the sources and javadoc are not empty. */
22+
public class JavadocPlaceholder {}

buildSrc/src/main/kotlin/elastic-otel.test-with-agent-conventions.gradle.kts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,41 @@ plugins {
22
id("elastic-otel.java-conventions")
33
}
44

5-
val agentForTesting: Configuration by configurations.creating
5+
val agentDistroForTesting: Configuration by configurations.creating
6+
val upstreamAgentForTesting: Configuration by configurations.creating
7+
val agentExtension: Configuration by configurations.creating
8+
9+
10+
//https://github.com/gradle/gradle/issues/15383
11+
val catalog = extensions.getByType<VersionCatalogsExtension>().named("catalog")
612

713
dependencies {
8-
agentForTesting(project(":testing:agent-for-testing"))
14+
agentDistroForTesting(project(":testing:agent-for-testing"))
915
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common")
16+
17+
catalog.findLibrary("opentelemetryInstrumentationAlphaBom").ifPresent {
18+
upstreamAgentForTesting(platform(it))
19+
}
20+
upstreamAgentForTesting("io.opentelemetry.javaagent:opentelemetry-agent-for-testing")
21+
agentExtension(project(":agentextension"))
1022
}
1123

12-
tasks.withType<Test>() {
13-
dependsOn(agentForTesting)
14-
useJUnitPlatform()
15-
jvmArgs("-javaagent:${agentForTesting.singleFile.absoluteFile}")
24+
tasks {
25+
26+
val testWithVanillaAgentAndExtension by registering( Test::class) {
27+
dependsOn(upstreamAgentForTesting, agentExtension)
28+
group = "verification"
29+
useJUnitPlatform()
30+
jvmArgs("-javaagent:${upstreamAgentForTesting.singleFile.absoluteFile}")
31+
jvmArgs("-Dotel.javaagent.extensions=${agentExtension.singleFile.absoluteFile}")
32+
}
33+
test {
34+
dependsOn(testWithVanillaAgentAndExtension)
35+
}
36+
37+
test {
38+
dependsOn(agentDistroForTesting)
39+
useJUnitPlatform()
40+
jvmArgs("-javaagent:${agentDistroForTesting.singleFile.absoluteFile}")
41+
}
1642
}

custom/src/main/java/co/elastic/otel/ElasticDistroResourceProvider.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ public class ElasticDistroResourceProvider implements ResourceProvider {
3131

3232
@Override
3333
public Resource createResource(ConfigProperties configProperties) {
34-
return AgentVersion.VERSION == null
35-
? Resource.empty()
36-
: Resource.create(
37-
Attributes.of(
38-
ElasticAttributes.TELEMETRY_DISTRO_NAME,
39-
"elastic",
40-
ElasticAttributes.TELEMETRY_DISTRO_VERSION,
41-
AgentVersion.VERSION));
34+
if (AgentVersion.VERSION == null) {
35+
return Resource.empty();
36+
}
37+
try {
38+
Class.forName("co.elastic.otel.agent.ElasticAgent");
39+
} catch (ClassNotFoundException e) {
40+
// this means that we are running as an extension of the vanilla agent
41+
// and not as distro.
42+
return Resource.empty();
43+
}
44+
return Resource.create(
45+
Attributes.of(
46+
ElasticAttributes.TELEMETRY_DISTRO_NAME,
47+
"elastic",
48+
ElasticAttributes.TELEMETRY_DISTRO_VERSION,
49+
AgentVersion.VERSION));
4250
}
4351
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ jmh = {id = "me.champeau.jmh", version = "0.7.2"}
7272
nexusPublish = { id = "io.github.gradle-nexus.publish-plugin", version = '2.0.0' }
7373
licenseReport = { id = "com.github.jk1.dependency-license-report", version = "2.6" }
7474
dockerJavaApplication = { id = "com.bmuschko.docker-java-application", version = "9.4.0" }
75+
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rootProject.name = "elastic-otel-java"
1313

1414
include("agent")
1515
include("agent:entrypoint")
16+
include("agentextension")
1617
include("bootstrap")
1718
include("custom")
1819
include("instrumentation")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
20+
21+
import io.opentelemetry.api.GlobalOpenTelemetry;
22+
import io.opentelemetry.api.trace.Span;
23+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
24+
import io.opentelemetry.sdk.resources.Resource;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.RegisterExtension;
27+
28+
public class DistroResourceAttributesTest {
29+
30+
@RegisterExtension
31+
static final AgentInstrumentationExtension testing = AgentInstrumentationExtension.create();
32+
33+
@Test
34+
public void checkDistroName() {
35+
Span testSpan =
36+
GlobalOpenTelemetry.get().getTracer("dummy-tracer").spanBuilder("testSpan").startSpan();
37+
testSpan.end();
38+
assertThat(testing.spans()).hasSize(1);
39+
Resource resource = testing.spans().get(0).getResource();
40+
41+
boolean isRunningDistro = System.getProperty("otel.javaagent.extensions") == null;
42+
43+
if (isRunningDistro) {
44+
assertThat(resource.getAttributes()).containsEntry("telemetry.distro.name", "elastic");
45+
assertThat(resource.getAttributes()).containsKey("telemetry.distro.version");
46+
} else {
47+
// we are running with the vanilla agent as extension: we should not be setting the distro
48+
// name
49+
assertThat(resource.getAttributes()).doesNotContainKey("telemetry.distro.name");
50+
assertThat(resource.getAttributes()).doesNotContainKey("telemetry.distro.version");
51+
}
52+
}
53+
}

testing/integration-tests/agent-internals/src/test/java/SpanValueTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
import java.lang.reflect.Field;
2525
import java.util.concurrent.atomic.AtomicReferenceArray;
2626
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
2728

29+
@DisabledIfSystemProperty(
30+
named = "otel.javaagent.extensions",
31+
matches = ".+",
32+
disabledReason =
33+
"When running with the vanilla agent, span values won't be able to use the field backed storage")
2834
public class SpanValueTest {
2935
@Test
3036
@SuppressWarnings("unchecked")

testing/integration-tests/inferred-spans-test/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations")
99
}
1010

11-
tasks.test {
11+
tasks.withType<Test>() {
1212
jvmArgs(
1313
//"-Dotel.javaagent.debug=true",
1414
"-Delastic.otel.inferred.spans.enabled=true",

0 commit comments

Comments
 (0)