2019-06-14 22:09:41 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-04 22:23:23 +02:00
|
|
|
"log"
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/distributedcontext"
|
2019-11-26 06:41:24 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/key"
|
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2019-11-15 23:01:20 +02:00
|
|
|
metricstdout "go.opentelemetry.io/otel/exporter/metric/stdout"
|
|
|
|
tracestdout "go.opentelemetry.io/otel/exporter/trace/stdout"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/push"
|
2019-11-04 22:23:23 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-19 20:20:02 +02:00
|
|
|
fooKey = key.New("ex.com/foo")
|
|
|
|
barKey = key.New("ex.com/bar")
|
|
|
|
lemonsKey = key.New("ex.com/lemons")
|
2019-07-12 00:28:38 +02:00
|
|
|
anotherKey = key.New("ex.com/another")
|
2019-10-23 08:29:24 +02:00
|
|
|
)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-11-04 22:23:23 +02:00
|
|
|
// initTracer creates and registers trace provider instance.
|
|
|
|
func initTracer() {
|
|
|
|
var err error
|
2019-11-15 23:01:20 +02:00
|
|
|
exp, err := tracestdout.NewExporter(tracestdout.Options{PrettyPrint: false})
|
2019-11-04 22:23:23 +02:00
|
|
|
if err != nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
log.Panicf("failed to initialize trace stdout exporter %v", err)
|
2019-11-04 22:23:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tp, err := sdktrace.NewProvider(sdktrace.WithSyncer(exp),
|
|
|
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
|
|
|
|
if err != nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
log.Panicf("failed to initialize trace provider %v", err)
|
2019-11-04 22:23:23 +02:00
|
|
|
}
|
|
|
|
global.SetTraceProvider(tp)
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func initMeter() *push.Controller {
|
2020-01-03 19:48:45 +02:00
|
|
|
pusher, err := metricstdout.InstallNewPipeline(metricstdout.Config{
|
2019-11-15 23:01:20 +02:00
|
|
|
Quantiles: []float64{0.5, 0.9, 0.99},
|
|
|
|
PrettyPrint: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("failed to initialize metric stdout exporter %v", err)
|
|
|
|
}
|
|
|
|
return pusher
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func main() {
|
2019-11-15 23:01:20 +02:00
|
|
|
defer initMeter().Stop()
|
2019-11-04 22:23:23 +02:00
|
|
|
initTracer()
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
// Note: Have to get the meter and tracer after the global is
|
|
|
|
// initialized. See OTEP 0005.
|
|
|
|
|
2019-11-25 19:46:07 +02:00
|
|
|
tracer := global.TraceProvider().Tracer("ex.com/basic")
|
2019-11-26 19:54:05 +02:00
|
|
|
meter := global.MeterProvider().Meter("ex.com/basic")
|
2019-11-04 22:23:23 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
oneMetric := meter.NewFloat64Gauge("ex.com.one",
|
2019-06-14 20:37:05 +02:00
|
|
|
metric.WithKeys(fooKey, barKey, lemonsKey),
|
|
|
|
metric.WithDescription("A gauge set to 1.0"),
|
|
|
|
)
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
measureTwo := meter.NewFloat64Measure("ex.com.two")
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-10-10 23:30:22 +02:00
|
|
|
ctx = distributedcontext.NewContext(ctx,
|
2019-10-30 22:21:13 +02:00
|
|
|
fooKey.String("foo1"),
|
|
|
|
barKey.String("bar1"),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
commonLabels := meter.Labels(lemonsKey.Int(10), key.String("A", "1"), key.String("B", "2"), key.String("C", "3"))
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
gauge := oneMetric.Bind(commonLabels)
|
|
|
|
defer gauge.Unbind()
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-12-28 02:30:19 +02:00
|
|
|
measure := measureTwo.Bind(commonLabels)
|
|
|
|
defer measure.Unbind()
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {
|
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))
|
2019-06-24 19:35:16 +02:00
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(anotherKey.String("yes"))
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
gauge.Set(ctx, 1)
|
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
meter.RecordBatch(
|
2019-10-10 23:30:22 +02:00
|
|
|
// Note: call-site variables added as context Entries:
|
2019-10-30 22:21:13 +02:00
|
|
|
distributedcontext.NewContext(ctx, anotherKey.String("xyz")),
|
2019-10-09 00:45:49 +02:00
|
|
|
commonLabels,
|
|
|
|
|
|
|
|
oneMetric.Measurement(1.0),
|
|
|
|
measureTwo.Measurement(2.0),
|
|
|
|
)
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
return tracer.WithSpan(
|
|
|
|
ctx,
|
|
|
|
"Sub operation...",
|
|
|
|
func(ctx context.Context) error {
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(lemonsKey.String("five"))
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).AddEvent(ctx, "Sub span event")
|
2019-06-24 19:35:16 +02:00
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
measure.Record(ctx, 1.3)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|