Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Context Propagation @ OTEP 66 #381

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Checkpoint
  • Loading branch information
jmacd committed Dec 10, 2019
commit 33bfd7298a6b590ba6fb9f14b63e3723a791aaeb
103 changes: 103 additions & 0 deletions api/context/propagation/propagation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package propagation

import (
"context"
"sync/atomic"

"go.opentelemetry.io/otel/api/core"
)

// HTTPSupplier is implemented by http.Headers.
type HTTPSupplier interface {
Get(key string) string
Set(key string, value string)
}

type HTTPExtractor interface {
// Extract method retrieves encoded SpanContext using supplier
// from the associated carrier. It decodes the SpanContext
// and returns it and a dctx of correlated context. If no
// SpanContext was retrieved OR if the retrieved SpanContext
// is invalid then an empty SpanContext is returned.
Extract(context.Context, HTTPSupplier) (core.SpanContext, dctx.Map)
}

type HTTPInjector interface {
// Inject method retrieves current SpanContext from the ctx,
// encodes it into propagator specific format and then injects
// the encoded SpanContext using supplier into a carrier
// associated with the supplier. It also takes a
// correlationCtx whose values will be injected into a carrier
// using the supplier.
Inject(context.Context, HTTPSupplier) context.Context
}

type Propagators interface {
// HTTP propagation
SetHTTPExtractors(...HTTPExtractor)
SetHTTPInjectors(...HTTPInjector)
HTTPExtractors() []HTTPExtractor
HTTPInjectors() []HTTPInjector

// Binary propagation
// TODO
}

type propagators struct {
// TODO Nah. Use an options pattern to avoid mutation.

httpEx atomic.Value // []HTTPExtractor
httpIn atomic.Value // []HTTPInjector
}

func New() Propagators {
p := &propagators{}

p.httpIn.Store([]HTTPInjector{})
p.httpEs.Store([]HTTPExtractor{})

return p
}

func (p *Propagators) SetHTTPExtractors(ex ...HTTPExtractor) {
if ex == nil {
ex = []HTTPExtractor{}
}
p.httpEx.Store(ex)
}

func (p *Propagators) SetHTTPInjectors(in ...HTTPInjector) {
if in == nil {
in = []HTTPInjector{}
}
p.httpIn.Store(in)
}

func (p *Propagators) HTTPExtractors() []HTTPExtractor {
return p.httpEx.Load().([]HTTPExtractor)
}

func (p *Propagators) HTTPInjectors() []HTTPInjector {
return p.httpIn.Load().([]HTTPInjector)
}

type NoopPropagators struct{}

func (NoopPropagators) SetHTTPExtractors(ex ...HTTPExtractor) {}
func (NoopPropagators) SetHTTPInjectors(in ...HTTPInjector) {}
func (NoopPropagators) HTTPExtractors() []HTTPExtractor { return nil }
func (NoopPropagators) HTTPInjectors() []HTTPInjector { return nil }
25 changes: 23 additions & 2 deletions api/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package global
import (
"sync/atomic"

"go.opentelemetry.io/otel/api/context/propagation"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/api/trace"
)
Expand All @@ -29,11 +30,16 @@ type (
meterProvider struct {
mp metric.Provider
}

propagators struct {
pr propagation.Propagators
}
)

var (
globalTracer atomic.Value
globalMeter atomic.Value
globalTracer atomic.Value
globalMeter atomic.Value
globalPropagators atomic.Value
)

// TraceProvider returns the registered global trace provider.
Expand Down Expand Up @@ -67,3 +73,18 @@ func MeterProvider() metric.Provider {
func SetMeterProvider(mp metric.Provider) {
globalMeter.Store(meterProvider{mp: mp})
}

// Propagators returns the registered global propagators instance. If
// none is registered then an instance of propagators.NoopPropagators
// is returned.
func Propagators() propagation.Propagators {
if gp := globalPropagators.Load(); gp != nil {
return gp.(propagators).prop
}
return propagation.NoopPropagators{}
}

// SetPropagators registers `p` as the global propagators instance.
func SetPropagators(p Propagators) {
globalPropagators.Store(propatators{prop: p})
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opentelemetry.io v0.1.0 h1:EANZoRCOP+A3faIlw/iN6YEWoYb1vleZRKm1EvH8T48=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
Expand Down