-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Library instrumentation should read its version from a file (#5692)
* Library instrumentation should read its version from a file * errorprone * animalsniffer * code review comments * add name as task input too * code review comments
- Loading branch information
Mateusz Rzeszutek
authored
Apr 4, 2022
1 parent
d908821
commit 8e722cc
Showing
23 changed files
with
188 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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
80 changes: 80 additions & 0 deletions
80
...java/io/opentelemetry/instrumentation/api/internal/EmbeddedInstrumentationProperties.java
This file contains 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,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.internal; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class EmbeddedInstrumentationProperties { | ||
|
||
private static final Logger logger = | ||
Logger.getLogger(EmbeddedInstrumentationProperties.class.getName()); | ||
|
||
private static final ClassLoader DEFAULT_LOADER; | ||
|
||
static { | ||
ClassLoader defaultLoader = EmbeddedInstrumentationProperties.class.getClassLoader(); | ||
if (defaultLoader == null) { | ||
defaultLoader = new BootstrapProxy(); | ||
} | ||
DEFAULT_LOADER = defaultLoader; | ||
} | ||
|
||
private static volatile ClassLoader loader = DEFAULT_LOADER; | ||
private static final Map<String, String> versions = new ConcurrentHashMap<>(); | ||
|
||
public static void setPropertiesLoader(ClassLoader propertiesLoader) { | ||
if (loader != DEFAULT_LOADER) { | ||
logger.warning( | ||
"Embedded properties loader has already been set up, further setPropertiesLoader() calls are ignored"); | ||
return; | ||
} | ||
loader = propertiesLoader; | ||
} | ||
|
||
@Nullable | ||
public static String findVersion(String instrumentationName) { | ||
return versions.computeIfAbsent( | ||
instrumentationName, EmbeddedInstrumentationProperties::loadVersion); | ||
} | ||
|
||
@Nullable | ||
private static String loadVersion(String instrumentationName) { | ||
String path = | ||
"META-INF/io/opentelemetry/instrumentation/" + instrumentationName + ".properties"; | ||
try (InputStream in = loader.getResourceAsStream(path)) { | ||
if (in == null) { | ||
logger.log(FINE, "Did not find embedded instrumentation properties file {0}", path); | ||
return null; | ||
} | ||
Properties parsed = new Properties(); | ||
parsed.load(in); | ||
return parsed.getProperty("version"); | ||
} catch (IOException e) { | ||
logger.log(FINE, "Failed to load embedded instrumentation properties file " + path, e); | ||
return null; | ||
} | ||
} | ||
|
||
private static final class BootstrapProxy extends ClassLoader { | ||
BootstrapProxy() { | ||
super(null); | ||
} | ||
} | ||
|
||
private EmbeddedInstrumentationProperties() {} | ||
} |
This file contains 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
2 changes: 2 additions & 0 deletions
2
.../test/resources/META-INF/io/opentelemetry/instrumentation/test-instrumentation.properties
This file contains 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,2 @@ | ||
# This file is used by the InstrumenterTest#instrumentationVersion_default() test method | ||
version=1.2.3 |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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.