-
Notifications
You must be signed in to change notification settings - Fork 0
/
craque.go
111 lines (95 loc) · 2.62 KB
/
craque.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
/*
Craque
*/
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// check the backend server for a datetimestamp
func dt(w http.ResponseWriter, r *http.Request) {
// DDog strongly recommends the ENV VARs are configured in the yaml,
// so they are put here as defaults to signal when they are not.
tracer.Start(
tracer.WithServiceName("aslsp"),
tracer.WithEnv("proto"), // DD_ENV
tracer.WithService("generic"), // DD_SERVICE
tracer.WithServiceVersion("0.0.0"), // DD_VERSION
)
defer tracer.Stop() // The Agent flushes the data here.
span := tracer.StartSpan("web.request",
tracer.ResourceName("Bacque"),
)
defer span.Finish()
span.SetTag("http.url", r.URL.Path)
// existing prometheus tracing
dtCount.Add(1)
dtTimer := prometheus.NewTimer(CFetchDuration)
defer dtTimer.ObserveDuration()
zerolog.TimeFieldFormat = ""
// this value expects the full url,
// i.e.: http://localhost:9999/fetch
url := os.Getenv("BACQUE")
craqueClient := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal()
}
req.Header.Set("User-Agent", "craquego")
res, err := craqueClient.Do(req)
if err != nil {
// if BACQUE fails, fall back to a local timestamp only,
// not the enriched IP addresses that BACQUE returns.
http.Error(w, http.StatusText(418), 418)
arg := "+%Y%m%d%H%S"
app := "date"
dtloc, err := exec.Command(app, arg).Output()
if err != nil {
log.Fatal()
}
fmt.Fprintf(w, "DateTime=%s", dtloc)
// log service unresponsive
log.Error().
Str("host", r.Host).
Str("ref", r.RemoteAddr).
Str("xref", r.Header.Get("X-Forwarded-For")).
Str("method", r.Method).
Str("path", r.URL.Path).
Str("proto", r.Proto).
Str("agent", r.Header.Get("User-Agent")).
Str("response", "418").
Err(err).
Msg("service unresponsive local dt returned")
return
}
// don't waste resources
defer res.Body.Close()
// TODO: status code should probably be checked to move on
// fmt.Printf("StatusCode: %d: %q", res.StatusCode, res.Request.URL) // debug
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal()
}
fmt.Fprintf(w, "%s", body)
// log frontend access
log.Info().
Str("host", r.Host).
Str("ref", r.RemoteAddr).
Str("xref", r.Header.Get("X-Forwarded-For")).
Str("method", r.Method).
Str("path", r.URL.Path).
Str("proto", r.Proto).
Str("agent", r.Header.Get("User-Agent")).
Str("response", "200").
Msg("")
}