2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-06-14 13:09:41 -07:00
|
|
|
//
|
|
|
|
// 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 11:37:05 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-05 04:23:23 +08:00
|
|
|
"log"
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2020-01-28 19:13:46 +01:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
2019-11-26 12:41:24 +08:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2020-07-22 12:34:44 -07:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout"
|
2020-08-17 20:25:03 -07:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-17 20:25:03 -07:00
|
|
|
fooKey = label.Key("ex.com/foo")
|
|
|
|
barKey = label.Key("ex.com/bar")
|
|
|
|
lemonsKey = label.Key("ex.com/lemons")
|
|
|
|
anotherKey = label.Key("ex.com/another")
|
2019-10-23 08:29:24 +02:00
|
|
|
)
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2020-07-22 12:34:44 -07:00
|
|
|
func main() {
|
|
|
|
pusher, err := stdout.InstallNewPipeline([]stdout.Option{
|
|
|
|
stdout.WithQuantiles([]float64{0.5, 0.9, 0.99}),
|
|
|
|
stdout.WithPrettyPrint(),
|
|
|
|
}, nil)
|
2019-11-15 13:01:20 -08:00
|
|
|
if err != nil {
|
2020-07-22 12:34:44 -07:00
|
|
|
log.Fatalf("failed to initialize stdout export pipeline: %v", err)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
2020-07-22 12:34:44 -07:00
|
|
|
defer pusher.Stop()
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-11 12:23:32 -03:00
|
|
|
tracer := global.Tracer("ex.com/basic")
|
|
|
|
meter := global.Meter("ex.com/basic")
|
2019-11-05 04:23:23 +08:00
|
|
|
|
2020-08-17 20:25:03 -07:00
|
|
|
commonLabels := []label.KeyValue{lemonsKey.Int(10), label.String("A", "1"), label.String("B", "2"), label.String("C", "3")}
|
2020-03-10 16:00:37 -07:00
|
|
|
|
2020-05-19 21:33:10 -07:00
|
|
|
oneMetricCB := func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-03-27 14:06:48 -07:00
|
|
|
result.Observe(1, commonLabels...)
|
2020-03-10 16:00:37 -07:00
|
|
|
}
|
2020-05-21 15:42:14 -07:00
|
|
|
_ = metric.Must(meter).NewFloat64ValueObserver("ex.com.one", oneMetricCB,
|
2020-05-18 11:03:43 -07:00
|
|
|
metric.WithDescription("A ValueObserver set to 1.0"),
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two")
|
2019-06-14 11:37:05 -07:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-01-28 19:13:46 +01:00
|
|
|
ctx = correlation.NewContext(ctx,
|
2019-10-30 13:21:13 -07:00
|
|
|
fooKey.String("foo1"),
|
|
|
|
barKey.String("bar1"),
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorder := valuerecorderTwo.Bind(commonLabels...)
|
|
|
|
defer valuerecorder.Unbind()
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2020-08-08 12:10:36 -07:00
|
|
|
err = func(ctx context.Context) error {
|
|
|
|
var span trace.Span
|
|
|
|
ctx, span = tracer.Start(ctx, "operation")
|
|
|
|
defer span.End()
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2020-08-17 20:25:03 -07:00
|
|
|
span.AddEvent(ctx, "Nice operation!", label.Int("bogons", 100))
|
2020-08-08 12:10:36 -07:00
|
|
|
span.SetAttributes(anotherKey.String("yes"))
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2019-10-08 15:45:49 -07:00
|
|
|
meter.RecordBatch(
|
2019-10-10 14:30:22 -07:00
|
|
|
// Note: call-site variables added as context Entries:
|
2020-01-28 19:13:46 +01:00
|
|
|
correlation.NewContext(ctx, anotherKey.String("xyz")),
|
2019-10-08 15:45:49 -07:00
|
|
|
commonLabels,
|
|
|
|
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorderTwo.Measurement(2.0),
|
2019-10-08 15:45:49 -07:00
|
|
|
)
|
|
|
|
|
2020-08-08 12:10:36 -07:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
var span trace.Span
|
|
|
|
ctx, span = tracer.Start(ctx, "Sub operation...")
|
|
|
|
defer span.End()
|
2019-06-24 10:35:16 -07:00
|
|
|
|
2020-08-08 12:10:36 -07:00
|
|
|
span.SetAttributes(lemonsKey.String("five"))
|
|
|
|
span.AddEvent(ctx, "Sub span event")
|
|
|
|
valuerecorder.Record(ctx, 1.3)
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2020-08-08 12:10:36 -07:00
|
|
|
return nil
|
|
|
|
}(ctx)
|
|
|
|
}(ctx)
|
2019-06-14 11:37:05 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|