/
main.go
45 lines (35 loc) · 1.04 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
)
const ServiceName = "hello-service"
func healthzHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
span := trace.SpanFromContext(r.Context())
log.Info().Str("TraceID", span.SpanContext().TraceID().String()).Msg("Hello Service")
fmt.Fprintf(w, "Hello, World!")
}
func main() {
handler := http.HandlerFunc(httpHandler)
telemetryHandler := otelhttp.NewHandler(handler, ServiceName)
http.Handle("/hello", telemetryHandler)
http.HandleFunc("/healthz", healthzHandler)
initMetrics()
cleanup := initTracing()
defer cleanup()
http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, http.DefaultServeMux))
}
// HandleErr is a generic error handler
func HandleErr(err error, message string) {
if err != nil {
log.Fatal().Err(err).Msg(message)
}
}