-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver_newrelic.go
More file actions
80 lines (67 loc) · 1.94 KB
/
Copy pathreceiver_newrelic.go
File metadata and controls
80 lines (67 loc) · 1.94 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
package main
import (
"context"
"fmt"
"reflect"
"sort"
"time"
"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry"
log "github.com/sirupsen/logrus"
)
const ACARSCustomEventType = "CustomACARS"
type NewRelicReceiver struct {
Module
Receiver
// API License key to use New Relic.
APIKey string `jsonschema:"required" default:"api_key"`
// Name for the custom event type to create (example if set to "MyCustomACARSEvents": `FROM MyCustomACARSEvents SELECT count(timestamp)`). If not provided, it will be `CustomACARS`.
CustomEventType string `default:"CustomACARS"`
}
// Must satisfy Receiver interface
func (n NewRelicReceiver) Name() string {
return reflect.TypeOf(n).Name()
}
// Must satisfy Receiver interface
func (n NewRelicReceiver) Send(a APMessage) (err error) {
if n.APIKey == "" {
return fmt.Errorf("New Relic API key not specified: %w", err)
}
// Create a new harvester for sending telemetry data.
harvester, err := telemetry.NewHarvester(
telemetry.ConfigAPIKey(n.APIKey),
)
if err != nil {
return fmt.Errorf("Error creating harvester: %w", err)
}
// Allow overriding the custom event type if set
eventType := ACARSCustomEventType
if n.CustomEventType != "" {
eventType = n.CustomEventType
}
event := telemetry.Event{
EventType: eventType,
Attributes: a,
}
// Record the custom event.
err = harvester.RecordEvent(event)
if err != nil {
return err
}
// Flush events to New Relic. HarvestNow sends any recorded events immediately.
log.Debug(Content("%s: calling receiver", n.Name()))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
harvester.HarvestNow(ctx)
err = ctx.Err()
return err
}
func (f NewRelicReceiver) Configured() bool {
return !reflect.DeepEqual(f, NewRelicReceiver{})
}
func (f NewRelicReceiver) GetDefaultFields() (s []string) {
for f := range FormatAsAPMessage(NewRelicReceiver{}, "NewRelicReceiver") {
s = append(s, f)
}
sort.Strings(s)
return s
}