/
remnant.go
183 lines (159 loc) · 4.3 KB
/
remnant.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"sync"
"time"
_ "github.com/lib/pq"
data "github.com/travissimon/remnant/dummydata"
"github.com/travissimon/remnant/types"
)
func swaggerIndexHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "swagger/remnant.swagger.json")
}
func logHandler(w http.ResponseWriter, r *http.Request) {
// decode log message
var logMsg types.LogMessage
err := json.NewDecoder(r.Body).Decode(&logMsg)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
data.InsertLog(logMsg)
}
func spanHandler(w http.ResponseWriter, r *http.Request) {
// decode log message
var span types.Span
err := json.NewDecoder(r.Body).Decode(&span)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
if span.TraceId == span.Id {
// Initiating span
data.InsertSpan(span)
} else {
mergeSpan(span)
}
}
var spanCache map[string]types.Span
var spanMutex = &sync.Mutex{}
func writeMismatch(fieldName, existing, new string) {
fmt.Fprintf(os.Stderr, "Span merge, mismatched field: %s\n", fieldName)
fmt.Fprintf(os.Stderr, "existing: %s\n", existing)
fmt.Fprintf(os.Stderr, "new: %s\n", new)
}
func mergeSpan(span types.Span) {
spanMutex.Lock()
sp, exists := spanCache[span.Id]
if !exists {
// either client or remote half of this span is missing -
// cache it until we get all the details
spanCache[span.Id] = span
spanMutex.Unlock()
return
}
spanMutex.Unlock()
// remove span from cache
delete(spanCache, span.Id)
// copy across missing values
if sp.TraceId == "" {
sp.TraceId = span.TraceId
} else if sp.TraceId != span.TraceId {
writeMismatch("TraceId", sp.TraceId, span.TraceId)
}
if sp.ParentId == "" {
sp.ParentId = span.ParentId
} else if span.ParentId != "" && sp.ParentId != span.ParentId {
writeMismatch("ParentId", sp.ParentId, span.ParentId)
}
if sp.ClientStart == "" {
sp.ClientStart = span.ClientStart
}
if sp.ClientEnd == "" {
sp.ClientEnd = span.ClientEnd
}
if sp.RemoteStart == "" {
sp.RemoteStart = span.RemoteStart
}
if sp.RemoteEnd == "" {
sp.RemoteEnd = span.RemoteEnd
}
if sp.Host == "" {
sp.Host = span.Host
} else if span.Host != "" && sp.Host != span.Host {
writeMismatch("Host", sp.Host, span.Host)
}
if sp.Method == "" {
sp.Method = span.Method
} else if span.Method != "" && sp.Method != span.Method {
writeMismatch("Method", sp.Method, span.Method)
}
if sp.Url == "" {
sp.Url = span.Url
} else if span.Url != "" && sp.Url != span.Url {
writeMismatch("Url", sp.Url, span.Url)
}
if sp.Headers == nil || len(sp.Headers) == 0 {
sp.Headers = span.Headers
}
if sp.Parameters == nil || len(sp.Parameters) == 0 {
sp.Parameters = span.Parameters
}
if sp.Body == "" {
sp.Body = span.Body
} else if span.Body != "" && sp.Body != span.Body {
writeMismatch("Body", sp.Body, span.Body)
}
if sp.ResponseCode == 0 {
sp.ResponseCode = span.ResponseCode
} else if span.ResponseCode != 0 && sp.ResponseCode != span.ResponseCode {
writeMismatch("ResponseCode", string(sp.ResponseCode), string(span.ResponseCode))
}
data.InsertSpan(sp)
}
func healthzHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}
func purgeCache() {
sleepTime := 1 * time.Second
for {
expireTime := time.Now().UTC().Add(-5 * time.Second)
// remove old entries
for _, span := range spanCache {
var timeStr = span.ClientStart
if timeStr == "" {
timeStr = span.RemoteStart
}
t, err := time.Parse(time.RFC3339Nano, timeStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing cached span's start time: %s\n", err.Error())
} else {
if t.Before(expireTime) {
spanMutex.Lock()
delete(spanCache, span.Id)
spanMutex.Unlock()
data.InsertSpan(span)
}
}
}
time.Sleep(sleepTime)
}
}
func main() {
var port = flag.String("port", "7777", "Define what TCP port to bind to")
flag.Parse()
spanCache = make(map[string]types.Span)
http.Handle("/swagger/", http.StripPrefix("/swagger/", http.FileServer(http.Dir("swagger"))))
http.HandleFunc("/v1/log", logHandler)
http.HandleFunc("/v1/client-span", spanHandler)
http.HandleFunc("/v1/remote-span", spanHandler)
http.HandleFunc("/healthz", healthzHandler)
fmt.Printf("Starting Remnant server on port %s\n", *port)
http.ListenAndServe(":"+*port, nil)
}