-
Notifications
You must be signed in to change notification settings - Fork 835
fix(exposition): export internal package for OSGi resolution #2415
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
Open
arimu1
wants to merge
4
commits into
prometheus:main
Choose a base branch
from
arimu1:fix/2395-osgi-expositionformats-internal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
687e885
fix(exposition): export internal package for OSGi resolution
arimu1 991921d
Merge branch 'main' into fix/2395-osgi-expositionformats-internal
jaydeluca bc2860e
test(exposition): assert OSGi export of internal package
arimu1 0e7138a
fix(exposition): drop SNAPSHOT OSGi versions and optional import
arimu1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
...formats/src/test/java/io/prometheus/metrics/expositionformats/OsgiBundleManifestTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package io.prometheus.metrics.expositionformats; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl; | ||
| 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.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.jar.JarFile; | ||
| import java.util.jar.Manifest; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class OsgiBundleManifestTest { | ||
|
|
||
| private static final String INTERNAL_PACKAGE = | ||
| "io.prometheus.metrics.expositionformats.internal"; | ||
| private static final String GENERATED_PREFIX = | ||
| "io.prometheus.metrics.expositionformats.generated"; | ||
| private static final String SHADED_BSN = "io.prometheus.metrics-exposition-formats"; | ||
| private static final String TEXTFORMATS_BSN = "io.prometheus.metrics-exposition-textformats"; | ||
|
|
||
| @Test | ||
| void formatsBundleExportsInternalPackage() throws Exception { | ||
| Manifest manifest = loadBundleManifest(PrometheusProtobufWriterImpl.class); | ||
| assertThat(bundleSymbolicName(manifest)).contains("exposition-formats"); | ||
| List<PackageClause> exported = parsePackageHeader(manifest, "Export-Package"); | ||
| assertThat(names(exported)).contains(INTERNAL_PACKAGE); | ||
| assertThat(exported).anyMatch(clause -> clause.name.startsWith(GENERATED_PREFIX)); | ||
| for (PackageClause clause : exported) { | ||
| if (!INTERNAL_PACKAGE.equals(clause.name) && !clause.name.startsWith(GENERATED_PREFIX)) { | ||
| continue; | ||
| } | ||
| assertThat(clause.attributes.get("version")) | ||
| .as("version of %s", clause.name) | ||
| .isNotBlank() | ||
| .doesNotContain(".SNAPSHOT") | ||
| .doesNotContain("-SNAPSHOT"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void textformatsBundleImportsInternalPackageOptionally() throws Exception { | ||
| Manifest manifest = loadBundleManifest(PrometheusProtobufWriter.class); | ||
| assertThat(bundleSymbolicName(manifest)).isEqualTo(TEXTFORMATS_BSN); | ||
| PackageClause internal = | ||
| requireClause(parsePackageHeader(manifest, "Import-Package"), INTERNAL_PACKAGE); | ||
| assertThat(internal.directives.get("resolution")).isEqualTo("optional"); | ||
| } | ||
|
|
||
| @Test | ||
| void protobufImportMatchesShading() throws Exception { | ||
| Manifest manifest = loadBundleManifest(PrometheusProtobufWriterImpl.class); | ||
| List<String> imported = names(parsePackageHeader(manifest, "Import-Package")); | ||
| if (SHADED_BSN.equals(bundleSymbolicName(manifest))) { | ||
| assertThat(imported).doesNotContain("com.google.protobuf"); | ||
| } else { | ||
| assertThat(imported).contains("com.google.protobuf"); | ||
| } | ||
| } | ||
|
|
||
| private static String bundleSymbolicName(Manifest manifest) { | ||
| return manifest.getMainAttributes().getValue("Bundle-SymbolicName"); | ||
| } | ||
|
|
||
| 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 PackageClause requireClause(List<PackageClause> clauses, String packageName) { | ||
| return clauses.stream() | ||
| .filter(clause -> packageName.equals(clause.name)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new AssertionError("missing package clause " + packageName)); | ||
| } | ||
|
|
||
| private static List<String> names(List<PackageClause> clauses) { | ||
| List<String> names = new ArrayList<>(); | ||
| for (PackageClause clause : clauses) { | ||
| names.add(clause.name); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| /** OSGi headers are comma-separated clauses; attributes may contain quoted commas. */ | ||
| private static List<PackageClause> parsePackageHeader(Manifest manifest, String header) { | ||
| String value = manifest.getMainAttributes().getValue(header); | ||
| assertThat(value).as("%s", header).isNotBlank(); | ||
| List<PackageClause> clauses = new ArrayList<>(); | ||
| for (String rawClause : splitRespectingQuotes(value, ',')) { | ||
| List<String> parts = splitRespectingQuotes(rawClause, ';'); | ||
| if (parts.isEmpty()) { | ||
| continue; | ||
| } | ||
| String name = parts.get(0).trim(); | ||
| if (name.isEmpty()) { | ||
| continue; | ||
| } | ||
| Map<String, String> attributes = new LinkedHashMap<>(); | ||
| Map<String, String> directives = new LinkedHashMap<>(); | ||
| for (int i = 1; i < parts.size(); i++) { | ||
| String part = parts.get(i).trim(); | ||
| int directiveEq = part.indexOf(":="); | ||
| int attributeEq = part.indexOf('='); | ||
| if (directiveEq >= 0 && (attributeEq < 0 || directiveEq <= attributeEq)) { | ||
| directives.put( | ||
| part.substring(0, directiveEq).trim(), | ||
| unquote(part.substring(directiveEq + 2).trim())); | ||
| } else if (attributeEq >= 0) { | ||
| attributes.put( | ||
| part.substring(0, attributeEq).trim(), | ||
| unquote(part.substring(attributeEq + 1).trim())); | ||
| } | ||
| } | ||
| clauses.add(new PackageClause(name, attributes, directives)); | ||
| } | ||
| return clauses; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| private static String unquote(String value) { | ||
| if (value.length() >= 2 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') { | ||
| return value.substring(1, value.length() - 1); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| static final class PackageClause { | ||
| final String name; | ||
| final Map<String, String> attributes; | ||
| final Map<String, String> directives; | ||
|
|
||
| PackageClause(String name, Map<String, String> attributes, Map<String, String> directives) { | ||
| this.name = name; | ||
| this.attributes = attributes; | ||
| this.directives = directives; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.