| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelhttptrace |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"net/http" |
| 10 |
"net/http/httptrace" |
| 11 |
|
| 12 |
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" |
| 13 |
) |
| 14 |
|
| 15 |
func ExampleNewClientTrace() { |
| 16 |
client := http.Client{ |
| 17 |
Transport: otelhttp.NewTransport( |
| 18 |
http.DefaultTransport, |
| 19 |
otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace { |
| 20 |
return NewClientTrace(ctx) |
| 21 |
}), |
| 22 |
), |
| 23 |
} |
| 24 |
|
| 25 |
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://example.com", http.NoBody) |
| 26 |
if err != nil { |
| 27 |
fmt.Println(err) |
| 28 |
return |
| 29 |
} |
| 30 |
|
| 31 |
resp, err := client.Do(req) |
| 32 |
if err != nil { |
| 33 |
fmt.Println(err) |
| 34 |
return |
| 35 |
} |
| 36 |
|
| 37 |
defer resp.Body.Close() |
| 38 |
|
| 39 |
fmt.Println(resp.Status) |
| 40 |
} |