/
main.go
57 lines (46 loc) · 1.28 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
package main
import (
"context"
"log"
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/PacktPublishing/Mastering-Distributed-Tracing/Chapter04/go/lib/tracing"
)
func main() {
tracer, closer := tracing.Init("go-4-formatter")
defer closer.Close()
opentracing.SetGlobalTracer(tracer)
http.HandleFunc("/formatGreeting", handleFormatGreeting)
log.Print("Listening on http://localhost:8082/")
log.Fatal(http.ListenAndServe(":8082", nil))
}
func handleFormatGreeting(w http.ResponseWriter, r *http.Request) {
span := opentracing.GlobalTracer().StartSpan("/formatGreeting")
defer span.Finish()
ctx := opentracing.ContextWithSpan(r.Context(), span)
name := r.FormValue("name")
title := r.FormValue("title")
descr := r.FormValue("description")
greeting := FormatGreeting(ctx, name, title, descr)
w.Write([]byte(greeting))
}
// FormatGreeting combines information about a person into a greeting string.
func FormatGreeting(
ctx context.Context,
name, title, description string,
) string {
span, ctx := opentracing.StartSpanFromContext(
ctx,
"format-greeting",
)
defer span.Finish()
response := "Hello, "
if title != "" {
response += title + " "
}
response += name + "!"
if description != "" {
response += " " + description
}
return response
}