Skip to content
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

Add resource attributes lost moving ReconfigurableOpenTelemetry to opentelemetry-api-plugin #11

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/opentelemetry-api-plugin</gitHubRepo>
<opentelemetry.version>1.40.0</opentelemetry.version>
<opentelemetry-instrumentation.version>2.5.0</opentelemetry-instrumentation.version>
<opentelemetry-semconv.version>1.26.0-alpha</opentelemetry-semconv.version>
<opentelemetry-instrumentation.version>2.6.0</opentelemetry-instrumentation.version>
<opentelemetry-semconv.version>1.25.0-alpha</opentelemetry-semconv.version>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We revert semconv to 1.25 as opentelemetry instrumentations have a dependency on semconv 1.25.


<jenkins.version>2.440.3</jenkins.version>

Expand Down Expand Up @@ -101,6 +101,10 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-resources</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>okhttp-api</artifactId>
Expand All @@ -110,6 +114,28 @@
<artifactId>opentelemetry-sdk-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-apache-httpclient-4.3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-runtime-telemetry-java8</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.jenkins.plugins.opentelemetry.api.instrumentation.resource;

import io.jenkins.plugins.opentelemetry.api.semconv.JenkinsAttributes;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;
import io.opentelemetry.semconv.ServiceAttributes;
import io.opentelemetry.semconv.incubating.ServiceIncubatingAttributes;

import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JenkinsResourceProvider implements ResourceProvider {
private final static Logger LOGGER = Logger.getLogger(JenkinsResourceProvider.class.getName());

@Override
public Resource createResource(ConfigProperties config) {
ResourceBuilder resourceBuilder = Resource.builder();
resourceBuilder.put(ServiceAttributes.SERVICE_NAME, JenkinsAttributes.JENKINS);
resourceBuilder.put(ServiceIncubatingAttributes.SERVICE_NAMESPACE, JenkinsAttributes.JENKINS);

Optional<String> jenkinsVersion = Optional.ofNullable(config.getString(JenkinsAttributes.JENKINS_VERSION.getKey()));
jenkinsVersion.ifPresent(version -> resourceBuilder.put(ServiceAttributes.SERVICE_VERSION, version));
// Report jenkins.version even if service.version is overriden
jenkinsVersion.ifPresent(version -> resourceBuilder.put(JenkinsAttributes.JENKINS_VERSION.getKey(), version));

Optional<String> jenkinsUrl = Optional.ofNullable(config.getString(JenkinsAttributes.JENKINS_URL.getKey()));
jenkinsUrl.ifPresent(version -> resourceBuilder.put(JenkinsAttributes.JENKINS_URL, version));

Optional<String> serviceInstanceId = Optional.ofNullable(config.getString(ServiceIncubatingAttributes.SERVICE_INSTANCE_ID.getKey()));
serviceInstanceId.ifPresent(version -> resourceBuilder.put(ServiceIncubatingAttributes.SERVICE_INSTANCE_ID, version));
Resource resource = resourceBuilder.build();
LOGGER.log(Level.FINER, () -> "Jenkins resource: " + resource);
return resource;

Check warning on line 41 in src/main/java/io/jenkins/plugins/opentelemetry/api/instrumentation/resource/JenkinsResourceProvider.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 20-41 are not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.jenkins.plugins.opentelemetry.api.semconv;

import io.opentelemetry.api.common.AttributeKey;
import jenkins.model.Jenkins;

/**
* @see io.opentelemetry.api.common.Attributes
* @see io.opentelemetry.semconv.ServiceAttributes
*/
public class JenkinsAttributes {

/**
* @see Jenkins#getRootUrl()
*/
public static final AttributeKey<String> JENKINS_URL = AttributeKey.stringKey("jenkins.url");

public static final String JENKINS = "jenkins";

public static final AttributeKey<String> JENKINS_VERSION = AttributeKey.stringKey("jenkins.version");

Check warning on line 24 in src/main/java/io/jenkins/plugins/opentelemetry/api/semconv/JenkinsAttributes.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 15-24 are not covered by tests

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.jenkins.plugins.opentelemetry.api.instrumentation.resource.JenkinsResourceProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.jenkins.plugins.opentelemetry;

import io.jenkins.plugins.opentelemetry.api.ReconfigurableOpenTelemetry;
import io.opentelemetry.instrumentation.apachehttpclient.v4_3.ApacheHttpClientTelemetry;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;

public class ApacheHttpClientInstrumentationTest {

@Test
public void test_instantiate_instrumented_http_client() {
ReconfigurableOpenTelemetry openTelemetry = new ReconfigurableOpenTelemetry();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be in a try-with-resources?

Suggested change
ReconfigurableOpenTelemetry openTelemetry = new ReconfigurableOpenTelemetry();
try (ReconfigurableOpenTelemetry openTelemetry = new ReconfigurableOpenTelemetry()) {

HttpClientBuilder httpClientBuilder = ApacheHttpClientTelemetry.create(openTelemetry).newHttpClientBuilder();
CloseableHttpClient httpClient = httpClientBuilder.build();
System.out.println(httpClient);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to assert something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes verify we don't have class loading issues. I'll iterate on the test

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.jenkins.plugins.opentelemetry;

import io.jenkins.plugins.opentelemetry.api.ReconfigurableOpenTelemetry;
import io.opentelemetry.instrumentation.jdbc.datasource.JdbcTelemetry;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.Test;

import javax.sql.DataSource;

public class JdbcInstrumentationTest {

@Test
public void test_instantiate_instrumented_data_source() {
try (ReconfigurableOpenTelemetry openTelemetry = new ReconfigurableOpenTelemetry()) {
DataSource dataSource = JdbcTelemetry.create(openTelemetry).wrap(new BasicDataSource());
System.out.println(dataSource);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to assert something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, verify class loading issues. I'll iterate on these tests

}
}
}