Skip to content

Commit f852068

Browse files
authored
Merge pull request #141 from openzipkin-contrib/refactor_propagation
refactoring of propagation handling
2 parents 96684ea + 0694388 commit f852068

File tree

12 files changed

+254
-254
lines changed

12 files changed

+254
-254
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

appveyor.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ version: v1.0.0.{build}
22

33
platform: x64
44

5-
clone_folder: c:\gopath\src\github.com\openzipkin\zipkin-go
5+
clone_folder: c:\gopath\src\github.com\openzipkin-contrib\zipkin-go-opentracing
66

77
environment:
88
GOPATH: c:\gopath
99
GO111MODULE: on
1010
GOFLAGS: -mod=readonly
1111

1212
install:
13-
- choco install rabbitmq --ignoredependencies -y
14-
- echo %PATH%
15-
- echo %GOPATH%
1613
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
1714
- go version
1815
- go env

bench_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"math/rand"
77
"net/http"
8-
"sync"
98
"testing"
109
"time"
1110

@@ -19,24 +18,13 @@ import (
1918
var tags []string
2019

2120
func init() {
21+
rand.Seed(time.Now().UnixNano())
2222
tags = make([]string, 1000)
2323
for j := 0; j < len(tags); j++ {
24-
tags[j] = fmt.Sprintf("%d", randomID())
24+
tags[j] = fmt.Sprintf("%d", rand.Uint64())
2525
}
2626
}
2727

28-
var (
29-
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
30-
// The golang rand generators are *not* intrinsically thread-safe.
31-
seededIDLock sync.Mutex
32-
)
33-
34-
func randomID() uint64 {
35-
seededIDLock.Lock()
36-
defer seededIDLock.Unlock()
37-
return uint64(seededIDGen.Int63())
38-
}
39-
4028
func executeOps(sp opentracing.Span, numEvent, numTag, numItems int) {
4129
for j := 0; j < numEvent; j++ {
4230
sp.LogEvent("event")

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2
22
jobs:
33
build:
4-
working_directory: /go/src/github.com/openzipkin/zipkin-go
4+
working_directory: /go/src/github.com/openzipkin-contrib/zipkin-go-opentracing
55
parallelism: 1
66
docker:
77
- image: circleci/golang

context.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
)
66

77
// SpanContext holds the basic Span metadata.
8-
type spanContextImpl struct {
9-
zipkinContext model.SpanContext
10-
}
8+
type SpanContext model.SpanContext
119

1210
// ForeachBaggageItem belongs to the opentracing.SpanContext interface
13-
func (c *spanContextImpl) ForeachBaggageItem(handler func(k, v string) bool) {
14-
}
11+
func (c SpanContext) ForeachBaggageItem(handler func(k, v string) bool) {}

propagation.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2019 The OpenZipkin Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zipkintracer
16+
17+
import (
18+
"net/http"
19+
20+
opentracing "github.com/opentracing/opentracing-go"
21+
"github.com/openzipkin/zipkin-go/model"
22+
"github.com/openzipkin/zipkin-go/propagation"
23+
"github.com/openzipkin/zipkin-go/propagation/b3"
24+
)
25+
26+
// DelegatingCarrier is a flexible carrier interface which can be implemented
27+
// by types which have a means of storing the trace metadata and already know
28+
// how to serialize themselves
29+
type DelegatingCarrier interface {
30+
State() (model.SpanContext, error)
31+
SetState(model.SpanContext) error
32+
}
33+
34+
type textMapPropagator struct {
35+
tracer *tracerImpl
36+
}
37+
38+
func (p *textMapPropagator) Inject(
39+
spanContext opentracing.SpanContext,
40+
opaqueCarrier interface{},
41+
) error {
42+
sc, ok := spanContext.(SpanContext)
43+
if !ok {
44+
return opentracing.ErrInvalidSpanContext
45+
}
46+
// native zipkin-go injector
47+
if injector, ok := opaqueCarrier.(propagation.Injector); ok {
48+
return injector(model.SpanContext(sc))
49+
}
50+
// fallback to support native opentracing http carrier
51+
if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok {
52+
req := &http.Request{Header: http.Header(httpCarrier)}
53+
switch p.tracer.opts.b3InjectOpt {
54+
case B3InjectSingle:
55+
return b3.InjectHTTP(req, b3.WithSingleHeaderOnly())(model.SpanContext(sc))
56+
case B3InjectBoth:
57+
return b3.InjectHTTP(req, b3.WithSingleAndMultiHeader())(model.SpanContext(sc))
58+
default:
59+
return b3.InjectHTTP(req)(model.SpanContext(sc))
60+
}
61+
}
62+
63+
return opentracing.ErrInvalidCarrier
64+
}
65+
66+
func (p *textMapPropagator) Extract(
67+
opaqueCarrier interface{},
68+
) (opentracing.SpanContext, error) {
69+
if extractor, ok := opaqueCarrier.(propagation.Extractor); ok {
70+
sc, err := extractor()
71+
if sc != nil {
72+
return SpanContext(*sc), err
73+
}
74+
return SpanContext{}, err
75+
}
76+
if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok {
77+
req := &http.Request{Header: http.Header(httpCarrier)}
78+
sc, err := b3.ExtractHTTP(req)()
79+
if sc != nil {
80+
return SpanContext(*sc), err
81+
}
82+
return SpanContext{}, err
83+
}
84+
return nil, opentracing.ErrUnsupportedFormat
85+
}
86+
87+
type accessorPropagator struct {
88+
tracer *tracerImpl
89+
}
90+
91+
func (p *accessorPropagator) Inject(
92+
spanContext opentracing.SpanContext,
93+
opaqueCarrier interface{},
94+
) error {
95+
dc, ok := opaqueCarrier.(DelegatingCarrier)
96+
if !ok || dc == nil {
97+
return opentracing.ErrInvalidCarrier
98+
}
99+
sc, ok := spanContext.(SpanContext)
100+
if !ok {
101+
return opentracing.ErrInvalidSpanContext
102+
}
103+
return dc.SetState(model.SpanContext(sc))
104+
}
105+
106+
func (p *accessorPropagator) Extract(
107+
opaqueCarrier interface{},
108+
) (opentracing.SpanContext, error) {
109+
dc, ok := opaqueCarrier.(DelegatingCarrier)
110+
if !ok || dc == nil {
111+
return nil, opentracing.ErrInvalidCarrier
112+
}
113+
114+
sc, err := dc.State()
115+
return SpanContext(sc), err
116+
}

propagation/http/http.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)