Skip to content

Commit b2d9205

Browse files
authored
Merge pull request #103 from cschleiden/document-tracing
Make tracer available to workflows and document
2 parents af45f61 + c8dc622 commit b2d9205

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,40 @@ logger := activity.Logger(ctx)
536536
537537
The returned `logger` implements the `Logger` interface, and already has the id of the activity, and the workflow instance and execution IDs set as default fields.
538538
539+
### Tracing
540+
541+
The library supports tracing via [OpenTelemetry](https://opentelemetry.io/). When you pass a `TracerProvider` when creating a backend instance, workflow execution will be traced. You can also add additional spans for both activities and workflows.
542+
543+
_Note: the support is considered experimental right now, if you decide to use it, please leave feedback_
544+
545+
#### Activities
546+
547+
The `context.Context` passed into activities is set up with the correct current span. If you create additional spans, they'll show up under the `ActivityTaskExecution`:
548+
549+
```go
550+
func Activity1(ctx context.Context, a, b int) (int, error) {
551+
ctx, span := otel.Tracer("activity1").Start(ctx, "Custom Activity1 span")
552+
defer span.End()
553+
554+
// Do something
555+
}
556+
```
557+
558+
#### Workflows
559+
560+
For workflows the usage is a bit different, the tracer needs to be aware of whether the workflow is being replayed or not:
561+
562+
```go
563+
func Workflow(ctx workflow.Context) error {
564+
ctx, span := workflow.Tracer(ctx).Start(ctx, "Workflow1 span", trace.WithAttributes(
565+
// Add additional
566+
attribute.String("msg", "hello world"),
567+
))
568+
569+
// Do something
570+
571+
span.End()
572+
```
539573

540574
## Tools
541575

samples/tracing/workflow.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/cschleiden/go-workflows/activity"
88
"github.com/cschleiden/go-workflows/workflow"
99
"go.opentelemetry.io/otel"
10+
"go.opentelemetry.io/otel/attribute"
11+
"go.opentelemetry.io/otel/trace"
1012
)
1113

1214
type Inputs struct {
@@ -19,6 +21,15 @@ func Workflow1(ctx workflow.Context, msg string, times int, inputs Inputs) (int,
1921
logger.Debug("Entering Workflow1", "msg", msg, "times", times, "inputs", inputs)
2022
defer logger.Debug("Leaving Workflow1")
2123

24+
ctx, span := workflow.Tracer(ctx).Start(ctx, "Workflow1 span", trace.WithAttributes(
25+
// Add additional
26+
attribute.String("msg", "hello world"),
27+
))
28+
29+
// Do something
30+
31+
span.End()
32+
2233
workflow.ExecuteActivity[int](ctx, workflow.DefaultActivityOptions, Activity1, 35, 12).Get(ctx)
2334

2435
workflow.Sleep(ctx, time.Second*1)

workflow/tracer.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package workflow
2+
3+
import (
4+
"github.com/cschleiden/go-workflows/internal/workflowtracer"
5+
"go.opentelemetry.io/otel/trace"
6+
)
7+
8+
type Span interface {
9+
End()
10+
}
11+
12+
type WorkflowTracer interface {
13+
Start(ctx Context, name string, opts ...trace.SpanStartOption) (Context, Span)
14+
}
15+
16+
func Tracer(ctx Context) WorkflowTracer {
17+
return &workflowTracer{
18+
t: workflowtracer.Tracer(ctx),
19+
}
20+
}
21+
22+
type workflowTracer struct {
23+
t *workflowtracer.WorkflowTracer
24+
}
25+
26+
func (wt *workflowTracer) Start(ctx Context, name string, opts ...trace.SpanStartOption) (Context, Span) {
27+
ctx, span := wt.t.Start(ctx, name, opts...)
28+
29+
return ctx, &span
30+
}

0 commit comments

Comments
 (0)