Skip to content

Commit

Permalink
Move Version function and code from global to toplevel (#1330)
Browse files Browse the repository at this point in the history
* Move global code to toplevel package

* Move version function to toplevel package

* Update changelog

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
krnowak and MrAlias committed Nov 16, 2020
1 parent 20c78fe commit 3d6e8b5
Show file tree
Hide file tree
Showing 57 changed files with 232 additions and 364 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Move the `Number` type (together with related functions) from `go.opentelemetry.io/otel/api/metric` package into `go.opentelemetry.io/otel/metric/number` as a part of #1303. (#1316)
- The function signature of the Span `AddEvent` method in `go.opentelemetry.io/otel` is updated to no longer take an unused context and instead take a required name and a variable number of `EventOption`s. (#1254)
- The function signature of the Span `RecordError` method in `go.opentelemetry.io/otel` is updated to no longer take an unused context and instead take a required error value and a variable number of `EventOption`s. (#1254)
- Move the `go.opentelemetry.io/otel/api/global` package to `go.opentelemetry.io/otel/global`. (#1262)
- Move the `go.opentelemetry.io/otel/api/global` package to `go.opentelemetry.io/otel`. (#1262) (#1330)
- Move the `Version` function from `go.opentelemetry.io/otel/sdk` to `go.opentelemetry.io/otel`. (#1330)
- Rename correlation context header from `"otcorrelations"` to `"baggage"` to match the OpenTelemetry specification. (#1267)
- Fix `Code.UnmarshalJSON` to work with valid json only. (#1276)
- The `resource.New()` method changes signature to support builtin attributes and functional options, including `telemetry.sdk.*` and
Expand Down
10 changes: 5 additions & 5 deletions bridge/opencensus/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

octrace "go.opencensus.io/trace"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ func convertStartOptions(optFns []octrace.StartOption, name string) []trace.Span
}

if ocOpts.Sampler != nil {
global.Handle(fmt.Errorf("ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler", name))
otel.Handle(fmt.Errorf("ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler", name))
}
return otOpts
}
Expand All @@ -81,7 +81,7 @@ func (o *otelTracer) NewContext(parent context.Context, s *octrace.Span) context
if otSpan, ok := s.Internal().(*span); ok {
return trace.ContextWithSpan(parent, otSpan.otSpan)
}
global.Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String()))
otel.Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String()))
return parent
}

Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *span) AddMessageReceiveEvent(messageID, uncompressedByteSize, compresse
}

func (s *span) AddLink(l octrace.Link) {
global.Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String()))
otel.Handle(fmt.Errorf("ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation", l, s.String()))
}

func (s *span) String() string {
Expand All @@ -190,7 +190,7 @@ func (s *span) String() string {

func otelSpanContextToOc(sc trace.SpanContext) octrace.SpanContext {
if sc.IsDebug() || sc.IsDeferred() {
global.Handle(fmt.Errorf("ignoring OpenTelemetry Debug or Deferred trace flags for span %q because they are not supported by OpenCensus", sc.SpanID))
otel.Handle(fmt.Errorf("ignoring OpenTelemetry Debug or Deferred trace flags for span %q because they are not supported by OpenCensus", sc.SpanID))
}
var to octrace.TraceOptions
if sc.IsSampled() {
Expand Down
4 changes: 2 additions & 2 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
otext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/bridge/opentracing/migration"
"go.opentelemetry.io/otel/codes"
otelglobal "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/internal/trace/noop"
otelparent "go.opentelemetry.io/otel/internal/trace/parent"
Expand Down Expand Up @@ -654,5 +654,5 @@ func (t *BridgeTracer) getPropagator() propagation.TextMapPropagator {
if t.propagator != nil {
return t.propagator
}
return otelglobal.TextMapPropagator()
return otel.GetTextMapPropagator()
}
10 changes: 5 additions & 5 deletions bridge/opentracing/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

ot "github.com/opentracing/opentracing-go"

otelglobal "go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel"
otelbaggage "go.opentelemetry.io/otel/internal/baggage"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestMixedAPIs(t *testing.T) {
t.Log(msg)
})

otelglobal.SetTracerProvider(otelProvider)
otel.SetTracerProvider(otelProvider)
ot.SetGlobalTracer(otTracer)

tc.setup(t, mockOtelTracer)
Expand Down Expand Up @@ -440,7 +440,7 @@ func (tm *tracerMessTest) setup(t *testing.T, tracer *internal.MockTracer) {

func (tm *tracerMessTest) check(t *testing.T, tracer *internal.MockTracer) {
globalOtTracer := ot.GlobalTracer()
globalOtelTracer := otelglobal.Tracer("")
globalOtelTracer := otel.Tracer("")
if len(tm.recordedOTSpanTracers) != 3 {
t.Errorf("Expected 3 recorded OpenTracing tracers from spans, got %d", len(tm.recordedOTSpanTracers))
}
Expand Down Expand Up @@ -684,7 +684,7 @@ func min(a, b int) int {
}

func runOtelOTOtel(t *testing.T, ctx context.Context, name string, callback func(*testing.T, context.Context) context.Context) {
tr := otelglobal.Tracer("")
tr := otel.Tracer("")
ctx, span := tr.Start(ctx, fmt.Sprintf("%s_Otel_OTOtel", name), trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
ctx = callback(t, ctx)
Expand All @@ -701,7 +701,7 @@ func runOtelOTOtel(t *testing.T, ctx context.Context, name string, callback func
}

func runOTOtelOT(t *testing.T, ctx context.Context, name string, callback func(*testing.T, context.Context) context.Context) {
tr := otelglobal.Tracer("")
tr := otel.Tracer("")
span, ctx := ot.StartSpanFromContext(ctx, fmt.Sprintf("%s_OT_OtelOT", name), ot.Tag{Key: "span.kind", Value: "client"})
defer span.Finish()
ctx = callback(t, ctx)
Expand Down
113 changes: 5 additions & 108 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Package otel provides an implementation of the OpenTelemetry API.
This package is currently in a pre-GA phase. Backwards incompatible changes
may be introduced in subsequent minor version releases as we work to track the
evolving OpenTelemetry specification and user feedback.
The provided API is used to instrument code and measure data about that code's
performance and operation. The measured data, by default, is not processed or
transmitted anywhere. An implementation of the OpenTelemetry SDK, like the
default SDK implementation (go.opentelemetry.io/otel/sdk), and associated
exporters are used to process and transport this data.
Tracing
To participate in distributed traces a Span needs to be created for the
operation being performed as part of a traced workflow. It its simplest form:
var tracer otel.Tracer
func init() {
tracer = global.Tracer("instrumentation/package/name")
}
func operation(ctx context.Context) {
var span trace.Span
ctx, span = tracer.Start(ctx, "operation")
defer span.End()
// ...
}
A Tracer is unique to the instrumentation and is used to create Spans.
Instrumentation should be designed to accept a TracerProvider from which it
can create its own unique Tracer. Alternatively, the registered global
TracerProvider from the go.opentelemetry.io/otel/global package can be used as
a default.
const (
name = "instrumentation/package/name"
version = "0.1.0"
)
type Instrumentation struct {
tracer otel.Tracer
}
func NewInstrumentation(tp otel.TracerProvider) *Instrumentation {
if tp == nil {
tp := global.TracerProvider()
}
return &Instrumentation{
tracer: tp.Tracer(name, otel.WithTracerVersion(version)),
}
}
func operation(ctx context.Context, inst *Instrumentation) {
var span trace.Span
ctx, span = inst.tracer.Start(ctx, "operation")
defer span.End()
// ...
}
Metric Measurements
Measurements can be made about an operation being performed or the state of a
system in general. These measurements can be crucial to the reliable operation
of code and provide valuable insights about the inner workings of a system.
Measurements are made using instruments provided by this package. The type of
instrument used will depend on the type of measurement being made and of what
part of a system is being measured.
Instruments are categorized as Synchronous or Asynchronous and independently
as Adding or Grouping. Synchronous instruments are called by the user with a
Context. Asynchronous instruments are called by the SDK during collection.
Additive instruments are semantically intended for capturing a sum. Grouping
instruments are intended for capturing a distribution.
Additive instruments may be monotonic, in which case they are non-decreasing
and naturally define a rate.
The synchronous instrument names are:
Counter: additive, monotonic
UpDownCounter: additive
ValueRecorder: grouping
and the asynchronous instruments are:
SumObserver: additive, monotonic
UpDownSumObserver: additive
ValueObserver: grouping
All instruments are provided with support for either float64 or int64 input
values.
An instrument is created using a Meter. Additionally, a Meter is used to
record batches of synchronous measurements or asynchronous observations. A
Meter is obtained using a MeterProvider. A Meter, like a Tracer, is unique to
the instrumentation it instruments and must be named and versioned when
created with a MeterProvider with the name and version of the instrumentation
library.
Instrumentation should be designed to accept a MeterProvider from which it can
create its own unique Meter. Alternatively, the registered global
MeterProvider from the go.opentelemetry.io/otel/api/global package can be used
as a default.
*/
// Package global provides global providers, propagators and more.
//
// This package is currently in a pre-GA phase. Backwards incompatible changes
// may be introduced in subsequent minor version releases as we work to track
// the evolving OpenTelemetry specification and user feedback.
package otel // import "go.opentelemetry.io/otel"
12 changes: 6 additions & 6 deletions example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"context"
"log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -59,13 +59,13 @@ func main() {
)
pusher.Start()
defer pusher.Stop()
global.SetTracerProvider(tp)
global.SetMeterProvider(pusher.MeterProvider())
otel.SetTracerProvider(tp)
otel.SetMeterProvider(pusher.MeterProvider())

// set global propagator to baggage (the default is no-op).
global.SetTextMapPropagator(propagation.Baggage{})
tracer := global.Tracer("ex.com/basic")
meter := global.Meter("ex.com/basic")
otel.SetTextMapPropagator(propagation.Baggage{})
tracer := otel.Tracer("ex.com/basic")
meter := otel.Meter("ex.com/basic")

commonLabels := []label.KeyValue{lemonsKey.Int(10), label.String("A", "1"), label.String("B", "2"), label.String("C", "3")}

Expand Down
6 changes: 3 additions & 3 deletions example/jaeger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"log"

"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"

"go.opentelemetry.io/otel/exporters/trace/jaeger"
Expand Down Expand Up @@ -53,15 +53,15 @@ func main() {
flush := initTracer()
defer flush()

tr := global.Tracer("component-main")
tr := otel.Tracer("component-main")
ctx, span := tr.Start(ctx, "foo")
defer span.End()

bar(ctx)
}

func bar(ctx context.Context) {
tr := global.Tracer("component-bar")
tr := otel.Tracer("component-bar")
_, span := tr.Start(ctx, "bar")
defer span.End()

Expand Down
4 changes: 2 additions & 2 deletions example/namedtracer/foo/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package foo // import "go.opentelemetry.io/otel/example/namedtracer/foo"
import (
"context"

"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -31,7 +31,7 @@ var (
func SubOperation(ctx context.Context) error {
// Using global provider. Alternative is to have application provide a getter
// for its component to get the instance of the provider.
tr := global.Tracer("example/namedtracer/foo")
tr := otel.Tracer("example/namedtracer/foo")

var span trace.Span
_, span = tr.Start(ctx, "Sub operation...")
Expand Down
4 changes: 2 additions & 2 deletions example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"context"
"log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/example/namedtracer/foo"
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -52,7 +52,7 @@ func initTracer() {
),
sdktrace.WithSpanProcessor(bsp),
)
global.SetTracerProvider(tp)
otel.SetTracerProvider(tp)
}

func main() {
Expand Down
12 changes: 6 additions & 6 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

"google.golang.org/grpc"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -81,9 +81,9 @@ func initProvider() func() {
)

// set global propagator to tracecontext (the default is no-op).
global.SetTextMapPropagator(propagation.TraceContext{})
global.SetTracerProvider(tracerProvider)
global.SetMeterProvider(pusher.MeterProvider())
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
otel.SetMeterProvider(pusher.MeterProvider())
pusher.Start()

return func() {
Expand All @@ -99,8 +99,8 @@ func main() {
shutdown := initProvider()
defer shutdown()

tracer := global.Tracer("test-tracer")
meter := global.Meter("test-meter")
tracer := otel.Tracer("test-tracer")
meter := otel.Meter("test-meter")

// labels represent additional key-value descriptors that can be bound to a
// metric observer or recorder.
Expand Down
4 changes: 2 additions & 2 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/metric/prometheus"
"go.opentelemetry.io/otel/global"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
)
Expand All @@ -48,7 +48,7 @@ func initMeter() {
func main() {
initMeter()

meter := global.Meter("ex.com/basic")
meter := otel.Meter("ex.com/basic")
observerLock := new(sync.RWMutex)
observerValueToReport := new(float64)
observerLabelsToReport := new([]label.KeyValue)
Expand Down
Loading

0 comments on commit 3d6e8b5

Please sign in to comment.