-
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.
Dont use globals for the tc metrics settings (#343)
- Loading branch information
1 parent
72de115
commit 5641021
Showing
9 changed files
with
57 additions
and
56 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
38 changes: 8 additions & 30 deletions
38
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 |
---|---|---|
@@ -1,48 +1,26 @@ | ||
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 | ||
|
||
@Nullable | ||
public static SdkMeterProvider getOTELMeterProvider(@Nullable MetricExporter metricExporter, Resource serviceNameResource) { | ||
if (metricsConfigured.get()) return sdkMeterProvider; | ||
metricsConfigured.set(true); | ||
if (metricExporter == null) return null; | ||
|
||
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) | ||
var providedMetricExporter = PeriodicMetricReader.builder(metricExporter) | ||
.setInterval(Duration.ofSeconds(10)) | ||
.build(); | ||
|
||
GlobalOpenTelemetry.set(globalOpenTelemetry); | ||
|
||
// Shutdown hooks to close resources properly | ||
Runtime.getRuntime().addShutdownHook(new Thread(sdkMeterProvider::close)); | ||
return sdkMeterProvider; | ||
return SdkMeterProvider.builder() | ||
.setResource(Resource.getDefault().merge(serviceNameResource)) | ||
.registerMetricReader(providedMetricExporter) | ||
.build(); | ||
} | ||
} |
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