-
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.
Move around where we configure the metrics
- Loading branch information
1 parent
8875b99
commit 4b60ace
Showing
9 changed files
with
70 additions
and
52 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
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
package com.octopus.teamcity.opentelemetry.server.helpers; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.exporter.logging.LoggingMetricExporter; | ||
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; | ||
|
||
public static SdkMeterProvider getOTELMeterProvider(@Nullable MetricExporter metricExporter, Resource serviceNameResource) { | ||
if (metricsConfigured.get()) return sdkMeterProvider; | ||
metricsConfigured.set(true); | ||
|
||
var loggingMetricExporter = LoggingMetricExporter.create(); | ||
var consoleLogMetricReader = PeriodicMetricReader.builder(loggingMetricExporter) | ||
.setInterval(Duration.ofSeconds(10)) | ||
.build(); | ||
var meterProviderBuilder = SdkMeterProvider.builder() | ||
.setResource(Resource.getDefault().merge(serviceNameResource)) | ||
.registerMetricReader(consoleLogMetricReader); | ||
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
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