| 1 |
package newrelic_platform_go |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
NEWRELIC_API_URL = "https://platform-api.newrelic.com/platform/v1/metrics" |
| 15 |
) |
| 16 |
|
| 17 |
type INewrelicPlugin interface { |
| 18 |
GetMetricaKey(metrica IMetrica) string |
| 19 |
Harvest() error |
| 20 |
Run() |
| 21 |
AddComponent(component IComponent) |
| 22 |
} |
| 23 |
type NewrelicPlugin struct { |
| 24 |
Agent *Agent `json:"agent"` |
| 25 |
Components []ComponentData `json:"components"` |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
Client http.Client `json:"-"` |
| 30 |
|
| 31 |
|
| 32 |
URL string |
| 33 |
|
| 34 |
ComponentModels []IComponent `json:"-"` |
| 35 |
LastPollTime time.Time `json:"-"` |
| 36 |
Verbose bool `json:"-"` |
| 37 |
LicenseKey string `json:"-"` |
| 38 |
PollIntervalInSecond int `json:"-"` |
| 39 |
} |
| 40 |
|
| 41 |
func NewNewrelicPlugin(version string, licenseKey string, pollInterval int) *NewrelicPlugin { |
| 42 |
plugin := &NewrelicPlugin{ |
| 43 |
LicenseKey: licenseKey, |
| 44 |
PollIntervalInSecond: pollInterval, |
| 45 |
} |
| 46 |
|
| 47 |
plugin.Agent = NewAgent(version) |
| 48 |
plugin.Agent.CollectEnvironmentInfo() |
| 49 |
plugin.URL = NEWRELIC_API_URL |
| 50 |
|
| 51 |
plugin.ComponentModels = []IComponent{} |
| 52 |
return plugin |
| 53 |
} |
| 54 |
|
| 55 |
func (plugin *NewrelicPlugin) Harvest() error { |
| 56 |
startTime := time.Now() |
| 57 |
var duration int |
| 58 |
if plugin.LastPollTime.IsZero() { |
| 59 |
duration = plugin.PollIntervalInSecond |
| 60 |
} else { |
| 61 |
duration = int(startTime.Sub(plugin.LastPollTime).Seconds()) |
| 62 |
} |
| 63 |
|
| 64 |
plugin.Components = make([]ComponentData, 0, len(plugin.ComponentModels)) |
| 65 |
for i := 0; i < len(plugin.ComponentModels); i++ { |
| 66 |
plugin.ComponentModels[i].SetDuration(duration) |
| 67 |
plugin.Components = append(plugin.Components, plugin.ComponentModels[i].Harvest(plugin)) |
| 68 |
} |
| 69 |
|
| 70 |
if httpCode, err := plugin.SendMetricas(); err != nil { |
| 71 |
log.Printf("Can not send metricas to newrelic: %v\n", err) |
| 72 |
return err |
| 73 |
} else { |
| 74 |
|
| 75 |
if plugin.Verbose { |
| 76 |
log.Printf("Got HTTP response code:%d", httpCode) |
| 77 |
} |
| 78 |
|
| 79 |
if err, isFatal := plugin.CheckResponse(httpCode); isFatal { |
| 80 |
log.Printf("Got fatal error:%v\n", err) |
| 81 |
return err |
| 82 |
} else { |
| 83 |
if err != nil { |
| 84 |
log.Printf("WARNING: %v", err) |
| 85 |
} |
| 86 |
return err |
| 87 |
} |
| 88 |
} |
| 89 |
return nil |
| 90 |
} |
| 91 |
|
| 92 |
func (plugin *NewrelicPlugin) GetMetricaKey(metrica IMetrica) string { |
| 93 |
var keyBuffer bytes.Buffer |
| 94 |
|
| 95 |
keyBuffer.WriteString("Component/") |
| 96 |
keyBuffer.WriteString(metrica.GetName()) |
| 97 |
keyBuffer.WriteString("[") |
| 98 |
keyBuffer.WriteString(metrica.GetUnits()) |
| 99 |
keyBuffer.WriteString("]") |
| 100 |
|
| 101 |
return keyBuffer.String() |
| 102 |
} |
| 103 |
|
| 104 |
func (plugin *NewrelicPlugin) SendMetricas() (int, error) { |
| 105 |
var metricasJson []byte |
| 106 |
var encodingError error |
| 107 |
|
| 108 |
if plugin.Verbose { |
| 109 |
metricasJson, encodingError = json.MarshalIndent(plugin, "", " ") |
| 110 |
} else { |
| 111 |
metricasJson, encodingError = json.Marshal(plugin) |
| 112 |
} |
| 113 |
|
| 114 |
if encodingError != nil { |
| 115 |
return 0, encodingError |
| 116 |
} |
| 117 |
|
| 118 |
jsonAsString := string(metricasJson) |
| 119 |
if plugin.Verbose { |
| 120 |
log.Printf("Send data:%s \n", jsonAsString) |
| 121 |
} |
| 122 |
|
| 123 |
if httpRequest, err := http.NewRequest("POST", plugin.URL, strings.NewReader(jsonAsString)); err != nil { |
| 124 |
return 0, err |
| 125 |
} else { |
| 126 |
httpRequest.Header.Set("X-License-Key", plugin.LicenseKey) |
| 127 |
httpRequest.Header.Set("Content-Type", "application/json") |
| 128 |
httpRequest.Header.Set("Accept", "application/json") |
| 129 |
|
| 130 |
if httpResponse, err := plugin.Client.Do(httpRequest); err != nil { |
| 131 |
return 0, err |
| 132 |
} else { |
| 133 |
defer httpResponse.Body.Close() |
| 134 |
return httpResponse.StatusCode, nil |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
return 0, nil |
| 140 |
} |
| 141 |
|
| 142 |
func (plugin *NewrelicPlugin) ClearSentData() { |
| 143 |
for _, component := range plugin.ComponentModels { |
| 144 |
component.ClearSentData() |
| 145 |
} |
| 146 |
plugin.Components = nil |
| 147 |
plugin.LastPollTime = time.Now() |
| 148 |
} |
| 149 |
|
| 150 |
func (plugin *NewrelicPlugin) CheckResponse(httpResponseCode int) (error, bool) { |
| 151 |
isFatal := false |
| 152 |
var err error |
| 153 |
switch httpResponseCode { |
| 154 |
case http.StatusOK: |
| 155 |
{ |
| 156 |
plugin.ClearSentData() |
| 157 |
} |
| 158 |
case http.StatusForbidden: |
| 159 |
{ |
| 160 |
err = fmt.Errorf("Authentication error (no license key header, or invalid license key).\n") |
| 161 |
isFatal = true |
| 162 |
} |
| 163 |
case http.StatusBadRequest: |
| 164 |
{ |
| 165 |
err = fmt.Errorf("The request or headers are in the wrong format or the URL is incorrect.\n") |
| 166 |
isFatal = true |
| 167 |
} |
| 168 |
case http.StatusNotFound: |
| 169 |
{ |
| 170 |
err = fmt.Errorf("Invalid URL\n") |
| 171 |
isFatal = true |
| 172 |
} |
| 173 |
case http.StatusRequestEntityTooLarge: |
| 174 |
{ |
| 175 |
err = fmt.Errorf("Too many metrics were sent in one request, or too many components (instances) were specified in one request, or other single-request limits were reached.\n") |
| 176 |
|
| 177 |
plugin.ClearSentData() |
| 178 |
} |
| 179 |
case http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout: |
| 180 |
{ |
| 181 |
err = fmt.Errorf("Got %v response code.Metricas will be aggregated", httpResponseCode) |
| 182 |
} |
| 183 |
} |
| 184 |
return err, isFatal |
| 185 |
} |
| 186 |
|
| 187 |
func (plugin *NewrelicPlugin) Run() { |
| 188 |
plugin.Harvest() |
| 189 |
tickerChannel := time.Tick(time.Duration(plugin.PollIntervalInSecond) * time.Second) |
| 190 |
for ts := range tickerChannel { |
| 191 |
plugin.Harvest() |
| 192 |
|
| 193 |
if plugin.Verbose { |
| 194 |
log.Printf("Harvest ended at:%v\n", ts) |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func (plugin *NewrelicPlugin) AddComponent(component IComponent) { |
| 200 |
plugin.ComponentModels = append(plugin.ComponentModels, component) |
| 201 |
} |