2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-06-14 22:09:41 +02: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 20:37:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-09-25 22:22:33 +02:00
|
|
|
"log"
|
2019-06-14 20:37:05 +02:00
|
|
|
"net/http"
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
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/trace"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/trace/stdout"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/plugin/httptrace"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-09-25 22:22:33 +02:00
|
|
|
func initTracer() {
|
2019-10-04 21:05:24 +02:00
|
|
|
// Create stdout exporter to be able to retrieve
|
2019-09-25 22:22:33 +02:00
|
|
|
// the collected spans.
|
2019-09-28 20:27:02 +02:00
|
|
|
exporter, err := stdout.NewExporter(stdout.Options{PrettyPrint: true})
|
2019-09-25 22:22:33 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the demonstration, use sdktrace.AlwaysSample sampler to sample all traces.
|
|
|
|
// In a production application, use sdktrace.ProbabilitySampler with a desired probability.
|
2019-10-22 22:19:11 +02:00
|
|
|
tp, err := sdktrace.NewProvider(sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
|
|
|
sdktrace.WithSyncer(exporter))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-25 20:38:52 +02:00
|
|
|
global.SetTraceProvider(tp)
|
2019-09-25 22:22:33 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
func main() {
|
2019-09-25 22:22:33 +02:00
|
|
|
initTracer()
|
2020-03-11 17:23:32 +02:00
|
|
|
tr := global.Tracer("example/server")
|
2019-09-25 22:22:33 +02:00
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
helloHandler := func(w http.ResponseWriter, req *http.Request) {
|
2019-10-10 23:30:22 +02:00
|
|
|
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2020-02-27 19:10:52 +02:00
|
|
|
req = req.WithContext(correlation.ContextWithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
|
2019-10-10 23:30:22 +02:00
|
|
|
MultiKV: entries,
|
2019-07-12 00:28:38 +02:00
|
|
|
})))
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
ctx, span := tr.Start(
|
2020-02-04 18:55:03 +02:00
|
|
|
trace.ContextWithRemoteSpanContext(req.Context(), spanCtx),
|
2019-06-14 20:37:05 +02:00
|
|
|
"hello",
|
2019-07-12 00:28:38 +02:00
|
|
|
trace.WithAttributes(attrs...),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
2019-09-27 19:48:10 +02:00
|
|
|
defer span.End()
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-09-03 20:03:51 +02:00
|
|
|
span.AddEvent(ctx, "handling this...")
|
2019-06-24 19:35:16 +02:00
|
|
|
|
2019-07-03 01:21:24 +02:00
|
|
|
_, _ = io.WriteString(w, "Hello, world!\n")
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
http.HandleFunc("/hello", helloHandler)
|
|
|
|
err := http.ListenAndServe(":7777", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|