Skip to content

Commit

Permalink
Initial public release updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Kim committed Jan 25, 2016
1 parent 736bacd commit 041faba
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

# OS specific
.DS_Store
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ApplicationInsights-Go
Microsoft Application Insights SDK for Go

This project provides a Go SDK for Application Insights. [Application Insights](http://azure.microsoft.com/en-us/services/application-insights/) is a service that allows developers to keep their applications available, performant, and successful. This go package will allow you to send telemetry of various kinds (event, trace, exception, etc.) to the Application Insights service where they can be visualized in the Azure Portal.
This project provides a Go SDK for Application Insights. [Application Insights](http://azure.microsoft.com/en-us/services/application-insights/) is a service that allows developers to keep their applications available, performant, and successful. This go package will allow you to send telemetry of various kinds (event, metric, trace) to the Application Insights service where they can be visualized in the Azure Portal.

## Requirements ##
**Install**
Expand All @@ -19,10 +19,10 @@ import "github.com/Microsoft/ApplicationInsights-Go"
client := appinsights.NewTelemetryClient("<instrumentation key>")
client.TrackEvent("custom event")
client.TrackMetric("custom metric", 123)
client.TrackTrace("trace message"
client.TrackTrace("trace message")
```

## telpad example test and example app ##
## telpad test and example app ##

The telpad app can be used to send test telemetry and as an example of using the SDK.

Expand Down
17 changes: 12 additions & 5 deletions appinsights/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type TelemetryClient interface {
Context() TelemetryContext
InstrumentationKey() string
IsEnabled() bool
SetIsEnabled(bool)
SetIsEnabled(bool)
Track(Telemetry)
TrackEvent(string)
TrackEventTelemetry(*EventTelemetry)
Expand All @@ -28,7 +28,7 @@ type telemetryClient struct {
func NewTelemetryClient(iKey string) TelemetryClient {
config := NewTelemetryConfiguration(iKey)
channel := NewInMemoryChannel(config.EndpointUrl)
context := NewTelemetryContext()
context := NewClientTelemetryContext()
return &telemetryClient{
TelemetryConfiguration: config,
channel: channel,
Expand All @@ -50,7 +50,7 @@ func (tc *telemetryClient) IsEnabled() bool {
}

func (tc *telemetryClient) SetIsEnabled(isEnabled bool) {
tc.isEnabled = isEnabled
tc.isEnabled = isEnabled
}

func (tc *telemetryClient) Track(item Telemetry) {
Expand All @@ -60,9 +60,16 @@ func (tc *telemetryClient) Track(item Telemetry) {
iKey = tc.TelemetryConfiguration.InstrumentationKey
}

item.Context().(*telemetryContext).iKey = iKey
itemContext := item.Context().(*telemetryContext)
itemContext.iKey = iKey

// TODO: Copy tc.context.Properties to item.Context().Properties
clientContext := tc.context.(*telemetryContext)

for tagkey, tagval := range clientContext.tags {
if itemContext.tags[tagkey] == "" {
itemContext.tags[tagkey] = tagval
}
}

tc.channel.Send(item)
}
Expand Down
10 changes: 10 additions & 0 deletions appinsights/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package appinsights

import "testing"

func TestClientBurstPerformance(t *testing.T) {
telemetryClient := NewTelemetryClient("")
for i := 0; i < 1000000; i++ {
telemetryClient.TrackTrace("A message")
}
}
72 changes: 36 additions & 36 deletions appinsights/datacontracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package appinsights
import "time"

type Telemetry interface {
Timestamp() time.Time
Context() TelemetryContext
Timestamp() time.Time
Context() TelemetryContext
baseTypeName() string
baseData() Domain
}

type BaseTelemetry struct {
timestamp time.Time
timestamp time.Time
context TelemetryContext
}

type TraceTelemetry struct {
BaseTelemetry
data *messageData
BaseTelemetry
data *messageData
}

func NewTraceTelemetry(message string, severityLevel SeverityLevel) *TraceTelemetry {
Expand All @@ -28,21 +28,21 @@ func NewTraceTelemetry(message string, severityLevel SeverityLevel) *TraceTeleme
data.Ver = 2

item := &TraceTelemetry{
data: data,
data: data,
}
item.timestamp = now
item.context = &telemetryContext{}
return item

item.timestamp = now
item.context = NewItemTelemetryContext()

return item
}

func (item *TraceTelemetry) Timestamp() time.Time {
return item.timestamp
}

func (item *TraceTelemetry) Context() TelemetryContext {
return item.context
return item.context
}

func (item *TraceTelemetry) baseTypeName() string {
Expand All @@ -55,7 +55,7 @@ func (item *TraceTelemetry) baseData() Domain {

type EventTelemetry struct {
BaseTelemetry
data *eventData
data *eventData
}

func NewEventTelemetry(name string) *EventTelemetry {
Expand All @@ -67,21 +67,21 @@ func NewEventTelemetry(name string) *EventTelemetry {
data.Ver = 2

item := &EventTelemetry{
data: data,
data: data,
}
item.timestamp = now
item.context = &telemetryContext{}
return item

item.timestamp = now
item.context = NewItemTelemetryContext()

return item
}

func (item *EventTelemetry) Timestamp() time.Time {
return item.timestamp
}

func (item *EventTelemetry) Context() TelemetryContext {
return item.context
return item.context
}

func (item *EventTelemetry) baseTypeName() string {
Expand All @@ -94,7 +94,7 @@ func (item *EventTelemetry) baseData() Domain {

type MetricTelemetry struct {
BaseTelemetry
data *metricData
data *metricData
}

func NewMetricTelemetry(name string, value float32) *MetricTelemetry {
Expand All @@ -113,21 +113,21 @@ func NewMetricTelemetry(name string, value float32) *MetricTelemetry {
data.Metrics[0] = metric

item := &MetricTelemetry{
data: data,
data: data,
}
item.timestamp = now
item.context = &telemetryContext{}
return item

item.timestamp = now
item.context = NewItemTelemetryContext()

return item
}

func (item *MetricTelemetry) Timestamp() time.Time {
return item.timestamp
}

func (item *MetricTelemetry) Context() TelemetryContext {
return item.context
return item.context
}

func (item *MetricTelemetry) baseTypeName() string {
Expand All @@ -140,7 +140,7 @@ func (item *MetricTelemetry) baseData() Domain {

type RequestTelemetry struct {
BaseTelemetry
data *requestData
data *requestData
}

func NewRequestTelemetry(name string, timestamp time.Time, duration time.Duration, responseCode string, success bool) *RequestTelemetry {
Expand All @@ -156,21 +156,21 @@ func NewRequestTelemetry(name string, timestamp time.Time, duration time.Duratio
data.Ver = 2

item := &RequestTelemetry{
data: data,
data: data,
}
item.timestamp = now
item.context = &telemetryContext{}
return item

item.timestamp = now
item.context = NewItemTelemetryContext()

return item
}

func (item *RequestTelemetry) Timestamp() time.Time {
return item.timestamp
}

func (item *RequestTelemetry) Context() TelemetryContext {
return item.context
return item.context
}

func (item *RequestTelemetry) baseTypeName() string {
Expand Down
55 changes: 55 additions & 0 deletions appinsights/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package appinsights

type DiagnosticsMessageWriter interface {
Write(string)
appendListener(*diagnosticsMessageListener)
}

type diagnosticsMessageWriter struct {
listeners []chan string
}

type DiagnosticsMessageProcessor func(string)

type DiagnosticsMessageListener interface {
ProcessMessages(DiagnosticsMessageProcessor)
}

type diagnosticsMessageListener struct {
channel chan string
}

var writer *diagnosticsMessageWriter = &diagnosticsMessageWriter{
listeners: make([]chan string, 0),
}

func getDiagnosticsMessageWriter() DiagnosticsMessageWriter {
return writer
}

func NewDiagnosticsMessageListener() DiagnosticsMessageListener {
listener := &diagnosticsMessageListener {
channel: make(chan string),
}

writer.appendListener(listener)

return listener
}

func (writer *diagnosticsMessageWriter) appendListener(listener *diagnosticsMessageListener) {
writer.listeners = append(writer.listeners, listener.channel)
}

func (writer *diagnosticsMessageWriter) Write(message string) {
for _, c := range writer.listeners {
c <- message
}
}

func (listener *diagnosticsMessageListener) ProcessMessages(process DiagnosticsMessageProcessor) {
for {
message := <- listener.channel
process(message)
}
}
42 changes: 42 additions & 0 deletions appinsights/diagnostics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package appinsights

import "testing"
import "sync"

func TestDiagnosticsWriterIsSingleton(t *testing.T) {
diagWriter1 := getDiagnosticsMessageWriter()
diagWriter2 := getDiagnosticsMessageWriter()

if diagWriter1 != diagWriter2 {
t.Errorf("getDiagnosticsMessageWriter() returned difference instances.")
}
}

func TestMessageSentToConsumers(t *testing.T) {
diagWriter := getDiagnosticsMessageWriter()

original := "test"

var wg sync.WaitGroup
wg.Add(2)

listener1 := NewDiagnosticsMessageListener()
go listener1.ProcessMessages(func(message string) {
if message != original {
t.Errorf("listener1 returned difference messages, want '%s' got '%s'.", original, message)
}
wg.Done()
})

listener2 := NewDiagnosticsMessageListener()
go listener2.ProcessMessages(func(message string) {
if message != original {
t.Errorf("listener2 returned difference messages, want '%s' got '%s'.", original, message)
}
wg.Done()
})

diagWriter.Write(original)

wg.Wait()
}
Loading

0 comments on commit 041faba

Please sign in to comment.