-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathevent.go
More file actions
89 lines (76 loc) · 2.41 KB
/
event.go
File metadata and controls
89 lines (76 loc) · 2.41 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
81
82
83
84
85
86
87
88
89
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2017
package instana
import (
"time"
)
// EventData is the construct serialized for the host agent
type EventData struct {
Title string `json:"title"`
Text string `json:"text"`
// Duration in milliseconds
Duration int `json:"duration"`
// Severity with value of -1, 5, 10 : see type severity
Severity int `json:"severity"`
Plugin string `json:"plugin,omitempty"`
ID string `json:"id,omitempty"`
Host string `json:"host"`
}
type severity int
// Severity values for events sent to the instana agent
const (
SeverityChange severity = -1
SeverityWarning severity = 5
SeverityCritical severity = 10
)
// Defaults for the Event API
const (
ServicePlugin = "com.instana.forge.connection.http.logical.LogicalWebApp"
ServiceHost = ""
)
// SendDefaultServiceEvent sends a default event which already contains the service and host
func SendDefaultServiceEvent(title string, text string, sev severity, duration time.Duration) {
var service string
if s, err := getSensor(); err != nil {
defaultLogger.Warn("error retrieving sensor", err.Error())
} else {
service = s.serviceOrBinaryName()
}
// If the sensor is not yet initialized, there is no default service (as
// configured on the sensor) so we will send blank instead
SendServiceEvent(service, title, text, sev, duration)
}
// SendServiceEvent sends an event on a specific service
func SendServiceEvent(service string, title string, text string, sev severity, duration time.Duration) {
sendEvent(&EventData{
Title: title,
Text: text,
Severity: int(sev),
Plugin: ServicePlugin,
ID: service,
Host: ServiceHost,
Duration: int(duration / time.Millisecond),
})
}
// SendHostEvent sends an event on the current host
func SendHostEvent(title string, text string, sev severity, duration time.Duration) {
sendEvent(&EventData{
Title: title,
Text: text,
Duration: int(duration / time.Millisecond),
Severity: int(sev),
})
}
func sendEvent(event *EventData) {
s, err := getSensor()
if err != nil {
// If the sensor hasn't initialized we do so here so that we properly
// discover where the host agent may be as it varies between a
// normal host, docker, kubernetes etc..
InitSensor(&Options{})
}
// we do fire & forget here, because the whole pid dance isn't necessary to send events
go func(e *EventData) {
_ = s.Agent().SendEvent(e)
}(event)
}