|
4 | 4 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; |
5 | 5 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
6 | 6 | import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder; |
| 7 | +import io.sentry.Instrumenter; |
| 8 | +import io.sentry.Sentry; |
| 9 | +import io.sentry.SentryOptions; |
| 10 | +import io.sentry.protocol.SdkVersion; |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URL; |
| 13 | +import java.util.Enumeration; |
7 | 14 | import java.util.HashMap; |
8 | 15 | import java.util.Map; |
| 16 | +import java.util.jar.Attributes; |
| 17 | +import java.util.jar.Manifest; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
9 | 20 |
|
10 | 21 | public final class SentryAutoConfigurationCustomizerProvider |
11 | 22 | implements AutoConfigurationCustomizerProvider { |
12 | 23 |
|
13 | 24 | @Override |
14 | 25 | public void customize(AutoConfigurationCustomizer autoConfiguration) { |
| 26 | + final @Nullable String sentryPropertiesFile = System.getenv("SENTRY_PROPERTIES_FILE"); |
| 27 | + final @Nullable String sentryDsn = System.getenv("SENTRY_DSN"); |
| 28 | + |
| 29 | + if (sentryPropertiesFile != null || sentryDsn != null) { |
| 30 | + Sentry.init( |
| 31 | + options -> { |
| 32 | + options.setEnableExternalConfiguration(sentryPropertiesFile != null); |
| 33 | + options.setInstrumenter(Instrumenter.OTEL); |
| 34 | + final @Nullable SdkVersion sdkVersion = createSdkVersion(options); |
| 35 | + if (sdkVersion != null) { |
| 36 | + options.setSdkVersion(sdkVersion); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
15 | 41 | autoConfiguration |
16 | 42 | .addTracerProviderCustomizer(this::configureSdkTracerProvider) |
17 | 43 | .addPropertiesSupplier(this::getDefaultProperties); |
18 | 44 | } |
19 | 45 |
|
| 46 | + private @Nullable SdkVersion createSdkVersion(final @NotNull SentryOptions sentryOptions) { |
| 47 | + SdkVersion sdkVersion = sentryOptions.getSdkVersion(); |
| 48 | + |
| 49 | + try { |
| 50 | + final @NotNull Enumeration<URL> resources = |
| 51 | + ClassLoader.getSystemClassLoader().getResources("META-INF/MANIFEST.MF"); |
| 52 | + while (resources.hasMoreElements()) { |
| 53 | + try { |
| 54 | + final @NotNull Manifest manifest = new Manifest(resources.nextElement().openStream()); |
| 55 | + final @Nullable Attributes mainAttributes = manifest.getMainAttributes(); |
| 56 | + if (mainAttributes != null) { |
| 57 | + final @Nullable String name = mainAttributes.getValue("Sentry-Opentelemetry-SDK-Name"); |
| 58 | + final @Nullable String version = mainAttributes.getValue("Sentry-Version-Name"); |
| 59 | + |
| 60 | + if (name != null && version != null) { |
| 61 | + sdkVersion = SdkVersion.updateSdkVersion(sdkVersion, name, version); |
| 62 | + sdkVersion.addPackage("maven:io.sentry:sentry-opentelemetry-agent", version); |
| 63 | + final @Nullable String otelVersion = |
| 64 | + mainAttributes.getValue("Sentry-Opentelemetry-Version-Name"); |
| 65 | + if (otelVersion != null) { |
| 66 | + sdkVersion.addPackage("maven:io.opentelemetry:opentelemetry-sdk", otelVersion); |
| 67 | + } |
| 68 | + final @Nullable String otelJavaagentVersion = |
| 69 | + mainAttributes.getValue("Sentry-Opentelemetry-Javaagent-Version-Name"); |
| 70 | + if (otelJavaagentVersion != null) { |
| 71 | + sdkVersion.addPackage( |
| 72 | + "maven:io.opentelemetry.javaagent:opentelemetry-javaagent", |
| 73 | + otelJavaagentVersion); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + // ignore |
| 79 | + } |
| 80 | + } |
| 81 | + } catch (IOException e) { |
| 82 | + // ignore |
| 83 | + } |
| 84 | + |
| 85 | + return sdkVersion; |
| 86 | + } |
| 87 | + |
20 | 88 | private SdkTracerProviderBuilder configureSdkTracerProvider( |
21 | 89 | SdkTracerProviderBuilder tracerProvider, ConfigProperties config) { |
22 | 90 | return tracerProvider.addSpanProcessor(new SentrySpanProcessor()); |
|
0 commit comments