-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
44 lines (35 loc) · 996 Bytes
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"io"
"net/http"
"time"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
func githubHandler(w http.ResponseWriter, r *http.Request) {
// Add sleep to make spans more distinct
time.Sleep(200 * time.Millisecond)
req, err := http.NewRequestWithContext(r.Context(), "GET", "https://api.github.com/", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req.Header.Add("Accept", `application/json`)
client := http.Client{
Timeout: time.Duration(1) * time.Second,
Transport: otelhttp.NewTransport(http.DefaultTransport),
}
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Printf("client.Do response status : %s \n", resp.Status)
fmt.Fprint(w, "External call ok\n")
}