-
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.
- Loading branch information
Showing
7 changed files
with
121 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dev.vality.disputes.config; | ||
|
||
import dev.vality.disputes.config.properties.OtelProperties; | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
import io.opentelemetry.context.propagation.ContextPropagators; | ||
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.time.Duration; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class OtelConfig { | ||
|
||
private final OtelProperties otelProperties; | ||
|
||
@Value("${spring.application.name}") | ||
private String applicationName; | ||
|
||
@Bean | ||
public OpenTelemetry openTelemetryConfig() { | ||
var resource = Resource.getDefault() | ||
.merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName))); | ||
var sdkTracerProvider = SdkTracerProvider.builder() | ||
.addSpanProcessor(BatchSpanProcessor.builder(OtlpHttpSpanExporter.builder() | ||
.setEndpoint(otelProperties.getResource()) | ||
.setTimeout(Duration.ofMillis(otelProperties.getTimeout())) | ||
.build()) | ||
.build()) | ||
.setSampler(Sampler.alwaysOn()) | ||
.setResource(resource) | ||
.build(); | ||
var openTelemetrySdk = OpenTelemetrySdk.builder() | ||
.setTracerProvider(sdkTracerProvider) | ||
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) | ||
.build(); | ||
registerGlobalOpenTelemetry(openTelemetrySdk); | ||
return openTelemetrySdk; | ||
} | ||
|
||
private static void registerGlobalOpenTelemetry(OpenTelemetry openTelemetry) { | ||
try { | ||
GlobalOpenTelemetry.set(openTelemetry); | ||
} catch (Exception e) { | ||
log.warn("please initialize the ObservabilitySdk before starting the application"); | ||
GlobalOpenTelemetry.resetForTest(); | ||
try { | ||
GlobalOpenTelemetry.set(openTelemetry); | ||
} catch (Exception ex) { | ||
log.warn("unable to set GlobalOpenTelemetry", ex); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/dev/vality/disputes/config/properties/OtelProperties.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,17 @@ | ||
package dev.vality.disputes.config.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Setter | ||
@Component | ||
@ConfigurationProperties(prefix = "otel") | ||
public class OtelProperties { | ||
|
||
private String resource; | ||
private Long timeout; | ||
|
||
} |
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