Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import glide.api.models.configuration.ReadFrom;
import org.springframework.lang.Nullable;

import glide.api.OpenTelemetry;
import glide.api.OpenTelemetry.MetricsConfig;
import glide.api.OpenTelemetry.OpenTelemetryConfig;
import glide.api.OpenTelemetry.TracesConfig;

/**
* Configuration interface for Valkey-Glide client settings.
*
Expand Down Expand Up @@ -209,7 +214,62 @@ public ValkeyGlideClientConfigurationBuilder reconnectStrategy(BackoffStrategy r
this.reconnectStrategy = reconnectStrategy;
return this;
}


/**
* Initialize GLIDE OpenTelemetry with OTLP endpoints.
*
* If at least one endpoint (traces or metrics) is provided, this will initialize
* OpenTelemetry once per JVM.
*/
public ValkeyGlideClientConfigurationBuilder useOpenTelemetry(
@Nullable ValkeyGlideOpenTelemetry telemetry
) {
if (telemetry == null) {
return this;
}

String tracesEndpoint = telemetry.tracesEndpoint();
String metricsEndpoint = telemetry.metricsEndpoint();
Integer samplePercentage = telemetry.samplePercentage();
Long flushIntervalMs = telemetry.flushIntervalMs();

boolean hasTraces = tracesEndpoint != null && !tracesEndpoint.isBlank();
boolean hasMetrics = metricsEndpoint != null && !metricsEndpoint.isBlank();

// Initialize if AT LEAST ONE endpoint is provided
if (hasTraces || hasMetrics) {

OpenTelemetryConfig.Builder otelBuilder = OpenTelemetryConfig.builder();

if (hasTraces) {
TracesConfig.Builder tracesBuilder =
TracesConfig.builder().endpoint(tracesEndpoint);

if (samplePercentage != null) {
tracesBuilder.samplePercentage(samplePercentage);
}

otelBuilder.traces(tracesBuilder.build());
}

if (hasMetrics) {
otelBuilder.metrics(
MetricsConfig.builder()
.endpoint(metricsEndpoint)
.build()
);
}

if (flushIntervalMs != null) {
otelBuilder.flushIntervalMs(flushIntervalMs);
}

OpenTelemetry.init(otelBuilder.build());
}

return this;
}

/**
* Set the maximum pool size for client pooling.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.valkey.springframework.data.valkey.connection.valkeyglide;

import org.springframework.lang.Nullable;

public final class ValkeyGlideOpenTelemetry {

// ---- DEFAULT VALUES (documented & reusable) ----
public static final String DEFAULT_TRACES_ENDPOINT =
"http://localhost:4318/v1/traces";
public static final String DEFAULT_METRICS_ENDPOINT =
"http://localhost:4318/v1/metrics";
public static final int DEFAULT_SAMPLE_PERCENTAGE = 10;
public static final long DEFAULT_FLUSH_INTERVAL_MS = 1000L;

private final @Nullable String tracesEndpoint;
private final @Nullable String metricsEndpoint;
private final @Nullable Integer samplePercentage;
private final @Nullable Long flushIntervalMs;

private ValkeyGlideOpenTelemetry(Builder b) {
this.tracesEndpoint = b.tracesEndpoint;
this.metricsEndpoint = b.metricsEndpoint;
this.samplePercentage = b.samplePercentage;
this.flushIntervalMs = b.flushIntervalMs;
}

/** Default OpenTelemetry configuration (local collector, sampled, low flush interval). */
public static ValkeyGlideOpenTelemetry defaults() {
return builder()
.tracesEndpoint(DEFAULT_TRACES_ENDPOINT)
.metricsEndpoint(DEFAULT_METRICS_ENDPOINT)
.samplePercentage(DEFAULT_SAMPLE_PERCENTAGE)
.flushIntervalMs(DEFAULT_FLUSH_INTERVAL_MS)
.build();
}

public static Builder builder() {
return new Builder();
}

public @Nullable String tracesEndpoint() { return tracesEndpoint; }
public @Nullable String metricsEndpoint() { return metricsEndpoint; }
public @Nullable Integer samplePercentage() { return samplePercentage; }
public @Nullable Long flushIntervalMs() { return flushIntervalMs; }

public static final class Builder {
private @Nullable String tracesEndpoint;
private @Nullable String metricsEndpoint;
private @Nullable Integer samplePercentage;
private @Nullable Long flushIntervalMs;

private Builder() {}

public Builder tracesEndpoint(@Nullable String v) {
this.tracesEndpoint = v;
return this;
}

public Builder metricsEndpoint(@Nullable String v) {
this.metricsEndpoint = v;
return this;
}

public Builder samplePercentage(@Nullable Integer v) {
this.samplePercentage = v;
return this;
}

public Builder flushIntervalMs(@Nullable Long v) {
this.flushIntervalMs = v;
return this;
}

public ValkeyGlideOpenTelemetry build() {
return new ValkeyGlideOpenTelemetry(this);
}
}
}