Skip to content

Commit 3a72f0f

Browse files
idoberko2andrescrz
andauthored
[OPIK-1354] add span type guardrail (#1726)
* OPIK-1354 add span type guardrail * OPIK-1354 removed rollback * OPIK-1354 rename migration script * Updated migration changeset to 16 * Fix in 16 version --------- Co-authored-by: Andres Cruz <[email protected]>
1 parent f111f96 commit 3a72f0f

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

apps/opik-backend/src/main/java/com/comet/opik/domain/SpanDAO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ INSERT INTO spans(
240240
:trace_id as trace_id,
241241
:parent_span_id as parent_span_id,
242242
:name as name,
243-
CAST(:type, 'Enum8(\\'unknown\\' = 0 , \\'general\\' = 1, \\'tool\\' = 2, \\'llm\\' = 3)') as type,
243+
CAST(:type, 'Enum8(\\'unknown\\' = 0 , \\'general\\' = 1, \\'tool\\' = 2, \\'llm\\' = 3, \\'guardrail\\' = 4)') as type,
244244
parseDateTime64BestEffort(:start_time, 9) as start_time,
245245
<if(end_time)> parseDateTime64BestEffort(:end_time, 9) as end_time, <else> null as end_time, <endif>
246246
:input as input,

apps/opik-backend/src/main/java/com/comet/opik/domain/SpanType.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public enum SpanType {
77
general,
88
tool,
99
llm,
10+
guardrail,
1011
;
1112

1213
public static SpanType fromString(String value) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--liquibase formatted sql
2+
--changeset idoberko2:000016_guardrails_span_type
3+
4+
5+
ALTER TABLE ${ANALYTICS_DB_DATABASE_NAME}.spans MODIFY COLUMN type Enum8(
6+
'unknown' = 0 ,
7+
'general' = 1,
8+
'tool' = 2,
9+
'llm' = 3,
10+
'guardrail' = 4);
11+

apps/opik-backend/src/test/java/com/comet/opik/api/resources/v1/priv/SpansResourceTest.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import java.time.Instant;
109109
import java.time.temporal.ChronoUnit;
110110
import java.util.ArrayList;
111+
import java.util.Arrays;
111112
import java.util.Comparator;
112113
import java.util.LinkedHashMap;
113114
import java.util.List;
@@ -1362,8 +1363,9 @@ void createAndGetByProjectNameAndTraceId() {
13621363
List.of());
13631364
}
13641365

1365-
@Test
1366-
void createAndGetByProjectIdAndTraceIdAndType() {
1366+
@ParameterizedTest
1367+
@EnumSource(SpanType.class)
1368+
void createAndGetByProjectIdAndTraceIdAndType(SpanType expectedType) {
13671369
String workspaceName = UUID.randomUUID().toString();
13681370
String workspaceId = UUID.randomUUID().toString();
13691371
String apiKey = UUID.randomUUID().toString();
@@ -1379,7 +1381,7 @@ void createAndGetByProjectIdAndTraceIdAndType() {
13791381
.parentSpanId(null)
13801382
.projectName(projectName)
13811383
.traceId(traceId)
1382-
.type(SpanType.llm)
1384+
.type(expectedType)
13831385
.feedbackScores(null)
13841386
.build())
13851387
.toList();
@@ -1396,7 +1398,7 @@ void createAndGetByProjectIdAndTraceIdAndType() {
13961398
.projectName(projectName)
13971399
.traceId(traceId)
13981400
.parentSpanId(null)
1399-
.type(SpanType.general)
1401+
.type(findOtherSpanType(expectedType))
14001402
.build());
14011403
unexpectedSpans.forEach(
14021404
expectedSpan -> SpansResourceTest.this.createAndAssert(expectedSpan, apiKey, workspaceName));
@@ -1406,7 +1408,7 @@ void createAndGetByProjectIdAndTraceIdAndType() {
14061408
null,
14071409
projectId,
14081410
traceId,
1409-
SpanType.llm,
1411+
expectedType,
14101412
null,
14111413
1,
14121414
pageSize,
@@ -1420,7 +1422,7 @@ void createAndGetByProjectIdAndTraceIdAndType() {
14201422
null,
14211423
projectId,
14221424
traceId,
1423-
SpanType.llm,
1425+
expectedType,
14241426
null,
14251427
2,
14261428
pageSize,
@@ -1431,6 +1433,11 @@ void createAndGetByProjectIdAndTraceIdAndType() {
14311433
List.of());
14321434
}
14331435

1436+
private SpanType findOtherSpanType(SpanType expectedType) {
1437+
return Arrays.stream(SpanType.values()).filter(type -> type != expectedType).findFirst()
1438+
.orElseThrow(() -> new IllegalArgumentException("expected to find another span type"));
1439+
}
1440+
14341441
@ParameterizedTest
14351442
@ValueSource(booleans = {true, false})
14361443
void whenUsingPagination__thenReturnTracesPaginated(boolean stream) {
@@ -6338,11 +6345,9 @@ private static int randomNumber(int minValue, int maxValue) {
63386345
class GetFeedbackScoreNames {
63396346

63406347
Stream<Arguments> getFeedbackScoreNames__whenGetFeedbackScoreNames__thenReturnFeedbackScoreNames() {
6341-
return Stream.of(
6342-
arguments(Optional.empty()),
6343-
arguments(Optional.of(SpanType.llm)),
6344-
arguments(Optional.of(SpanType.general)),
6345-
arguments(Optional.of(SpanType.tool)));
6348+
return Stream.concat(
6349+
Stream.of(arguments(Optional.empty())),
6350+
Arrays.stream(SpanType.values()).map(type -> arguments(Optional.of(type))));
63466351
}
63476352

63486353
@ParameterizedTest

0 commit comments

Comments
 (0)