2019-06-14 13:09:41 -07: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 11:37:05 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-09-25 13:22:33 -07:00
|
|
|
"log"
|
2019-06-14 11:37:05 -07:00
|
|
|
"net/http"
|
|
|
|
|
2019-07-15 14:49:21 -07:00
|
|
|
"go.opentelemetry.io/api/tag"
|
|
|
|
"go.opentelemetry.io/api/trace"
|
2019-09-25 13:22:33 -07:00
|
|
|
"go.opentelemetry.io/exporter/trace/jaeger"
|
2019-07-15 14:49:21 -07:00
|
|
|
"go.opentelemetry.io/plugin/httptrace"
|
2019-09-25 13:22:33 -07:00
|
|
|
sdktrace "go.opentelemetry.io/sdk/trace"
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
2019-09-25 13:22:33 -07:00
|
|
|
func initTracer() {
|
|
|
|
sdktrace.Register()
|
|
|
|
|
|
|
|
// Create Jaeger exporter to be able to retrieve
|
|
|
|
// the collected spans.
|
|
|
|
exporter, err := jaeger.NewExporter(jaeger.Options{
|
|
|
|
CollectorEndpoint: "http://localhost:14268/api/traces",
|
|
|
|
Process: jaeger.Process{
|
|
|
|
ServiceName: "trace-demo",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap Jaeger exporter with SimpleSpanProcessor and register the processor.
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
|
|
|
sdktrace.RegisterSpanProcessor(ssp)
|
|
|
|
|
|
|
|
// For the demonstration, use sdktrace.AlwaysSample sampler to sample all traces.
|
|
|
|
// In a production application, use sdktrace.ProbabilitySampler with a desired probability.
|
|
|
|
sdktrace.ApplyConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()})
|
|
|
|
}
|
2019-06-14 11:37:05 -07:00
|
|
|
|
|
|
|
func main() {
|
2019-09-25 13:22:33 -07:00
|
|
|
initTracer()
|
|
|
|
|
2019-06-14 11:37:05 -07:00
|
|
|
helloHandler := func(w http.ResponseWriter, req *http.Request) {
|
2019-09-23 11:51:32 -07:00
|
|
|
attrs, tags, spanCtx := httptrace.Extract(req.Context(), req)
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2019-07-11 15:28:38 -07:00
|
|
|
req = req.WithContext(tag.WithMap(req.Context(), tag.NewMap(tag.MapUpdate{
|
|
|
|
MultiKV: tags,
|
|
|
|
})))
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2019-09-25 13:22:33 -07:00
|
|
|
ctx, span := trace.GlobalTracer().Start(
|
2019-06-14 11:37:05 -07:00
|
|
|
req.Context(),
|
|
|
|
"hello",
|
2019-07-11 15:28:38 -07:00
|
|
|
trace.WithAttributes(attrs...),
|
|
|
|
trace.ChildOf(spanCtx),
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
defer span.Finish()
|
|
|
|
|
2019-09-03 20:03:51 +02:00
|
|
|
span.AddEvent(ctx, "handling this...")
|
2019-06-24 10:35:16 -07:00
|
|
|
|
2019-07-02 16:21:24 -07:00
|
|
|
_, _ = io.WriteString(w, "Hello, world!\n")
|
2019-06-14 11:37:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
http.HandleFunc("/hello", helloHandler)
|
|
|
|
err := http.ListenAndServe(":7777", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|