/
main.go
130 lines (112 loc) · 3.12 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Splunk Inc.
//
// 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.
// Package example instruments a simple HTTP server-client application.
//
// The application is configured to send spans to a local instance
// of the OpenTelemetry Collector, which propagates them to both
// Splunk Observability Cloud and to a local Jaeger instance.
package main
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"time"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/sync/errgroup"
"github.com/signalfx/splunk-otel-go/distro"
"github.com/signalfx/splunk-otel-go/instrumentation/net/http/splunkhttp"
)
const address = "localhost:8080"
func main() {
if err := run(); err != nil {
log.Fatalln(err)
}
}
func run() (err error) {
// handle CTRL+C gracefully
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// initialize Splunk OTel distro
sdk, err := distro.Run()
if err != nil {
return err
}
defer func() {
err = errors.Join(err, sdk.Shutdown(context.Background()))
}()
// instrument http.Handler
var handler http.Handler = http.HandlerFunc(handle)
handler = splunkhttp.NewHandler(handler)
handler = otelhttp.NewHandler(handler, "handle")
l, err := net.Listen("tcp", address)
if err != nil {
return err
}
srv := &http.Server{
Handler: handler,
WriteTimeout: time.Second,
ReadTimeout: time.Second,
ReadHeaderTimeout: time.Second,
}
g := &errgroup.Group{}
g.Go(func() error {
err := srv.Serve(l) // Closing via srv.Shutdown.
if err == http.ErrServerClosed {
return nil
}
return err // Error while serving.
})
g.Go(func() error {
// instrument http.Client
client := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
call(ctx, client)
// When Shutdown is called, Serve immediately returns ErrServerClosed.
return srv.Shutdown(context.Background())
})
return g.Wait()
}
func handle(w http.ResponseWriter, req *http.Request) {
fmt.Println("HTTP request:")
dump, err := httputil.DumpRequest(req, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Print(string(dump))
}
func call(ctx context.Context, client *http.Client) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+address, http.NoBody)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
fmt.Println("HTTP response:")
dump, err := httputil.DumpResponse(resp, false)
if err != nil {
log.Println(err)
return
}
fmt.Print(string(dump))
}