Skip to content

Commit 9cf4d61

Browse files
committed
works
1 parent 01c1198 commit 9cf4d61

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

sample-app/generate_joke_workflow_example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func runJokeWorkflow() {
264264
"user_id": "user_12345",
265265
"chat_id": "chat_1234",
266266
},
267-
})
267+
}, nil)
268268
defer wf.End()
269269

270270
// Execute workflow steps

sample-app/recipe_agent_example.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ import (
77

88
"github.com/sashabaranov/go-openai"
99
sdk "github.com/traceloop/go-openllmetry/traceloop-sdk"
10+
"github.com/traceloop/go-openllmetry/traceloop-sdk/model"
1011
)
1112

1213
var associationProperties = map[string]string{
1314
"user_id": "user_67890",
14-
"ab_testing_variant": "variant_a",
15+
}
16+
17+
var abTest = &model.ABTest{
18+
VarientsKeys: map[string]bool{
19+
"variant_a": false,
20+
"variant_b": true,
21+
},
1522
}
1623

1724
// ingredientValidatorTool validates that requested ingredients are available/safe
@@ -236,7 +243,10 @@ func runRecipeAgent() {
236243
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
237244

238245
// Create standalone agent with association properties
239-
agent := traceloop.NewAgent(ctx, "recipe_generator", associationProperties)
246+
agent := traceloop.NewAgent(ctx, "recipe_generator", sdk.AgentAttributes{
247+
Name: "recipe_generator",
248+
AssociationProperties: associationProperties,
249+
}, abTest)
240250
defer agent.End()
241251

242252
// User request

sample-app/workflow_example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func workflowMain() {
2626

2727
wf := traceloop.NewWorkflow(ctx, tlp.WorkflowAttributes{
2828
Name: "history_generation",
29-
})
29+
}, nil)
3030
defer wf.End()
3131

3232
factGenTask := wf.NewTask("current_date_fact_generation")

traceloop-sdk/agent.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import (
55
"fmt"
66

77
semconvai "github.com/traceloop/go-openllmetry/semconv-ai"
8+
"github.com/traceloop/go-openllmetry/traceloop-sdk/model"
89
"go.opentelemetry.io/otel/attribute"
910
"go.opentelemetry.io/otel/trace"
10-
"github.com/traceloop/go-openllmetry/traceloop-sdk/model"
1111
)
1212

1313
type Agent struct {
1414
sdk *Traceloop
1515
workflow *Workflow
1616
ctx context.Context
1717
Attributes AgentAttributes `json:"agent_attributes"`
18+
ABTest *model.ABTest `json:"ab_test"`
1819
}
1920

2021
func (agent *Agent) End() {
@@ -70,5 +71,3 @@ func (agent *Agent) NewTool(name string, toolType string, toolFunction ToolFunct
7071
Function: toolFunction,
7172
}
7273
}
73-
74-

traceloop-sdk/model/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ const (
88
SpanKindTask SpanKind = "task"
99
SpanKindWorkflow SpanKind = "workflow"
1010
)
11+
12+
// The varient that is active will be added to the trace.
13+
type ABTest struct {
14+
VarientsKeys map[string]bool `json:"varients_keys"`
15+
}

traceloop-sdk/sdk.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (instance *Traceloop) getTracer() apitrace.Tracer {
143143
}
144144

145145
// NewAgent creates a standalone agent (without a workflow)
146-
func (instance *Traceloop) NewAgent(ctx context.Context, name string, associationProperties map[string]string) *Agent {
146+
func (instance *Traceloop) NewAgent(ctx context.Context, name string, agentAttrs AgentAttributes, abTest *model.ABTest) *Agent {
147147
aCtx, span := instance.getTracer().Start(ctx, fmt.Sprintf("%s.agent", name), apitrace.WithNewRoot())
148148

149149
attrs := []attribute.KeyValue{
@@ -152,8 +152,17 @@ func (instance *Traceloop) NewAgent(ctx context.Context, name string, associatio
152152
semconvai.LLMAgentName.String(name),
153153
}
154154

155+
if abTest != nil {
156+
for key, activeVarient := range abTest.VarientsKeys {
157+
if activeVarient {
158+
agentAttrs.AssociationProperties["ab_testing_variant"] = key
159+
break
160+
}
161+
}
162+
}
163+
155164
// Add association properties if provided
156-
for key, value := range associationProperties {
165+
for key, value := range agentAttrs.AssociationProperties {
157166
attrs = append(attrs, attribute.String("traceloop.association.properties."+key, value))
158167
}
159168

@@ -163,10 +172,8 @@ func (instance *Traceloop) NewAgent(ctx context.Context, name string, associatio
163172
sdk: instance,
164173
workflow: nil,
165174
ctx: aCtx,
166-
Attributes: AgentAttributes{
167-
Name: name,
168-
AssociationProperties: associationProperties,
169-
},
175+
Attributes: agentAttrs,
176+
ABTest: abTest,
170177
}
171178
}
172179

traceloop-sdk/workflow.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ type Workflow struct {
1414
sdk *Traceloop
1515
ctx context.Context
1616
Attributes WorkflowAttributes `json:"workflow_attributes"`
17+
ABTest *model.ABTest `json:"ab_test"`
1718
}
1819

19-
func (instance *Traceloop) NewWorkflow(ctx context.Context, attrs WorkflowAttributes) *Workflow {
20+
func (instance *Traceloop) NewWorkflow(ctx context.Context, attrs WorkflowAttributes, abTest *model.ABTest) *Workflow {
2021
wCtx, span := instance.getTracer().Start(ctx, fmt.Sprintf("%s.workflow", attrs.Name), trace.WithNewRoot())
2122

2223
span.SetAttributes(
@@ -25,6 +26,15 @@ func (instance *Traceloop) NewWorkflow(ctx context.Context, attrs WorkflowAttrib
2526
semconvai.TraceloopEntityName.String(attrs.Name),
2627
)
2728

29+
if abTest != nil {
30+
for key, activeVarient := range abTest.VarientsKeys {
31+
if activeVarient {
32+
span.SetAttributes(attribute.String("traceloop.association.properties.ab_testing_variant", key))
33+
break
34+
}
35+
}
36+
}
37+
2838
return &Workflow{
2939
sdk: instance,
3040
ctx: wCtx,
@@ -69,6 +79,13 @@ func (workflow *Workflow) NewAgent(name string, associationProperties map[string
6979
semconvai.TraceloopEntityName.String(name),
7080
}
7181

82+
if workflow.ABTest != nil {
83+
for key, activeVarient := range workflow.ABTest.VarientsKeys {
84+
if activeVarient {
85+
associationProperties["ab_testing_variant"] = key
86+
}
87+
}
88+
}
7289
// Add agent-specific association properties to the span
7390
for key, value := range associationProperties {
7491
attrs = append(attrs, attribute.String("traceloop.association.properties."+key, value))

0 commit comments

Comments
 (0)