Skip to content

Commit 14980b3

Browse files
authored
Improve ctxpropagation sample
2 parents 3291d14 + 5ee7762 commit 14980b3

File tree

7 files changed

+101
-40
lines changed

7 files changed

+101
-40
lines changed

ctxpropagation/README.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
This sample workflow demos context propagation through a workflow. Details about context propagation are
1+
This sample Workflow demos context propagation through a Workflow. Details about context propagation are
22
available [here](https://docs.temporal.io/docs/go-tracing).
33

4-
The sample workflow initializes the client with a context propagator which propagates
5-
specific information in the `context.Context` object across the workflow. The `context.Context` object is populated
6-
with the information prior to calling `StartWorkflow`. The workflow demonstrates that the information is available
7-
in the workflow and any activities executed.
4+
The sample Workflow initializes the client with a context propagator which propagates
5+
specific information in the `context.Context` object across the Workflow. The `context.Context` object is populated
6+
with the information prior to calling `StartWorkflow`. The Workflow demonstrates that the information is available
7+
in the Workflow and any activities executed.
8+
9+
Also, this sample initializes a Jaeger global tracer and pass it to the client. The sample will work without
10+
actual Jaeger instance -- just report every tracer call to the log. To see traces in Jaeger run it with follow command:
11+
```
12+
$ docker run --publish 6831:6831 --publish 16686:16686 jaegertracing/all-in-one:latest
13+
```
814

915
Steps to run this sample:
10-
1) You need a Temporal service running. See details README.md
11-
2) Run the following command multiple times on different console window. This is to simulate running workers on multiple different machines.
16+
1) You need a Temporal service running. See details README.md.
17+
2) Run
1218
```
1319
go run ctxpropagation/worker/main.go
1420
```
15-
3) Run the following command to execute the context:
21+
to start worker for `ctxpropagation` Workflow.
22+
23+
3) Run:
1624
```
1725
go run ctxpropagation/starter/main.go
1826
```
27+
to start Workflow.
28+
29+
You should see prints showing the context information available in the workflow and activities.
30+
1931

20-
You should see prints showing the context information available in the workflow
21-
and activities.

ctxpropagation/propagator.go

+13-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ctxpropagation
33
import (
44
"context"
55

6-
commonpb "go.temporal.io/api/common/v1"
76
"go.temporal.io/sdk/converter"
87
"go.temporal.io/sdk/workflow"
98
)
@@ -28,7 +27,7 @@ var PropagateKey = contextKey{}
2827

2928
// propagationKey is the key used by the propagator to pass values through the
3029
// Temporal server headers
31-
const propagationKey = "_prop"
30+
const propagationKey = "custom-header"
3231

3332
// NewContextPropagator returns a context propagator that propagates a set of
3433
// string key-value pairs across a workflow
@@ -60,34 +59,26 @@ func (s *propagator) InjectFromWorkflow(ctx workflow.Context, writer workflow.He
6059

6160
// Extract extracts values from headers and puts them into context
6261
func (s *propagator) Extract(ctx context.Context, reader workflow.HeaderReader) (context.Context, error) {
63-
if err := reader.ForEachKey(func(key string, value *commonpb.Payload) error {
64-
if key == propagationKey {
65-
var values Values
66-
if err := converter.GetDefaultDataConverter().FromPayload(value, &values); err != nil {
67-
return err
68-
}
69-
ctx = context.WithValue(ctx, PropagateKey, values)
62+
if value, ok := reader.Get(propagationKey); ok {
63+
var values Values
64+
if err := converter.GetDefaultDataConverter().FromPayload(value, &values); err != nil {
65+
return ctx, nil
7066
}
71-
return nil
72-
}); err != nil {
73-
return nil, err
67+
ctx = context.WithValue(ctx, PropagateKey, values)
7468
}
69+
7570
return ctx, nil
7671
}
7772

7873
// ExtractToWorkflow extracts values from headers and puts them into context
7974
func (s *propagator) ExtractToWorkflow(ctx workflow.Context, reader workflow.HeaderReader) (workflow.Context, error) {
80-
if err := reader.ForEachKey(func(key string, value *commonpb.Payload) error {
81-
if key == propagationKey {
82-
var values Values
83-
if err := converter.GetDefaultDataConverter().FromPayload(value, &values); err != nil {
84-
return err
85-
}
86-
ctx = workflow.WithValue(ctx, PropagateKey, values)
75+
if value, ok := reader.Get(propagationKey); ok {
76+
var values Values
77+
if err := converter.GetDefaultDataConverter().FromPayload(value, &values); err != nil {
78+
return ctx, nil
8779
}
88-
return nil
89-
}); err != nil {
90-
return nil, err
80+
ctx = workflow.WithValue(ctx, PropagateKey, values)
9181
}
82+
9283
return ctx, nil
9384
}

ctxpropagation/starter/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66

7+
"github.com/opentracing/opentracing-go"
78
"github.com/pborman/uuid"
89
"go.temporal.io/sdk/client"
910
"go.temporal.io/sdk/workflow"
@@ -12,9 +13,14 @@ import (
1213
)
1314

1415
func main() {
16+
// Set tracer which will be returned by opentracing.GlobalTracer().
17+
closer := ctxpropagation.SetJaegerGlobalTracer()
18+
defer func() { _ = closer.Close() }()
19+
1520
// The client is a heavyweight object that should be created once per process.
1621
c, err := client.NewClient(client.Options{
1722
HostPort: client.DefaultHostPort,
23+
Tracer: opentracing.GlobalTracer(),
1824
ContextPropagators: []workflow.ContextPropagator{ctxpropagation.NewContextPropagator()},
1925
})
2026
if err != nil {

ctxpropagation/tracer.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ctxpropagation
2+
3+
import (
4+
"io"
5+
6+
"github.com/opentracing/opentracing-go"
7+
"github.com/uber/jaeger-client-go"
8+
"github.com/uber/jaeger-client-go/config"
9+
)
10+
11+
func SetJaegerGlobalTracer() io.Closer {
12+
cfg := config.Configuration{
13+
ServiceName: "ctx-propogation-sample",
14+
Sampler: &config.SamplerConfig{
15+
Type: jaeger.SamplerTypeConst,
16+
Param: 1,
17+
},
18+
Reporter: &config.ReporterConfig{
19+
LogSpans: true,
20+
},
21+
}
22+
tracer, closer, err := cfg.NewTracer(
23+
config.Logger(jaeger.StdLogger),
24+
)
25+
if err != nil {
26+
panic(err)
27+
}
28+
opentracing.SetGlobalTracer(tracer)
29+
30+
return closer
31+
}

ctxpropagation/worker/main.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"log"
55

6+
"github.com/opentracing/opentracing-go"
67
"go.temporal.io/sdk/client"
78
"go.temporal.io/sdk/worker"
89
"go.temporal.io/sdk/workflow"
@@ -11,12 +12,15 @@ import (
1112
)
1213

1314
func main() {
15+
// Set tracer which will be returned by opentracing.GlobalTracer().
16+
closer := ctxpropagation.SetJaegerGlobalTracer()
17+
defer func() { _ = closer.Close() }()
18+
1419
// The client and worker are heavyweight objects that should be created once per process.
1520
c, err := client.NewClient(client.Options{
16-
HostPort: client.DefaultHostPort,
17-
ContextPropagators: []workflow.ContextPropagator{
18-
ctxpropagation.NewContextPropagator(),
19-
},
21+
HostPort: client.DefaultHostPort,
22+
ContextPropagators: []workflow.ContextPropagator{ctxpropagation.NewContextPropagator()},
23+
Tracer: opentracing.GlobalTracer(),
2024
})
2125
if err != nil {
2226
log.Fatalln("Unable to create client", err)

go.mod

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ module github.com/temporalio/samples-go
33
go 1.14
44

55
require (
6+
github.com/HdrHistogram/hdrhistogram-go v0.9.0 // indirect
67
github.com/golang/mock v1.4.4
78
github.com/google/uuid v1.1.2 // indirect
8-
github.com/opentracing/opentracing-go v1.2.0 // indirect
9+
github.com/opentracing/opentracing-go v1.2.0
910
github.com/pborman/uuid v1.2.1
1011
github.com/stretchr/objx v0.3.0 // indirect
1112
github.com/stretchr/testify v1.6.1
13+
github.com/uber/jaeger-client-go v2.25.0+incompatible
14+
github.com/uber/jaeger-lib v2.4.0+incompatible // indirect
1215
go.temporal.io/api v1.0.0
13-
go.temporal.io/sdk v1.0.0
16+
go.temporal.io/sdk v1.1.0
1417
go.uber.org/atomic v1.7.0 // indirect
18+
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 // indirect
19+
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf // indirect
1520
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
21+
google.golang.org/genproto v0.0.0-20201006033701-bcad7cf615f2 // indirect
1622
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
1723
)

go.sum

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4+
github.com/HdrHistogram/hdrhistogram-go v0.9.0 h1:dpujRju0R4M/QZzcnR1LH1qm+TVG3UzkWdp5tH1WMcg=
5+
github.com/HdrHistogram/hdrhistogram-go v0.9.0/go.mod h1:nxrse8/Tzg2tg3DZcZjm6qEclQKK70g0KxO61gFFZD4=
46
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
57
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
68
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
@@ -88,13 +90,17 @@ github.com/uber-go/tally v3.3.17+incompatible h1:nFHIuW3VQ22wItiE9kPXic8dEgExWOs
8890
github.com/uber-go/tally v3.3.17+incompatible/go.mod h1:YDTIBxdXyOU/sCWilKB4bgyufu1cEi0jdVnRdxvjnmU=
8991
github.com/uber/jaeger-client-go v2.23.1+incompatible h1:uArBYHQR0HqLFFAypI7RsWTzPSj/bDpmZZuQjMLSg1A=
9092
github.com/uber/jaeger-client-go v2.23.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
93+
github.com/uber/jaeger-client-go v2.25.0+incompatible h1:IxcNZ7WRY1Y3G4poYlx24szfsn/3LvK9QHCq9oQw8+U=
94+
github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
9195
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
9296
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
97+
github.com/uber/jaeger-lib v2.4.0+incompatible h1:fY7QsGQWiCt8pajv4r7JEvmATdCVaWxXbjwyYwsNaLQ=
98+
github.com/uber/jaeger-lib v2.4.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
9399
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
94100
go.temporal.io/api v1.0.0 h1:mWtvS+5ENYvG4ZPZ/4/bxCj4j3gIF4D05C2GVrhLpjc=
95101
go.temporal.io/api v1.0.0/go.mod h1:AgbKINgV3KR9SlTH8nQRsNadVbxVI+/LnZ1uFModMIA=
96-
go.temporal.io/sdk v1.0.0 h1:Cfrr/RkcoGu+B/vpOII1D7xv4rhRHa5eYQStnuASS7k=
97-
go.temporal.io/sdk v1.0.0/go.mod h1:mtuFg8AafvaXeAE3gwBjZZN05uwTGjzVf1qkXmq4hpM=
102+
go.temporal.io/sdk v1.1.0 h1:qhrjW0Z2cxN1DVpAX0D+krnmoFOS4bQJ4aCrp231HV8=
103+
go.temporal.io/sdk v1.1.0/go.mod h1:3MLsVXjrvQ9z4XaxRie5OOJdiZ/Fu2v3fDCVcAqssEs=
98104
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
99105
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
100106
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
@@ -121,6 +127,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
121127
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
122128
golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 h1:lleNcKRbcaC8MqgLwghIkzZ2JBQAb7QQ9MiwRt1BisA=
123129
golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
130+
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 h1:wBouT66WTYFXdxfVdz9sVWARVd/2vfGcmI45D2gj45M=
131+
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
124132
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
125133
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
126134
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -133,6 +141,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
133141
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
134142
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d h1:L/IKR6COd7ubZrs2oTnTi73IhgqJ71c9s80WsQnh0Es=
135143
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
144+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
145+
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf h1:Bg47KQy0JhTHuf4sLiQwTMKwUMfSDwgSGatrxGR7nLM=
146+
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
136147
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
137148
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
138149
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -165,6 +176,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
165176
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
166177
google.golang.org/genproto v0.0.0-20200925023002-c2d885f95484 h1:Rr9EZdYRq2WLckzJQVtN3ISKoP7dvgwi7jbglILNZ34=
167178
google.golang.org/genproto v0.0.0-20200925023002-c2d885f95484/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
179+
google.golang.org/genproto v0.0.0-20201006033701-bcad7cf615f2 h1:rBG1miiV00OG3NTbgxg42kPWohjVNn1sGhJyt4xyPLQ=
180+
google.golang.org/genproto v0.0.0-20201006033701-bcad7cf615f2/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
168181
google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
169182
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
170183
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=

0 commit comments

Comments
 (0)