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"
|
2020-04-23 13:11:24 -07:00
|
|
|
"flag"
|
2019-06-14 11:37:05 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-09-25 13:22:33 -07:00
|
|
|
"log"
|
|
|
|
|
2020-05-14 07:06:03 +08:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
|
|
|
|
2019-06-14 11:37:05 -07:00
|
|
|
"net/http"
|
2019-09-25 13:22:33 -07:00
|
|
|
"time"
|
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"
|
2020-03-02 13:54:57 -08:00
|
|
|
"go.opentelemetry.io/otel/exporters/trace/stdout"
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/plugin/httptrace"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
2019-09-25 13:22:33 -07:00
|
|
|
func initTracer() {
|
2019-09-28 11:27:02 -07:00
|
|
|
// Create stdout exporter to be able to retrieve
|
2019-09-25 13:22:33 -07:00
|
|
|
// the collected spans.
|
2019-09-28 11:27:02 -07:00
|
|
|
exporter, err := stdout.NewExporter(stdout.Options{PrettyPrint: true})
|
2019-09-25 13:22:33 -07: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 13:19:11 -07:00
|
|
|
tp, err := sdktrace.NewProvider(sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
|
|
|
sdktrace.WithSyncer(exporter))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-25 11:38:52 -07:00
|
|
|
global.SetTraceProvider(tp)
|
2019-09-25 13:22:33 -07:00
|
|
|
}
|
2019-06-14 11:37:05 -07:00
|
|
|
|
|
|
|
func main() {
|
2019-09-25 13:22:33 -07:00
|
|
|
initTracer()
|
2020-04-23 13:11:24 -07:00
|
|
|
url := flag.String("server", "http://localhost:7777/hello", "server url")
|
|
|
|
flag.Parse()
|
2019-09-25 13:22:33 -07:00
|
|
|
|
2019-06-14 11:37:05 -07:00
|
|
|
client := http.DefaultClient
|
2020-01-28 19:13:46 +01:00
|
|
|
ctx := correlation.NewContext(context.Background(),
|
2020-05-14 07:06:03 +08:00
|
|
|
kv.String("username", "donuts"),
|
2019-06-14 11:37:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var body []byte
|
|
|
|
|
2020-03-11 12:23:32 -03:00
|
|
|
tr := global.Tracer("example/client")
|
2019-10-22 13:19:11 -07:00
|
|
|
err := tr.WithSpan(ctx, "say hello",
|
2019-06-14 11:37:05 -07:00
|
|
|
func(ctx context.Context) error {
|
2020-04-23 13:11:24 -07:00
|
|
|
req, _ := http.NewRequest("GET", *url, nil)
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2019-09-23 11:51:32 -07:00
|
|
|
ctx, req = httptrace.W3C(ctx, req)
|
|
|
|
httptrace.Inject(ctx, req)
|
2019-06-14 11:37:05 -07:00
|
|
|
|
2019-09-25 13:22:33 -07:00
|
|
|
fmt.Printf("Sending request...\n")
|
2019-06-14 11:37:05 -07:00
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
body, err = ioutil.ReadAll(res.Body)
|
2019-12-09 13:03:11 -08:00
|
|
|
_ = res.Body.Close()
|
2019-06-14 11:37:05 -07:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:22:33 -07: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)
|
2020-04-23 13:11:24 -07:00
|
|
|
fmt.Printf("Inspect traces on stdout\n")
|
2019-06-14 11:37:05 -07:00
|
|
|
}
|