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
27 changes: 27 additions & 0 deletions prometheus-metrics-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,31 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Automatic-Module-Name>${automatic.module.name}</Automatic-Module-Name>
<!-- Micrometer excludes this artifact and supplies its own supplier.
The import must not be required for the bundle to resolve. -->
<Import-Package>
io.prometheus.metrics.tracer.initializer;resolution:=optional,*
</Import-Package>
</instructions>
</configuration>
<executions>
<execution>
<!-- Write MANIFEST.MF in process-classes for OSGi header tests. -->
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.prometheus.metrics.core.exemplars;

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

import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.junit.jupiter.api.Test;

class OsgiBundleManifestTest {

private static final String TRACER_INITIALIZER = "io.prometheus.metrics.tracer.initializer";

@Test
void tracerInitializerImportIsOptional() throws Exception {
Manifest manifest = loadBundleManifest(ExemplarSampler.class);
String header = manifest.getMainAttributes().getValue("Import-Package");
assertThat(header).as("Import-Package").isNotBlank();
String clause = requireClause(header, TRACER_INITIALIZER);
assertThat(clause)
.as("OSGi resolution of %s", TRACER_INITIALIZER)
.contains("resolution:=optional");
}

private static Manifest loadBundleManifest(Class<?> type) throws Exception {
CodeSource codeSource = type.getProtectionDomain().getCodeSource();
assertThat(codeSource).as("code source for %s", type.getName()).isNotNull();
URI location = codeSource.getLocation().toURI();
Path path = Path.of(location);
if (Files.isDirectory(path)) {
Path manifestFile = path.resolve("META-INF/MANIFEST.MF");
assertThat(Files.exists(manifestFile))
.as("bnd MANIFEST.MF for %s at %s", type.getName(), manifestFile)
.isTrue();
try (InputStream in = Files.newInputStream(manifestFile)) {
return new Manifest(in);
}
}
try (JarFile jar = new JarFile(path.toFile())) {
Manifest manifest = jar.getManifest();
assertThat(manifest).as("MANIFEST.MF in %s", path).isNotNull();
return manifest;
}
}

private static String requireClause(String header, String packageName) {
for (String rawClause : splitRespectingQuotes(header, ',')) {
String name = rawClause.trim();
int separator = indexOfUnquoted(name, ';');
if (separator >= 0) {
name = name.substring(0, separator).trim();
}
if (packageName.equals(name)) {
return rawClause.trim();
}
}
throw new AssertionError("missing Import-Package clause " + packageName);
}

private static int indexOfUnquoted(String value, char target) {
boolean inQuote = false;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '"') {
inQuote = !inQuote;
} else if (!inQuote && c == target) {
return i;
}
}
return -1;
}

private static List<String> splitRespectingQuotes(String value, char separator) {
List<String> parts = new ArrayList<>();
StringBuilder current = new StringBuilder();
boolean inQuote = false;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '"') {
inQuote = !inQuote;
current.append(c);
} else if (!inQuote && c == separator) {
parts.add(current.toString());
current.setLength(0);
} else {
current.append(c);
}
}
parts.add(current.toString());
return parts;
}
}
Loading