This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
192 lines (181 loc) · 5.5 KB
/
helpers.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
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"reflect"
"strings"
"gopkg.in/mgo.v2/bson"
"github.com/mitchellh/mapstructure"
)
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondWithJson(w, code, map[string]string{"error": msg})
}
func respondWithJson(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func readAllString(r io.Reader) (res string, err error) {
log.Output(0, "Function: readAllString")
b, err := ioutil.ReadAll(r)
if err != nil {
log.Output(0, err.Error())
return "", err
}
return string(b), nil
}
func insertMultipleTransactions(transactions []interface{}) (int, error) {
log.Output(0, "Function: insertMultipleTransactions")
if len(transactions) <= 0 {
return http.StatusOK, errors.New("No Transactions found")
}
err := dao.BulkInsert(transactions)
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusCreated, nil
}
func httpRequest(url string, method string, Headers interface{}, payload_string string) (response SimulateResponse, statusCode int, err error) {
log.Output(0, "Function: httpRequest")
var payload = []byte(payload_string)
method = strings.ToUpper(method)
if method != "GET" && method != "POST" {
log.Output(0, "Error while creating http request.\tReason:"+err.Error())
return response, http.StatusBadRequest, errors.New("Request Method not supported")
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
log.Output(0, "Error while creating http request.\tReason:"+err.Error())
return response, http.StatusInternalServerError, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(reflect.TypeOf(err))
log.Println(err)
if strings.Contains(err.Error(), "connect: connection refused"){
return response, http.StatusServiceUnavailable, err
}
log.Output(0, "Error sending http request.\tReason:"+err.Error())
return response, http.StatusInternalServerError, err
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Output(0, "Error: "+err.Error())
return response, http.StatusInternalServerError, err
}
response = SimulateResponse{
Status : "success",
StatusCode : resp.StatusCode,
Response : strings.TrimSpace(string(responseBody)),
Headers : fetchHeaders(resp.Header),
}
return response, http.StatusOK, nil
}
func fetchHeaders(headers http.Header) (interface{}) {
headerArray := make(map[string]interface{}, len(headers))
for index, header := range headers {
/*log.Println(reflect.TypeOf(index));
log.Println(reflect.TypeOf(header));
log.Println(index);
log.Println(header[0]);*/
headerArray[index] = header[0]
}
return headerArray
}
func getTransactions(allBody string) []interface{} {
log.Output(0, "Function: getTransaction")
// Trimming leading and trailing whitespaces, tabs and new lines
allBody = strings.TrimSpace(allBody)
// making array of each lines
requestObjects := strings.Split(allBody, "\n")
var message context
var transactions []interface{}
var m Metadata
for _, element := range requestObjects {
// in case of an empty array object, we will skip
if element == "" {
continue
}
/*log.Println("element:\n",element)*/
err := json.Unmarshal([]byte(element), &message)
if err != nil {
log.Output(0, "Error: "+err.Error())
return nil
}
if val, ok := message["metadata"]; ok {
log.Output(0, "Metadata detected")
mapstructure.Decode(val, &m)
/*metadataJSON, err := json.Marshal(m)
if err != nil {
log.Fatal(err)
return nil
}
log.Println("string(metadataJSON):\n", string(metadataJSON))*/
}
if val, ok := message["transaction"]; ok {
var t Transaction
log.Output(0, "Transaction detected")
/*tempJSON, err := json.Marshal(val)
if err != nil {
log.Fatal(err)
return nil
}
log.Println(string(tempJSON))*/
/*mapstructure.Decode(val, &t)*/
tempJSON, err := json.Marshal(val)
if err != nil {
log.Output(0, "Error: "+err.Error())
return nil
}
err = json.Unmarshal([]byte(tempJSON), &t)
if err != nil {
log.Output(0, "Error: "+err.Error())
return nil
}
mo := MongoObject{
ID: bson.NewObjectId(),
Timestamp: t.Timestamp,
Sampled: t.Sampled,
Result: t.Result,
Duration: t.Duration,
TraceID: t.TraceID,
}
mo.Metadata.Service.Name = m.Service.Name
mo.Metadata.Version = m.Service.Version
mo.Metadata.Language = m.Service.Language
mo.Metadata.Agent = m.Service.Agent
mo.Metadata.Framework = m.Service.Framework
mo.Request.URL = t.Context.Request.URL.Full
mo.Request.Body = t.Context.Request.Body
mo.Request.Headers = t.Context.Request.Headers
mo.Request.Method = t.Context.Request.Method
mo.Response.StatusCode = t.Context.Response.StatusCode
mo.Response.Headers = t.Context.Response.Headers
transactions = append(transactions, &mo)
}
}
/*transactionsJSON, err := json.Marshal(transactions)
if err != nil {
log.Fatal(err)
return nil
}
log.Println("string(transactionsJSON):\n", string(transactionsJSON))*/
return transactions
}
func AppendIfUnique(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}