Skip to content

Commit c3f5896

Browse files
authored
Merge pull request #17 from Microsoft/develop
Merge develop to master (0.4.1)
2 parents 3612f58 + cb36923 commit c3f5896

7 files changed

+92
-25
lines changed

appinsights/exception.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// occurred during execution of the monitored application.
1414
type ExceptionTelemetry struct {
1515
BaseTelemetry
16+
BaseTelemetryMeasurements
1617

1718
// Panic message: string, error, or Stringer
1819
Error interface{}
@@ -38,9 +39,11 @@ func newExceptionTelemetry(err interface{}, skip int) *ExceptionTelemetry {
3839
Frames: GetCallstack(2 + skip),
3940
SeverityLevel: Error,
4041
BaseTelemetry: BaseTelemetry{
41-
Timestamp: currentClock.Now(),
42-
Tags: make(contracts.ContextTags),
43-
Properties: make(map[string]string),
42+
Timestamp: currentClock.Now(),
43+
Tags: make(contracts.ContextTags),
44+
Properties: make(map[string]string),
45+
},
46+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
4447
Measurements: make(map[string]float64),
4548
},
4649
}

appinsights/package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ package appinsights
44

55
const (
66
sdkName = "go"
7-
Version = "0.4.0"
7+
Version = "0.4.1"
88
)

appinsights/telemetry.go

+60-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type Telemetry interface {
2424
// Gets the time when this item was measured
2525
Time() time.Time
2626

27+
// Sets the timestamp to the specified time.
28+
SetTime(time.Time)
29+
2730
// Gets context data containing extra, optional tags. Overrides
2831
// values found on client TelemetryContext.
2932
ContextTags() map[string]string
@@ -39,26 +42,40 @@ type Telemetry interface {
3942
GetMeasurements() map[string]float64
4043
}
4144

42-
// Common base struct for telemetry items.
45+
// BaseTelemetry is the common base struct for telemetry items.
4346
type BaseTelemetry struct {
4447
// The time this when this item was measured
4548
Timestamp time.Time
4649

4750
// Custom properties
4851
Properties map[string]string
4952

53+
// Telemetry Context containing extra, optional tags.
54+
Tags contracts.ContextTags
55+
}
56+
57+
// BaseTelemetryMeasurements provides the Measurements field for telemetry
58+
// items that support it.
59+
type BaseTelemetryMeasurements struct {
5060
// Custom measurements
5161
Measurements map[string]float64
62+
}
5263

53-
// Telemetry Context containing extra, optional tags.
54-
Tags contracts.ContextTags
64+
// BaseTelemetryNoMeasurements provides no Measurements field for telemetry
65+
// items that omit it.
66+
type BaseTelemetryNoMeasurements struct {
5567
}
5668

57-
// Gets the time when this item was measured
69+
// Time returns the timestamp when this was measured.
5870
func (item *BaseTelemetry) Time() time.Time {
5971
return item.Timestamp
6072
}
6173

74+
// SetTime sets the timestamp to the specified time.
75+
func (item *BaseTelemetry) SetTime(t time.Time) {
76+
item.Timestamp = t
77+
}
78+
6279
// Gets context data containing extra, optional tags. Overrides values
6380
// found on client TelemetryContext.
6481
func (item *BaseTelemetry) ContextTags() map[string]string {
@@ -71,14 +88,20 @@ func (item *BaseTelemetry) GetProperties() map[string]string {
7188
}
7289

7390
// Gets custom measurements to submit with the telemetry item.
74-
func (item *BaseTelemetry) GetMeasurements() map[string]float64 {
91+
func (item *BaseTelemetryMeasurements) GetMeasurements() map[string]float64 {
7592
return item.Measurements
7693
}
7794

95+
// GetMeasurements returns nil for telemetry items that do not support measurements.
96+
func (item *BaseTelemetryNoMeasurements) GetMeasurements() map[string]float64 {
97+
return nil
98+
}
99+
78100
// Trace telemetry items represent printf-like trace statements that can be
79101
// text searched.
80102
type TraceTelemetry struct {
81103
BaseTelemetry
104+
BaseTelemetryNoMeasurements
82105

83106
// Trace message
84107
Message string
@@ -113,6 +136,7 @@ func (trace *TraceTelemetry) TelemetryData() TelemetryData {
113136
// Event telemetry items represent structured event records.
114137
type EventTelemetry struct {
115138
BaseTelemetry
139+
BaseTelemetryMeasurements
116140

117141
// Event name
118142
Name string
@@ -123,9 +147,11 @@ func NewEventTelemetry(name string) *EventTelemetry {
123147
return &EventTelemetry{
124148
Name: name,
125149
BaseTelemetry: BaseTelemetry{
126-
Timestamp: currentClock.Now(),
127-
Tags: make(contracts.ContextTags),
128-
Properties: make(map[string]string),
150+
Timestamp: currentClock.Now(),
151+
Tags: make(contracts.ContextTags),
152+
Properties: make(map[string]string),
153+
},
154+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
129155
Measurements: make(map[string]float64),
130156
},
131157
}
@@ -143,6 +169,7 @@ func (event *EventTelemetry) TelemetryData() TelemetryData {
143169
// Metric telemetry items each represent a single data point.
144170
type MetricTelemetry struct {
145171
BaseTelemetry
172+
BaseTelemetryNoMeasurements
146173

147174
// Metric name
148175
Name string
@@ -183,6 +210,7 @@ func (metric *MetricTelemetry) TelemetryData() TelemetryData {
183210
// function.
184211
type AggregateMetricTelemetry struct {
185212
BaseTelemetry
213+
BaseTelemetryNoMeasurements
186214

187215
// Metric name
188216
Name string
@@ -323,6 +351,7 @@ func (agg *AggregateMetricTelemetry) TelemetryData() TelemetryData {
323351
// application and contains a summary of that request execution and results.
324352
type RequestTelemetry struct {
325353
BaseTelemetry
354+
BaseTelemetryMeasurements
326355

327356
// Identifier of a request call instance. Used for correlation between request
328357
// and other telemetry items.
@@ -384,9 +413,11 @@ func NewRequestTelemetry(method, uri string, duration time.Duration, responseCod
384413
ResponseCode: responseCode,
385414
Success: success,
386415
BaseTelemetry: BaseTelemetry{
387-
Timestamp: currentClock.Now().Add(-duration),
388-
Tags: make(contracts.ContextTags),
389-
Properties: make(map[string]string),
416+
Timestamp: currentClock.Now().Add(-duration),
417+
Tags: make(contracts.ContextTags),
418+
Properties: make(map[string]string),
419+
},
420+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
390421
Measurements: make(map[string]float64),
391422
},
392423
}
@@ -423,6 +454,7 @@ func (request *RequestTelemetry) TelemetryData() TelemetryData {
423454
// component with a remote component/service like SQL or an HTTP endpoint.
424455
type RemoteDependencyTelemetry struct {
425456
BaseTelemetry
457+
BaseTelemetryMeasurements
426458

427459
// Name of the command that initiated this dependency call. Low cardinality
428460
// value. Examples are stored procedure name and URL path template.
@@ -463,9 +495,11 @@ func NewRemoteDependencyTelemetry(name, dependencyType, target string, success b
463495
Target: target,
464496
Success: success,
465497
BaseTelemetry: BaseTelemetry{
466-
Timestamp: currentClock.Now(),
467-
Tags: make(contracts.ContextTags),
468-
Properties: make(map[string]string),
498+
Timestamp: currentClock.Now(),
499+
Tags: make(contracts.ContextTags),
500+
Properties: make(map[string]string),
501+
},
502+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
469503
Measurements: make(map[string]float64),
470504
},
471505
}
@@ -498,6 +532,7 @@ func (telem *RemoteDependencyTelemetry) TelemetryData() TelemetryData {
498532
// test.
499533
type AvailabilityTelemetry struct {
500534
BaseTelemetry
535+
BaseTelemetryMeasurements
501536

502537
// Identifier of a test run. Used to correlate steps of test run and
503538
// telemetry generated by the service.
@@ -527,9 +562,11 @@ func NewAvailabilityTelemetry(name string, duration time.Duration, success bool)
527562
Duration: duration,
528563
Success: success,
529564
BaseTelemetry: BaseTelemetry{
530-
Timestamp: currentClock.Now(),
531-
Tags: make(contracts.ContextTags),
532-
Properties: make(map[string]string),
565+
Timestamp: currentClock.Now(),
566+
Tags: make(contracts.ContextTags),
567+
Properties: make(map[string]string),
568+
},
569+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
533570
Measurements: make(map[string]float64),
534571
},
535572
}
@@ -560,6 +597,7 @@ func (telem *AvailabilityTelemetry) TelemetryData() TelemetryData {
560597
// click.
561598
type PageViewTelemetry struct {
562599
BaseTelemetry
600+
BaseTelemetryMeasurements
563601

564602
// Request URL with all query string parameters
565603
Url string
@@ -577,9 +615,11 @@ func NewPageViewTelemetry(name, url string) *PageViewTelemetry {
577615
Name: name,
578616
Url: url,
579617
BaseTelemetry: BaseTelemetry{
580-
Timestamp: currentClock.Now(),
581-
Tags: make(contracts.ContextTags),
582-
Properties: make(map[string]string),
618+
Timestamp: currentClock.Now(),
619+
Tags: make(contracts.ContextTags),
620+
Properties: make(map[string]string),
621+
},
622+
BaseTelemetryMeasurements: BaseTelemetryMeasurements{
583623
Measurements: make(map[string]float64),
584624
},
585625
}

appinsights/telemetry_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func TestTraceTelemetry(t *testing.T) {
5959

6060
checkDataContract(t, "Message", d2.Message, "~my-2nd-message~")
6161
checkDataContract(t, "SeverityLevel", d2.SeverityLevel, Critical)
62+
63+
var telemInterface Telemetry
64+
if telemInterface = telem; telemInterface.GetMeasurements() != nil {
65+
t.Errorf("Trace.(Telemetry).GetMeasurements should return nil")
66+
}
6267
}
6368

6469
func TestEventTelemetry(t *testing.T) {
@@ -118,6 +123,11 @@ func TestMetricTelemetry(t *testing.T) {
118123
checkDataContract(t, "DataPoint.Value", dp2.Value, 5678.0)
119124
checkDataContract(t, "DataPoint.Kind", dp2.Kind, Measurement)
120125
checkDataContract(t, "DataPoint.Count", dp2.Count, 1)
126+
127+
var telemInterface Telemetry
128+
if telemInterface = telem; telemInterface.GetMeasurements() != nil {
129+
t.Errorf("Metric.(Telemetry).GetMeasurements should return nil")
130+
}
121131
}
122132

123133
type statsTest struct {

appinsights/telemetrycontext.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (context *TelemetryContext) envelop(item Telemetry) *contracts.Envelope {
6464
timestamp = currentClock.Now()
6565
}
6666

67-
envelope.Time = timestamp.UTC().Format(time.RFC3339)
67+
envelope.Time = timestamp.UTC().Format(time.RFC3339Nano)
6868

6969
if contextTags := item.ContextTags(); contextTags != nil {
7070
envelope.Tags = contextTags

appinsights/telemetrycontext_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package appinsights
33
import (
44
"strings"
55
"testing"
6+
"time"
67

78
"github.com/Microsoft/ApplicationInsights-Go/appinsights/contracts"
89
)
@@ -132,3 +133,13 @@ func TestSanitize(t *testing.T) {
132133
t.Error("Event measurement name was not truncated")
133134
}
134135
}
136+
137+
func TestTimestamp(t *testing.T) {
138+
ev := NewEventTelemetry("event")
139+
ev.Timestamp = time.Unix(1523667421, 500000000)
140+
141+
envelope := NewTelemetryContext().envelop(ev)
142+
if envelope.Time != "2018-04-14T00:57:01.5Z" {
143+
t.Errorf("Unexpected timestamp: %s", envelope.Time)
144+
}
145+
}

appinsights/transmitter_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ func TestTransmitResults(t *testing.T) {
425425
}
426426

427427
func TestGetRetryItems(t *testing.T) {
428+
mockClock()
429+
defer resetClock()
430+
428431
// Keep a pristine copy.
429432
originalPayload, originalItems := makePayload()
430433

0 commit comments

Comments
 (0)