in sampleapp/main.go [41:116]
func main() {
initProvider()
r := mux.NewRouter()
r.Use(otelmux.Middleware("my-server"))
// labels represent additional key-value descriptors that can be bound to a
// metric observer or recorder.
commonLabels := []attribute.KeyValue{
attribute.String("labelA", "chocolate"),
attribute.String("labelB", "raspberry"),
attribute.String("labelC", "vanilla"),
}
r.HandleFunc("/aws-sdk-call", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
ctx := r.Context()
xrayTraceID := getXrayTraceID(trace.SpanFromContext(ctx))
json := simplejson.New()
json.Set("traceId", xrayTraceID)
payload, _ := json.MarshalJSON()
_, _ = w.Write(payload)
}))
r.HandleFunc("/outgoing-http-call", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
ctx := r.Context()
xrayTraceID, _ := func(ctx context.Context) (string, error) {
req, _ := http.NewRequestWithContext(ctx, "GET", "https://aws.amazon.com", nil)
res, err := client.Do(req)
if err != nil {
handleErr(err, "HTTP call to aws.amazon.com failed")
}
_, _ = ioutil.ReadAll(res.Body)
_ = res.Body.Close()
return getXrayTraceID(trace.SpanFromContext(ctx)), err
}(ctx)
ctx, span := tracer.Start(
ctx,
"CollectorExporter-Example",
trace.WithAttributes(commonLabels...))
defer span.End()
json := simplejson.New()
json.Set("traceId", xrayTraceID)
payload, _ := json.MarshalJSON()
_, _ = w.Write(payload)
}))
http.Handle("/", r)
// Start server
address := os.Getenv("LISTEN_ADDRESS")
if len(address) > 0 {
_ = http.ListenAndServe(fmt.Sprintf("%s", address), nil)
} else {
// Default port 8080
_ = http.ListenAndServe("localhost:8080", nil)
}
}