1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/example/basic/main.go
Tyler Yahn f995380e58
Unify api/label and api/kv in new label package (#1060)
* Move `api/label` to `label`

* Move `api/kv` package contents into `label` package

* Unify label package name

* Move `api/internal/rawhelpers.go` to `internal`

* Propagate replacing `api/kv` with `label` pkg

* golint

* Fix over-aggressive change

* Update Changelog
2020-08-17 20:25:03 -07:00

102 lines
2.8 KiB
Go

// Copyright The 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 main
import (
"context"
"log"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/label"
)
var (
fooKey = label.Key("ex.com/foo")
barKey = label.Key("ex.com/bar")
lemonsKey = label.Key("ex.com/lemons")
anotherKey = label.Key("ex.com/another")
)
func main() {
pusher, err := stdout.InstallNewPipeline([]stdout.Option{
stdout.WithQuantiles([]float64{0.5, 0.9, 0.99}),
stdout.WithPrettyPrint(),
}, nil)
if err != nil {
log.Fatalf("failed to initialize stdout export pipeline: %v", err)
}
defer pusher.Stop()
tracer := global.Tracer("ex.com/basic")
meter := global.Meter("ex.com/basic")
commonLabels := []label.KeyValue{lemonsKey.Int(10), label.String("A", "1"), label.String("B", "2"), label.String("C", "3")}
oneMetricCB := func(_ context.Context, result metric.Float64ObserverResult) {
result.Observe(1, commonLabels...)
}
_ = metric.Must(meter).NewFloat64ValueObserver("ex.com.one", oneMetricCB,
metric.WithDescription("A ValueObserver set to 1.0"),
)
valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two")
ctx := context.Background()
ctx = correlation.NewContext(ctx,
fooKey.String("foo1"),
barKey.String("bar1"),
)
valuerecorder := valuerecorderTwo.Bind(commonLabels...)
defer valuerecorder.Unbind()
err = func(ctx context.Context) error {
var span trace.Span
ctx, span = tracer.Start(ctx, "operation")
defer span.End()
span.AddEvent(ctx, "Nice operation!", label.Int("bogons", 100))
span.SetAttributes(anotherKey.String("yes"))
meter.RecordBatch(
// Note: call-site variables added as context Entries:
correlation.NewContext(ctx, anotherKey.String("xyz")),
commonLabels,
valuerecorderTwo.Measurement(2.0),
)
return func(ctx context.Context) error {
var span trace.Span
ctx, span = tracer.Start(ctx, "Sub operation...")
defer span.End()
span.SetAttributes(lemonsKey.String("five"))
span.AddEvent(ctx, "Sub span event")
valuerecorder.Record(ctx, 1.3)
return nil
}(ctx)
}(ctx)
if err != nil {
panic(err)
}
}