-
Notifications
You must be signed in to change notification settings - Fork 202
/
opentelemetry.go
59 lines (44 loc) · 1.37 KB
/
opentelemetry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package opentelemetry
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"
)
var tracer trace.Tracer
func init() {
// Name the tracer after the package, or the service if you are in main
tracer = otel.Tracer("github.com/temporalio/samples-go/otel")
}
func Workflow(ctx workflow.Context, name string) error {
logger := workflow.GetLogger(ctx)
logger.Info("HelloWorld workflow started", "name", name)
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
})
err := workflow.ExecuteActivity(ctx, Activity).Get(ctx, nil)
if err != nil {
logger.Error("Activity failed.", "Error", err)
return err
}
logger.Info("HelloWorld workflow completed.")
return nil
}
func Activity(ctx context.Context, name string) error {
logger := activity.GetLogger(ctx)
logger.Info("Activity", "name", name)
// Get current span and add new attributes
span := trace.SpanFromContext(ctx)
span.SetAttributes(attribute.Bool("isTrue", true), attribute.String("stringAttr", "Ciao"))
// Create a child span
_, childSpan := tracer.Start(ctx, "custom-span")
time.Sleep(1 * time.Second)
childSpan.End()
time.Sleep(1 * time.Second)
// Add an event to the current span
span.AddEvent("Done Activity")
return nil
}