-
Notifications
You must be signed in to change notification settings - Fork 69
/
command_post.go
63 lines (50 loc) · 1.88 KB
/
command_post.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
package events
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/utils"
)
var (
event string
)
var cmdPost = &cobra.Command{
Use: "post",
Short: "Post a custom event to New Relic",
Long: `Post a custom event to New Relic
The post command accepts an account ID and JSON-formatted payload representing a
custom event to be posted to New Relic. These events once posted can be queried
using NRQL via the CLI or New Relic One UI.
The accepted payload requires the use of an ` + "`eventType`" + `field that
represents the custom event's type.
`,
Example: `newrelic events post --accountId 12345 --event '{ "eventType": "Payment", "amount": 123.45 }'`,
PreRun: client.RequireClient,
RunE: func(cmd *cobra.Command, args []string) error {
accountID := configAPI.RequireActiveProfileAccountID()
if configAPI.GetActiveProfileString(config.LicenseKey) == "" {
log.Fatal("a License key is required, set one in your default profile or use the NEW_RELIC_LICENSE_KEY environment variable")
}
var e map[string]interface{}
err := json.Unmarshal([]byte(event), &e)
if err != nil {
log.Fatal(err)
}
if err := client.NRClient.Events.CreateEventWithContext(utils.SignalCtx, accountID, event); err != nil {
return err
}
log.Info("success")
return nil
},
}
func init() {
Command.AddCommand(cmdPost)
cmdPost.Flags().StringVarP(&event, "event", "e", "{}", "a JSON-formatted event payload to post")
utils.LogIfError(cmdPost.MarkFlagRequired("event"))
Command.AddCommand(cmdPostFile)
cmdPostFile.Flags().StringVarP(&file, "file", "f", "file.json", "a JSON-formatted events file to post")
utils.LogIfError(cmdPostFile.MarkFlagFilename("file"))
}