-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
155 lines (124 loc) · 5.21 KB
/
client.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
package appinsights
import (
"time"
"github.com/Microsoft/ApplicationInsights-Go/appinsights/contracts"
)
// Application Insights telemetry client provides interface to track telemetry
// items.
type TelemetryClient interface {
// Gets the telemetry context for this client. Values found on this
// context will get written out to every telemetry item tracked by
// this client.
Context() *TelemetryContext
// Gets the instrumentation key assigned to this telemetry client.
InstrumentationKey() string
// Gets the telemetry channel used to submit data to the backend.
Channel() TelemetryChannel
// Gets whether this client is enabled and will accept telemetry.
IsEnabled() bool
// Enables or disables the telemetry client. When disabled, telemetry
// is silently swallowed by the client. Defaults to enabled.
SetIsEnabled(enabled bool)
// Submits the specified telemetry item.
Track(telemetry Telemetry)
// Log a user action with the specified name
TrackEvent(name string)
// Log a numeric value that is not specified with a specific event.
// Typically used to send regular reports of performance indicators.
TrackMetric(name string, value float64)
// Log a trace message with the specified severity level.
TrackTrace(name string, severity contracts.SeverityLevel)
// Log an HTTP request with the specified method, URL, duration and
// response code.
TrackRequest(method, url string, duration time.Duration, responseCode string)
// Log a dependency with the specified name, type, target, and
// success status.
TrackRemoteDependency(name, dependencyType, target string, success bool)
// Log an availability test result with the specified test name,
// duration, and success status.
TrackAvailability(name string, duration time.Duration, success bool)
// Log an exception with the specified error, which may be a string,
// error or Stringer. The current callstack is collected
// automatically.
TrackException(err interface{})
}
type telemetryClient struct {
channel TelemetryChannel
context *TelemetryContext
isEnabled bool
}
// Creates a new telemetry client instance that submits telemetry with the
// specified instrumentation key.
func NewTelemetryClient(iKey string) TelemetryClient {
return NewTelemetryClientFromConfig(NewTelemetryConfiguration(iKey))
}
// Creates a new telemetry client instance configured by the specified
// TelemetryConfiguration object.
func NewTelemetryClientFromConfig(config *TelemetryConfiguration) TelemetryClient {
return &telemetryClient{
channel: NewInMemoryChannel(config),
context: config.setupContext(),
isEnabled: true,
}
}
// Gets the telemetry context for this client. Values found on this context
// will get written out to every telemetry item tracked by this client.
func (tc *telemetryClient) Context() *TelemetryContext {
return tc.context
}
// Gets the telemetry channel used to submit data to the backend.
func (tc *telemetryClient) Channel() TelemetryChannel {
return tc.channel
}
// Gets the instrumentation key assigned to this telemetry client.
func (tc *telemetryClient) InstrumentationKey() string {
return tc.context.InstrumentationKey()
}
// Gets whether this client is enabled and will accept telemetry.
func (tc *telemetryClient) IsEnabled() bool {
return tc.isEnabled
}
// Enables or disables the telemetry client. When disabled, telemetry is
// silently swallowed by the client. Defaults to enabled.
func (tc *telemetryClient) SetIsEnabled(isEnabled bool) {
tc.isEnabled = isEnabled
}
// Submits the specified telemetry item.
func (tc *telemetryClient) Track(item Telemetry) {
if tc.isEnabled && item != nil {
tc.channel.Send(tc.context.envelop(item))
}
}
// Log a user action with the specified name
func (tc *telemetryClient) TrackEvent(name string) {
tc.Track(NewEventTelemetry(name))
}
// Log a numeric value that is not specified with a specific event.
// Typically used to send regular reports of performance indicators.
func (tc *telemetryClient) TrackMetric(name string, value float64) {
tc.Track(NewMetricTelemetry(name, value))
}
// Log a trace message with the specified severity level.
func (tc *telemetryClient) TrackTrace(message string, severity contracts.SeverityLevel) {
tc.Track(NewTraceTelemetry(message, severity))
}
// Log an HTTP request with the specified method, URL, duration and response
// code.
func (tc *telemetryClient) TrackRequest(method, url string, duration time.Duration, responseCode string) {
tc.Track(NewRequestTelemetry(method, url, duration, responseCode))
}
// Log a dependency with the specified name, type, target, and success
// status.
func (tc *telemetryClient) TrackRemoteDependency(name, dependencyType, target string, success bool) {
tc.Track(NewRemoteDependencyTelemetry(name, dependencyType, target, success))
}
// Log an availability test result with the specified test name, duration,
// and success status.
func (tc *telemetryClient) TrackAvailability(name string, duration time.Duration, success bool) {
tc.Track(NewAvailabilityTelemetry(name, duration, success))
}
// Log an exception with the specified error, which may be a string, error
// or Stringer. The current callstack is collected automatically.
func (tc *telemetryClient) TrackException(err interface{}) {
tc.Track(newExceptionTelemetry(err, 1))
}