Skip to content
Merged
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 @@ -28,6 +28,7 @@
import com.comet.opik.infrastructure.ratelimit.RateLimitModule;
import com.comet.opik.infrastructure.redis.RedisModule;
import com.comet.opik.infrastructure.usagelimit.UsageLimitModule;
import com.comet.opik.infrastructure.web.InstantParamConverter;
import com.comet.opik.utils.JsonBigDecimalDeserializer;
import com.comet.opik.utils.JsonUtils;
import com.comet.opik.utils.OpenAiMessageJsonDeserializer;
Expand Down Expand Up @@ -136,5 +137,6 @@ public void run(OpikConfiguration configuration, Environment environment) {
jersey.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);

jersey.register(JsonProcessingExceptionMapper.class);
jersey.register(InstantParamConverter.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.comet.opik.api;

import com.comet.opik.domain.IdGenerator;
import com.google.inject.Singleton;
import jakarta.inject.Inject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.util.UUID;

/**
* Mapper for converting Instant time boundaries to UUIDv7 bounds for efficient BETWEEN queries.
*
* UUIDv7 encodes the timestamp in the first 48 bits, allowing for lexicographic sorting by time.
* This mapper creates UUID boundaries to ensure correct BETWEEN query semantics for time-based filtering.
*
* Implementation Note:
* We use IdGenerator.getTimeOrderedEpoch() which reliably creates UUIDs from timestamps.
* Per UUIDv7 RFC, the sub-millisecond 12 bits are optional and depend on implementation.
* Our implementation uses monotonic values with millisecond granularity, so using the start/end
* interval semantics with ±1ms ensures correct BETWEEN query results.
* This approach has been battle-tested for months without issues.
*/
@Singleton
@RequiredArgsConstructor(onConstructor_ = @Inject)
@Slf4j
public class InstantToUUIDMapper {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, I'd rather have a single source of truth in IdGenerator. See getTimeOrderedEpoch method which relies on our library com.fasterxml.uuid.java-uuid-generator, specifically on method

Initially, I'd rather rely on a reliable library implementation, than re-inventing the wheel ourselves. That way we don't need to maintain the implementation ourselves.

I propose the following:

  1. Double check that IdGenerator.getTimeOrderedEpoch (based on TimeBasedEpochGenerator.construct.
  2. If all good, use it here.
  3. Optionally substitute our own implementation from OpenTelemetryMapper.convertOtelIdToUUIDv7.

Let me know how this sounds.

Copy link
Copy Markdown
Contributor Author

@thiagohora thiagohora Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andrescrz,

Thanks for your comment. On why not use getTimeOrderedEpoch:

While IdGenerator.getTimeOrderedEpoch() also creates UUIDs from timestamps using the java-uuid-generator library, it uses random bits for the lower 80 bits of the UUID. For time-range queries, we need:

  • Lower bound: UUID with ALL ZEROS for random bits (lexicographically smallest UUID at this timestamp)
  • Upper bound: UUID at (timestamp+1ms) with ALL ZEROS (first UUID AFTER the end time)

This ensures that BETWEEN queries correctly include all traces within the specified time range. IdGenerator's random bits would make the bounds non-deterministic, breaking BETWEEN semantics.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember discussing this with @BorisTkachenko and I raised the same concerns, but we came to the conclusion that getTimeOrderedEpoch works fine in practice. Some reasons:

  • From the UUID v7 RFC, the sub-millisecs 12 bits are optional and depend on the implementation.
  • Our implementation uses monotonic values instead, so granularly is actually by millisecs.
  • As you use start and end interval, just with the semantics and/or increasing/decreasing 1 millisecs, you would get correct results if this was an issue (it isn't).
  • You already do +1 millisec.

With all this in mind, I still recommend going with getTimeOrderedEpoch. Some queries based on getTimeOrderedEpoch are already in place and working for months, without bug reports so far.

But not concern if you prefer to move forward. This would be just a small piece of tech debt to centralise and remove duplications in the future (if we ever do it).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me double check


private final IdGenerator idGenerator;

/**
* Generates a UUIDv7 lower bound from a timestamp for BETWEEN queries.
* Creates the lexicographically smallest UUID with this timestamp.
*
* @param timestamp the instant in time
* @return the lower bound UUIDv7 (min UUID at this timestamp)
*/
public UUID toLowerBound(Instant timestamp) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: probably this whole class is no longer needed, as it adds verbosity with little benefit. You could move these to IdGenerator and IdGeneratorImpl as overloaded methods and encapsulate the logic there.

if (timestamp == null) {
return null;
}

return idGenerator.getTimeOrderedEpoch(timestamp.toEpochMilli());
}

/**
* Generates a UUIDv7 upper bound from a timestamp for BETWEEN queries.
* Creates the UUID at the next millisecond to ensure inclusive BETWEEN semantics.
*
* This ensures BETWEEN includes all UUIDs created within the end timestamp millisecond.
* For example, if querying traces between 10:00:00.000 and 10:00:01.000:
* - toLowerBound(10:00:00.000) gives the UUID at 10:00:00
* - toUpperBound(10:00:01.000) gives the UUID at 10:00:02 (next millisecond)
* - BETWEEN x AND y includes all UUIDs from 10:00:00 up to but NOT including 10:00:02
* - Which correctly includes all UUIDs from 10:00:00 through 10:00:01.999
*
* @param timestamp the instant in time
* @return the upper bound UUIDv7 (UUID at timestamp+1ms)
*/
public UUID toUpperBound(Instant timestamp) {
if (timestamp == null) {
return null;
}

// Add 1ms to get the first UUID AFTER the end time
// BETWEEN will include all UUIDs from toLowerBound to (but not including) this value
long nextMillis = timestamp.toEpochMilli() + 1;
return idGenerator.getTimeOrderedEpoch(nextMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.comet.opik.api.FeedbackScore;
import com.comet.opik.api.FeedbackScoreBatchContainer;
import com.comet.opik.api.FeedbackScoreNames;
import com.comet.opik.api.InstantToUUIDMapper;
import com.comet.opik.api.ProjectStats;
import com.comet.opik.api.Trace;
import com.comet.opik.api.Trace.TracePage;
Expand Down Expand Up @@ -80,6 +81,7 @@
import org.glassfish.jersey.server.ChunkedOutput;
import reactor.core.publisher.Flux;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -90,6 +92,7 @@
import static com.comet.opik.api.TraceThread.TraceThreadPage;
import static com.comet.opik.utils.AsyncUtils.setRequestContext;
import static com.comet.opik.utils.ValidationUtils.validateProjectNameAndProjectId;
import static com.comet.opik.utils.ValidationUtils.validateTimeRangeParameters;

@Path("/v1/private/traces")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -111,6 +114,7 @@ public class TracesResource {
private final @NonNull Streamer streamer;
private final @NonNull ProjectService projectService;
private final @NonNull TraceThreadService traceThreadService;
private final @NonNull InstantToUUIDMapper instantToUUIDMapper;

@GET
@Operation(operationId = "getTracesByProject", summary = "Get traces by project_name or project_id", description = "Get traces by project_name or project_id", responses = {
Expand All @@ -125,9 +129,12 @@ public Response getTracesByProject(
@QueryParam("truncate") @DefaultValue("false") @Schema(description = "Truncate input, output and metadata to slim payloads") boolean truncate,
@QueryParam("strip_attachments") @DefaultValue("false") @Schema(description = "If true, returns attachment references like [file.png]; if false, downloads and reinjects stripped attachments") boolean stripAttachments,
@QueryParam("sorting") String sorting,
@QueryParam("exclude") String exclude) {
@QueryParam("exclude") String exclude,
@QueryParam("from_time") @Schema(description = "Filter traces created from this time (ISO-8601 format). Must be provided together with 'to_time'.") Instant startTime,
@QueryParam("to_time") @Schema(description = "Filter traces created up to this time (ISO-8601 format). Must be provided together with 'from_time' and must be after 'from_time'.") Instant endTime) {

validateProjectNameAndProjectId(projectName, projectId);
validateTimeRangeParameters(startTime, endTime);
var traceFilters = filtersFactory.newFilters(filters, TraceFilter.LIST_TYPE_REFERENCE);
var sortingFields = traceSortingFactory.newSorting(sorting);

Expand All @@ -149,8 +156,10 @@ public Response getTracesByProject(
.filters(traceFilters)
.truncate(truncate)
.stripAttachments(stripAttachments)
.sortingFields(sortingFields)
.uuidFromTime(instantToUUIDMapper.toLowerBound(startTime))
.uuidToTime(instantToUUIDMapper.toUpperBound(endTime))
.exclude(ParamsValidator.get(exclude, Trace.TraceField.class, "exclude"))
.sortingFields(sortingFields)
.build();

log.info("Get traces by '{}' on workspaceId '{}'", searchCriteria, workspaceId);
Expand All @@ -166,7 +175,8 @@ public Response getTracesByProject(
.contextWrite(ctx -> setRequestContext(ctx, requestContext))
.block();

log.info("Found traces by '{}', count '{}' on workspaceId '{}'", searchCriteria, tracePage.size(), workspaceId);
log.info("Found traces by '{}', count '{}' on workspaceId '{}'", searchCriteria, tracePage.size(),
workspaceId);

return Response.ok(tracePage).build();
}
Expand Down Expand Up @@ -353,14 +363,20 @@ public Response deleteTraces(
@JsonView({ProjectStats.ProjectStatItem.View.Public.class})
public Response getStats(@QueryParam("project_id") UUID projectId,
@QueryParam("project_name") String projectName,
@QueryParam("filters") String filters) {
@QueryParam("filters") String filters,
@QueryParam("from_time") @Schema(description = "Filter traces created from this time (ISO-8601 format). Must be provided together with 'to_time'.") Instant startTime,
@QueryParam("to_time") @Schema(description = "Filter traces created up to this time (ISO-8601 format). Must be provided together with 'from_time' and must be after 'from_time'.") Instant endTime) {

validateProjectNameAndProjectId(projectName, projectId);
validateTimeRangeParameters(startTime, endTime);
var traceFilters = filtersFactory.newFilters(filters, TraceFilter.LIST_TYPE_REFERENCE);

var searchCriteria = TraceSearchCriteria.builder()
.projectName(projectName)
.projectId(projectId)
.filters(traceFilters)
.uuidFromTime(instantToUUIDMapper.toLowerBound(startTime))
.uuidToTime(instantToUUIDMapper.toUpperBound(endTime))
.build();

String workspaceId = requestContext.get().getWorkspaceId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
<endif>
WHERE workspace_id = :workspace_id
AND project_id = :project_id
<if(uuid_from_time)> AND id BETWEEN :uuid_from_time AND :uuid_to_time <endif>
<if(last_received_id)> AND id \\< :last_received_id <endif>
<if(filters)> AND <filters> <endif>
<if(annotation_queue_filters)> AND <annotation_queue_filters> <endif>
Expand Down Expand Up @@ -995,6 +996,7 @@ AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
<endif>
WHERE project_id = :project_id
AND workspace_id = :workspace_id
<if(uuid_from_time)> AND id BETWEEN :uuid_from_time AND :uuid_to_time <endif>
<if(filters)> AND <filters> <endif>
<if(annotation_queue_filters)> AND <annotation_queue_filters> <endif>
<if(feedback_scores_filters)>
Expand Down Expand Up @@ -1412,6 +1414,7 @@ AND notEquals(start_time, toDateTime64('1970-01-01 00:00:00.000', 9)),
<endif>
WHERE workspace_id = :workspace_id
AND project_id IN :project_ids
<if(uuid_from_time)> AND id BETWEEN :uuid_from_time AND :uuid_to_time <endif>
<if(filters)> AND <filters> <endif>
<if(annotation_queue_filters)> AND <annotation_queue_filters> <endif>
<if(feedback_scores_filters)>
Expand Down Expand Up @@ -2892,6 +2895,13 @@ private ST newFindTemplate(String query, TraceSearchCriteria traceSearchCriteria
});
Optional.ofNullable(traceSearchCriteria.lastReceivedId())
.ifPresent(lastReceivedTraceId -> template.add("last_received_id", lastReceivedTraceId));

// Add UUID bounds for time-based filtering (presence of uuid_from_time triggers the conditional)
Optional.ofNullable(traceSearchCriteria.uuidFromTime())
.ifPresent(uuid_from_time -> {
template.add("uuid_from_time", uuid_from_time);
template.add("uuid_to_time", traceSearchCriteria.uuidToTime());
});
return template;
}

Expand All @@ -2907,6 +2917,12 @@ private void bindSearchCriteria(TraceSearchCriteria traceSearchCriteria, Stateme
});
Optional.ofNullable(traceSearchCriteria.lastReceivedId())
.ifPresent(lastReceivedTraceId -> statement.bind("last_received_id", lastReceivedTraceId));

// Bind UUID BETWEEN bounds for time-based filtering
if (traceSearchCriteria.uuidFromTime() != null && traceSearchCriteria.uuidToTime() != null) {
statement.bind("uuid_from_time", traceSearchCriteria.uuidFromTime());
statement.bind("uuid_to_time", traceSearchCriteria.uuidToTime());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public record TraceSearchCriteria(
UUID lastReceivedId,
boolean truncate,
boolean stripAttachments,
Set<Trace.TraceField> exclude) {
Set<Trace.TraceField> exclude,
UUID uuidFromTime,
UUID uuidToTime) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.comet.opik.infrastructure.web;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Objects;

/**
* JAX-RS ParamConverter for automatic Instant conversion from query parameters.
* Supports both ISO-8601 format (e.g., "2024-01-01T00:00:00Z") and milliseconds since epoch.
*/
@Provider
@Slf4j
public class InstantParamConverter implements ParamConverterProvider {

private static final InstantConverter INSTANCE = new InstantConverter();

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (rawType != Instant.class) {
return null;
}

return (ParamConverter<T>) INSTANCE;
}

private static class InstantConverter implements ParamConverter<Instant> {

@Override
public Instant fromString(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}

try {
// Try parsing as ISO-8601 format first
return Instant.parse(value);
} catch (DateTimeParseException exception) {
log.debug("Failed to parse '{}' as ISO-8601, attempting to parse as milliseconds since epoch", value,
exception);
try {
// Fall back to parsing as milliseconds since epoch
long epochMillis = Long.parseLong(value);
return Instant.ofEpochMilli(epochMillis);
} catch (NumberFormatException numberFormatException) {
throw new BadRequestException(
"Invalid instant format: '%s'. Expected ISO-8601 (e.g., 2024-01-01T00:00:00Z) or milliseconds since epoch"
.formatted(value));
}
}
}

@Override
public String toString(Instant value) {
return Objects.toString(value, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.ws.rs.BadRequestException;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.util.UUID;

public class ValidationUtils {
Expand Down Expand Up @@ -49,4 +50,19 @@ public static void validateProjectNameAndProjectId(String projectName, UUID proj
throw new BadRequestException("Either 'project_name' or 'project_id' query params must be provided");
}
}

public static void validateTimeRangeParameters(Instant startTime, Instant endTime) {
boolean startTimePresent = startTime != null;
boolean endTimePresent = endTime != null;

if (startTimePresent != endTimePresent) {
throw new BadRequestException(
"Both 'from_time' and 'to_time' parameters must be provided together, or both must be omitted");
}

if (startTimePresent && endTimePresent && !startTime.isBefore(endTime)) {
throw new BadRequestException(
"Parameter 'from_time' must be before 'to_time'");
}
}
}
Loading