Skip to content

Commit

Permalink
Add an option to disable the histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran committed Nov 30, 2023
1 parent 2d1e822 commit b618d02
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ public class OpenTelemetryMiddleware implements TelemetryMiddleware {
private final LongHistogram histogram;
private final LongCounter errorCounter;
private final LongCounter requestCounter;
private final boolean enableHistogram;

public OpenTelemetryMiddleware(final OpenTelemetry otel) {
this(otel, OpenTelemetryInfo.PREFIX);
this(otel, true, OpenTelemetryInfo.PREFIX);
}

public OpenTelemetryMiddleware(final OpenTelemetry otel, final String prefix) {
public OpenTelemetryMiddleware(final OpenTelemetry otel, final boolean enableHistogram) {
this(otel, enableHistogram, OpenTelemetryInfo.PREFIX);
}

public OpenTelemetryMiddleware(final OpenTelemetry otel, final boolean enableHistogram, final String prefix) {
Meter meter = otel.meterBuilder(OpenTelemetryResponseSerializer.class.getPackage().getName()).build();
histogram = meter.histogramBuilder(prefix + "." + OpenTelemetryInfo.CLIENT_DURATION)
.ofLongs()
.setUnit(OpenTelemetryInfo.UNIT_MS)
.build();
this.enableHistogram = enableHistogram;
if (enableHistogram) {
histogram = meter.histogramBuilder(prefix + "." + OpenTelemetryInfo.CLIENT_DURATION)
.ofLongs()
.setUnit(OpenTelemetryInfo.UNIT_MS)
.build();
} else {
histogram = null;
}
errorCounter = meter.counterBuilder(prefix + "." + OpenTelemetryInfo.CLIENT_REQUEST_ERROR).build();
requestCounter = meter.counterBuilder(prefix + "." + OpenTelemetryInfo.CLIENT_REQUEST_TOTAL).build();
}
Expand All @@ -58,7 +68,9 @@ public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request,
builder.put(OpenTelemetryInfo.SERVER_PORT, request.getUri().getPort());
}
Attributes attributes = builder.build();
histogram.record(Duration.between(start, Instant.now()).toMillis(), attributes);
if (enableHistogram) {
histogram.record(Duration.between(start, Instant.now()).toMillis(), attributes);
}
requestCounter.add(1, attributes);
if (response.getStatusCode() >= 400) {
errorCounter.add(1, attributes);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

package com.commercetools.sdk.examples.springmvc.service;

import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.client.ProjectScopedApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;

import com.commercetools.monitoring.opentelemetry.OpenTelemetryMiddleware;
import com.commercetools.monitoring.opentelemetry.OpenTelemetryResponseSerializer;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.vrap.rmf.base.client.*;
Expand All @@ -15,7 +13,6 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.annotation.RequestScope;

@Configuration
@EnableAutoConfiguration
Expand All @@ -30,16 +27,27 @@ public class CtpClientBeanService {
@Value(value = "${ctp.project.key}")
private String projectKey;

@Value(value = "${ctp.enable.histogram:true}")
private boolean enableHistogram;

private ClientCredentials credentials() {
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
}

@Bean
public ProjectScopedApiRoot apiRoot() {
return ApiRootBuilder.of()
.defaultClient(credentials())
.withSerializer(new OpenTelemetryResponseSerializer(ResponseSerializer.of(), GlobalOpenTelemetry.get()))
.withTelemetryMiddleware(new OpenTelemetryMiddleware(GlobalOpenTelemetry.get()))
.build(projectKey);
if (enableHistogram){
return ApiRootBuilder.of()
.defaultClient(credentials())
.withSerializer(new OpenTelemetryResponseSerializer(ResponseSerializer.of(), GlobalOpenTelemetry.get()))
.withTelemetryMiddleware(new OpenTelemetryMiddleware(GlobalOpenTelemetry.get()))
.build(projectKey);
} else {
return ApiRootBuilder.of()
.defaultClient(credentials())
.withTelemetryMiddleware(new OpenTelemetryMiddleware(GlobalOpenTelemetry.get(), false))
.build(projectKey);
}

}
}

0 comments on commit b618d02

Please sign in to comment.