-
Notifications
You must be signed in to change notification settings - Fork 1
/
falcon.go
167 lines (154 loc) · 3.16 KB
/
falcon.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
/*
* falcon.go - the data structure of open falcon and related functions
*
* history
* --------------------
* 2017/8/18, by Ye Zhiqin, create
*
* DESCRIPTION
* This file contains the definition of open falcon data structure
* and the function to push data to open falcon
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type FalconData struct {
Metric string `json:"metric"`
Endpoint string `json:"endpoint"`
Value interface{} `json:"value"`
CounterType string `json:"counterType"`
Tags string `json:"tags"`
Timestamp int64 `json:"timestamp"`
Step int64 `json:"step"`
}
/*
* SetValue - set FalconData value
*
* RECEIVER: *FalconData
*
* PARAMS:
* - v: value
*
* RETURNS:
* No return value
*/
func (data *FalconData) SetValue(v interface{}) {
data.Value = v
}
/*
* String - generate a new FalconData
*
* RECEIVER: *FalconData
*
* PARAMS:
* No paramter
*
* RETURNS:
* - string: string to display
*/
func (data *FalconData) String() string {
s := fmt.Sprintf("FalconData Metric:%s Endpoint:%s Value:%v CounterType:%s Tags:%s Timestamp:%d Step:%d",
data.Metric, data.Endpoint, data.Value, data.CounterType, data.Tags, data.Timestamp, data.Step)
return s
}
/*
* NewFalconData - generate a new FalconData
*
* PARAMS:
* - metric
* - endpoint
* - value
* - counterType
* - timestamp
* - step
*
* RETURNS:
* - *FalconData
*/
func NewFalconData(metric string, endpoint string, value interface{}, counterType string, tags string, timestamp int64, step int64) *FalconData {
point := &FalconData{
Metric: metric,
Endpoint: GetEndpoint(endpoint),
CounterType: counterType,
Tags: tags,
Timestamp: GetTimestamp(timestamp),
Step: step,
}
point.SetValue(value)
return point
}
/*
* GetEndpoint - generate endpoint value
*
* PARAMS:
* - endpoint
*
* RETURNS:
* - endpoint: if endpoint is avaliable
* - hostname: if endpoint is empty
* - localhost: if endpoint is empty and can't get hostname
*/
func GetEndpoint(endpoint string) string {
if endpoint != "" {
return endpoint
}
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
return hostname
}
/*
* GetTimestamp - generate timestamp value
*
* PARAMS:
* - timestamp
*
* RETURNS:
* - timestamp: if timestamp > 0
* - now: if timestamp <= 0
*/
func GetTimestamp(timestamp int64) int64 {
if timestamp > 0 {
return timestamp
} else {
return time.Now().Unix()
}
}
/*
* PushData - push data to open falcon
*
* PARAMS:
* - api: url of agent or transfer
* - data: an array of FalconData
*
* RETURNS:
* - []byte, nil: if succeed
* - nil, error: if fail
*/
func PushData(api string, data []*FalconData) ([]byte, error) {
points, err := json.Marshal(data)
if err != nil {
log.Printf("data marshaling FAIL: %v", err)
return nil, err
}
response, err := http.Post(api, "Content-Type: application/json", bytes.NewBuffer(points))
if err != nil {
log.Printf("api call FAIL: %v", err)
return nil, err
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return content, nil
}