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"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-09-25 22:22:33 +02:00
|
|
|
"log"
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
"net/http"
|
2019-09-25 22:22:33 +02:00
|
|
|
"time"
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-19 19:44:46 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
2019-10-10 23:30:22 +02:00
|
|
|
"go.opentelemetry.io/api/distributedcontext"
|
2019-07-15 23:49:21 +02:00
|
|
|
"go.opentelemetry.io/api/key"
|
|
|
|
"go.opentelemetry.io/api/trace"
|
2019-09-28 20:27:02 +02:00
|
|
|
"go.opentelemetry.io/exporter/trace/stdout"
|
2019-09-25 18:38:55 +02:00
|
|
|
"go.opentelemetry.io/plugin/httptrace"
|
2019-09-25 22:22:33 +02:00
|
|
|
sdktrace "go.opentelemetry.io/sdk/trace"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-09-25 22:22:33 +02:00
|
|
|
func initTracer() {
|
2019-09-28 20:27:02 +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)
|
|
|
|
}
|
2019-10-17 00:13:39 +02:00
|
|
|
exporter.RegisterSimpleSpanProcessor()
|
2019-09-25 22:22:33 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
trace.SetGlobalProvider(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()
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
client := http.DefaultClient
|
2019-10-10 23:30:22 +02:00
|
|
|
ctx := distributedcontext.NewContext(context.Background(),
|
|
|
|
distributedcontext.Insert(key.New("username").String("donuts")),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var body []byte
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
tr := trace.GlobalProvider().GetTracer("example/client")
|
|
|
|
err := tr.WithSpan(ctx, "say hello",
|
2019-06-14 20:37:05 +02:00
|
|
|
func(ctx context.Context) error {
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost:7777/hello", nil)
|
|
|
|
|
2019-09-23 20:51:32 +02:00
|
|
|
ctx, req = httptrace.W3C(ctx, req)
|
|
|
|
httptrace.Inject(ctx, req)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-09-25 22:22:33 +02:00
|
|
|
fmt.Printf("Sending request...\n")
|
2019-06-14 20:37:05 +02:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
body, err = ioutil.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
2019-07-12 00:28:38 +02:00
|
|
|
trace.CurrentSpan(ctx).SetStatus(codes.OK)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:22:33 +02:00
|
|
|
fmt.Printf("Response Received: %s\n\n\n", body)
|
|
|
|
fmt.Printf("Waiting for few seconds to export spans ...\n\n")
|
|
|
|
time.Sleep(10 * time.Second)
|
2019-09-28 20:27:02 +02:00
|
|
|
fmt.Printf("Inspect traces on stdout")
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|