forked from honeycombio/beeline-go
/
main.go
67 lines (58 loc) · 2.06 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/honeycombio/beeline-go"
"github.com/honeycombio/beeline-go/wrappers/hnyhttprouter"
"github.com/honeycombio/beeline-go/wrappers/hnynethttp"
"github.com/julienschmidt/httprouter"
)
func main() {
// Initialize beeline. The only required field is WriteKey.
beeline.Init(beeline.Config{
WriteKey: "abcabc123123",
Dataset: "sql",
// for demonstration, send the event to STDOUT intead of Honeycomb.
// Remove the STDOUT setting when filling in a real write key.
STDOUT: true,
})
// ensure everything gets sent off before we exit
defer beeline.Close()
router := httprouter.New()
// call regular httprouter Handles with wrappers to extract parameters
router.GET("/hello/:name", hnyhttprouter.Middleware(Hello))
// though the wrapper also works on routes that don't have parameters
router.GET("/", hnyhttprouter.Middleware(Index))
// wrap the main router to set everything up for instrumenting
log.Fatal(http.ListenAndServe("localhost:8080", hnynethttp.WrapHandler(router)))
}
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
beeline.AddField(r.Context(), "inHello", true)
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
// Produces an event like this:
// {
// "data": {
// "app.inHello": true,
// "duration_ms": 0.63284,
// "handler.name": "main.Hello",
// "handler.vars.name": "foo",
// "meta.localhostname": "cobbler",
// "meta.type": "http request",
// "request.content_length": 0,
// "request.header.user_agent": "curl/7.54.0",
// "request.host": "localhost:8080",
// "request.method": "GET",
// "request.path": "/hello/foo",
// "request.proto": "HTTP/1.1",
// "request.remote_addr": "[::1]:52539",
// "response.status_code": 200,
// "trace.span_id": "9eed613e-40f4-4bc8-b6b5-866cae2c51cf",
// "trace.trace_id": "91be396a-41a1-44aa-9f0a-25bf779448cc"
// },
// "time": "2018-04-06T22:55:05.040951984-07:00"
// }