1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-05 15:05:51 +02:00
Joshua MacDonald be8fb0b4e2 Golang metrics prototype (#100)
* initial metrics work

* rename cumulative to counter

* rename bidirectional to nonmonotonic

* rename unidirectional to monotonic

* rename nonnegative to signed

this changes the default semantics a bit - before the change measure
could record negative values by default, now it can't.

The specification draft currently specifies both NonNegative and
Signed, but I think it's a mistake.

* rename instrument to descriptor

* license

* rework measurement values

* make measurement value a tagged union

* simplify to one kind of metrics

* add observers

* change some interfaces to match the spec

* keep integral measurement separate from floating ones

* remove duplicated measurement type

* add checking for options

* reorder some fields and functions

* rename a function

to avoid confusion between the Handle type and the Measure type

* drop disabled field from descriptor

* add back typed API for metrics

* make metric options type safe

* merge alternatives into a single bool

* make value kind name less stuttery

* fix observation callback prototype

* drop context parameter from NewHandle

* drop useless parameter names

* make descriptor an opaque struct

* use a store helper

* handle comment fixes

* reword Alternate comment

* drop the "any value" metrics

* make measurement value simpler

* document value stuff

* add tests for values

* docs

* do not panic if there is no span ID in the event
2019-10-08 15:45:49 -07:00

99 lines
2.5 KiB
Go

// 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 sdk
import (
"context"
"math/rand"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/key"
"go.opentelemetry.io/api/trace"
"go.opentelemetry.io/experimental/streaming/exporter"
)
var (
// TODO These should move somewhere in the api, right?
ErrorKey = key.New("error")
SpanIDKey = key.New("span_id")
TraceIDKey = key.New("trace_id")
MessageKey = key.New("message")
)
func (s *sdk) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
// TODO: use runtime/trace.WithRegion for execution sdk support
// TODO: use runtime/pprof.Do for profile tags support
ctx, span := s.Start(ctx, name)
defer span.End()
if err := body(ctx); err != nil {
span.SetAttribute(ErrorKey.Bool(true))
span.AddEvent(ctx, "span error", MessageKey.String(err.Error()))
return err
}
return nil
}
func (s *sdk) Start(ctx context.Context, name string, opts ...trace.SpanOption) (context.Context, trace.Span) {
var child core.SpanContext
child.SpanID = rand.Uint64()
o := &trace.SpanOptions{}
for _, opt := range opts {
opt(o)
}
var parentScope exporter.ScopeID
if o.Reference.HasTraceID() {
parentScope.SpanContext = o.Reference.SpanContext
} else {
parentScope.SpanContext = trace.CurrentSpan(ctx).SpanContext()
}
if parentScope.HasTraceID() {
parent := parentScope.SpanContext
child.TraceID.High = parent.TraceID.High
child.TraceID.Low = parent.TraceID.Low
} else {
child.TraceID.High = rand.Uint64()
child.TraceID.Low = rand.Uint64()
}
childScope := exporter.ScopeID{
SpanContext: child,
EventID: s.resources,
}
span := &span{
sdk: s,
initial: exporter.ScopeID{
SpanContext: child,
EventID: s.exporter.Record(exporter.Event{
Time: o.StartTime,
Type: exporter.START_SPAN,
Scope: s.exporter.NewScope(childScope, o.Attributes...),
Context: ctx,
Parent: parentScope,
String: name,
},
),
},
}
return trace.SetCurrentSpan(ctx, span), span
}