Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ServiceAttributes;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.apache.maven.rtinfo.internal.DefaultRuntimeInformation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.maven.Maven;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MavenResourceProvider implements ResourceProvider {

private static final Logger logger = LoggerFactory.getLogger(MavenResourceProvider.class);

@Override
public Resource createResource(ConfigProperties config) {
// TODO verify if there is solution to retrieve the RuntimeInformation instance loaded by the
// Maven Plexus Launcher
RuntimeInformation runtimeInformation = new DefaultRuntimeInformation();
return Resource.builder()
.put(ServiceAttributes.SERVICE_NAME, MavenOtelSemanticAttributes.SERVICE_NAME_VALUE)
.put(ServiceAttributes.SERVICE_VERSION, runtimeInformation.getMavenVersion())
.put(ServiceAttributes.SERVICE_VERSION, getMavenRuntimeVersion())
.put(
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_NAME,
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_NAME_VALUE)
Expand All @@ -31,4 +34,40 @@ public Resource createResource(ConfigProperties config) {
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_VERSION_VALUE)
.build();
}

/**
* Recopy of <a
* href="https://github.com/apache/maven/blob/maven-4.0.0-rc-2/compat/maven-compat/src/main/java/org/apache/maven/execution/DefaultRuntimeInformation.java">
* <code>org.apache.maven.rtinfo.internal.DefaultRuntimeInformation#getMavenVersion()</code></a>
* that is not available in Maven 4.0+
*/
static String getMavenRuntimeVersion() {
String mavenVersion;
Properties props = new Properties();
String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";

try (InputStream is = Maven.class.getResourceAsStream("/" + resource)) {
if (is != null) {
props.load(is);
} else {
logger.warn(
"Could not locate {} on classpath, Maven runtime information not available", resource);
}
} catch (IOException e) {
String msg = "Could not parse " + resource + ", Maven runtime information not available";
if (logger.isDebugEnabled()) {
logger.warn(msg, e);
} else {
logger.warn(msg);
}
}

String version = props.getProperty("version", "").trim();
if (!version.startsWith("${")) {
mavenVersion = version;
} else {
mavenVersion = "";
}
return mavenVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.maven.resources;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class MavenResourceProviderTest {

@Test
void testGetMavenVersion() {
String mavenVersion = MavenResourceProvider.getMavenRuntimeVersion();
assertThat(mavenVersion).isEqualTo("3.5.0");
}
}