-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.go
More file actions
162 lines (130 loc) · 3.67 KB
/
agent.go
File metadata and controls
162 lines (130 loc) · 3.67 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/MindsightCo/hotpath-agent/msclient"
"github.com/MindsightCo/hotpath-agent/samplecache"
"github.com/ereyes01/go-auth0-grant"
"github.com/pkg/errors"
)
var (
host string
port int
server string
cacheLen int
testMode bool
client *http.Client
)
const (
CREDS_AUDIENCE = "https://api.mindsight.io/"
DEFAULT_API_SERVER = "https://api.mindsight.io/query"
AUTH0_TOKEN_URL = "https://mindsight.auth0.com/oauth/token/"
)
func init() {
flag.StringVar(&host, "host", "", "Address to bind server to")
flag.IntVar(&port, "port", 8000, "Port to listen on")
flag.IntVar(&cacheLen, "cache", 5, "Number requests to cache before sending samples")
flag.StringVar(&server, "server", DEFAULT_API_SERVER, "URL of API server")
flag.BoolVar(&testMode, "test", false, "Enable test mode, does not attempt to send data")
client = &http.Client{}
}
var mutation string = `
mutation ($sample: DataSample!) {
collectData(sample: $sample)
}
`
func sendSamples(samples *samplecache.HotpathSample, grant auth0grant.Grant) error {
gql := msclient.GraphqlRequest{
Query: mutation,
Variables: map[string]interface{}{
"sample": samples,
},
}
if _, err := msclient.APIRequest(server, &gql, grant); err != nil {
return errors.Wrap(err, "send hotpath samples")
}
return nil
}
func initGrant(testMode bool) (auth0grant.Grant, error) {
if testMode {
return nil, nil
}
credRequest := &auth0grant.CredentialsRequest{
ClientID: os.Getenv("MINDSIGHT_CLIENT_ID"),
ClientSecret: os.Getenv("MINDSIGHT_CLIENT_SECRET"),
Audience: CREDS_AUDIENCE,
GrantType: auth0grant.CLIENT_CREDS_GRANT_TYPE,
}
if credRequest.ClientID == "" || credRequest.ClientSecret == "" {
return nil, errors.New("Must supply env variables MINDSIGHT_CLIENT_ID and MINDSIGHT_CLIENT_SECRET")
}
grant := auth0grant.NewGrant(AUTH0_TOKEN_URL, credRequest)
// test the token
if _, err := grant.GetAccessToken(); err != nil {
return nil, errors.Wrap(err, "testing credentials")
}
return grant, nil
}
func main() {
flag.Parse()
grant, err := initGrant(testMode)
if err != nil {
err = errors.Wrap(err, "Mindsight agent: fail init grant")
log.Fatal(err)
}
log.Println("Starting Mindsight agent...")
samples := samplecache.NewRawSamples()
count := 0
http.HandleFunc("/samples/", func(w http.ResponseWriter, r *http.Request) {
var data map[string]int
defer r.Body.Close()
query := r.URL.Query()
projectName := query.Get("project")
environment := query.Get("environment")
if projectName == "" {
http.Error(w, "must specify ``project'' query parameter", http.StatusBadRequest)
return
}
if r.Method != "POST" {
http.Error(w, "only POST allowed for /samples/", http.StatusNotFound)
return
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
msg := fmt.Sprintf("invalid json: %s", err)
http.Error(w, msg, http.StatusBadRequest)
return
}
samples.Set(data, projectName, environment)
count += 1
if count > cacheLen {
payloads := samples.GetAll()
if testMode {
if err := samples.Dump(); err != nil {
log.Println(err)
w.WriteHeader(http.StatusCreated)
return
}
}
for _, payload := range payloads {
if err := sendSamples(payload, grant); err != nil {
log.Println(err)
w.WriteHeader(http.StatusCreated)
return
}
}
samples.Clear()
count = 0
}
w.WriteHeader(http.StatusCreated)
})
if server != DEFAULT_API_SERVER {
log.Printf("Using API Server: %s", server)
}
bind := fmt.Sprintf("%s:%d", host, port)
log.Printf("Listening for data, binding to %s ...", bind)
log.Fatal(http.ListenAndServe(bind, nil))
}