-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for OTEL metrics export (#339)
Allows export of the internal OTEL metrics
- Loading branch information
1 parent
411061d
commit 5490846
Showing
6 changed files
with
96 additions
and
13 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
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
48 changes: 48 additions & 0 deletions
48
server/src/main/java/com/octopus/teamcity/opentelemetry/server/helpers/OTELMetrics.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,48 @@ | ||
package com.octopus.teamcity.opentelemetry.server.helpers; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.export.MetricExporter; | ||
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
|
||
import javax.annotation.Nullable; | ||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class OTELMetrics { | ||
|
||
private static final AtomicBoolean metricsConfigured = new AtomicBoolean(false); | ||
private static SdkMeterProvider sdkMeterProvider; | ||
|
||
// this is not quite right... | ||
// it's using project level settings (which can be different across projects) | ||
// but setting up a global configuration. | ||
// likely need to refactor the config so that it has metrics configuration as global | ||
|
||
public static SdkMeterProvider getOTELMeterProvider(@Nullable MetricExporter metricExporter, Resource serviceNameResource) { | ||
if (metricsConfigured.get()) return sdkMeterProvider; | ||
metricsConfigured.set(true); | ||
|
||
var meterProviderBuilder = SdkMeterProvider.builder() | ||
.setResource(Resource.getDefault().merge(serviceNameResource)); | ||
if (metricExporter != null) { | ||
var providedMetricExporterBuilder = PeriodicMetricReader.builder(metricExporter) | ||
.setInterval(Duration.ofSeconds(10)) | ||
.build(); | ||
meterProviderBuilder | ||
.registerMetricReader(providedMetricExporterBuilder); | ||
} | ||
sdkMeterProvider = meterProviderBuilder.build(); | ||
var globalOpenTelemetry = OpenTelemetrySdk.builder() | ||
.setMeterProvider(sdkMeterProvider) | ||
.build(); | ||
|
||
GlobalOpenTelemetry.set(globalOpenTelemetry); | ||
|
||
// Shutdown hooks to close resources properly | ||
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::close)); | ||
return sdkMeterProvider; | ||
} | ||
} |
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