-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddedInstrumentationProperties.java
More file actions
121 lines (104 loc) · 4.21 KB
/
Copy pathEmbeddedInstrumentationProperties.java
File metadata and controls
121 lines (104 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* 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 java.util.regex.Pattern;
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 version = loadVersionFromClass(instrumentationName);
if (version == null) {
version = loadVersionFromProperties(instrumentationName);
}
return version;
}
private static final Pattern STRIP_VERSION_SUFFIX = Pattern.compile("(-[0-9.]*)$");
private static final Pattern NORMALIZE_VERSION = Pattern.compile("([0-9]+)\\.([0-9]+)");
private static final Pattern EXTRACT_VERSION = Pattern.compile(".*?([0-9.]*)$");
// visible for testing
@Nullable
static String loadVersionFromClass(String instrumentationName) {
// The same logic is duplicated in otel.instrumentation-version
// Strip trailing version suffix and remove dashes
String moduleName =
STRIP_VERSION_SUFFIX.matcher(instrumentationName).replaceAll("").replace("-", "");
// If the module name contains a non-trailing version number e.g. jaxrs-3.0-jersey-3.0 replace
// the dot with underscore. This is needed to turn the module name into valid package name, java
// package name segments cannot start with a number.
moduleName = NORMALIZE_VERSION.matcher(moduleName).replaceAll("$1_$2");
// Extract trailing version number and replace dots with underscores so it could be used as a
// package name segment.
String baseVersion =
EXTRACT_VERSION.matcher(instrumentationName).replaceAll("$1").replace(".", "_");
String packageName = moduleName + (baseVersion.isEmpty() ? "" : ".v" + baseVersion);
try {
Class<?> clazz =
Class.forName(packageName + ".internal.InstrumentationVersion", false, loader);
return clazz.getField("VERSION").get(null).toString();
} catch (ReflectiveOperationException e) {
return null;
}
}
// visible for testing
@Nullable
static String loadVersionFromProperties(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() {}
}