Skip to content

Commit 2aceb70

Browse files
committed
feat: create new telemetry handle with connection strings
1 parent 3d0f319 commit 2aceb70

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

aitelemetry/telemetrywrapper.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func isPublicEnvironment(url string, retryCount, waitTimeInSecs int) (bool, erro
161161
return true, nil
162162
} else if err == nil {
163163
debugLog("[AppInsights] This is not azure public cloud:%s", cloudName)
164-
return false, fmt.Errorf("Not an azure public cloud: %s", cloudName)
164+
return false, fmt.Errorf("not an azure public cloud: %s", cloudName)
165165
}
166166

167167
debugLog("GetAzureCloud returned err :%v", err)
@@ -214,6 +214,42 @@ func NewAITelemetry(
214214
return th, nil
215215
}
216216

217+
// NewAITelemetry creates telemetry handle with user specified appinsights connection string.
218+
func NewAITelemetryWithConnectionString(
219+
cString string,
220+
aiConfig AIConfig,
221+
) (TelemetryHandle, error) {
222+
debugMode = aiConfig.DebugMode
223+
224+
if cString == "" {
225+
debugLog("Empty connection string")
226+
return nil, fmt.Errorf("AI connection string is empty")
227+
}
228+
229+
setAIConfigDefaults(&aiConfig)
230+
231+
telemetryConfig := appinsights.NewTelemetryConfigurationWithConnectionString(cString)
232+
telemetryConfig.MaxBatchSize = aiConfig.BatchSize
233+
telemetryConfig.MaxBatchInterval = time.Duration(aiConfig.BatchInterval) * time.Second
234+
235+
th := &telemetryHandle{
236+
client: appinsights.NewTelemetryClientFromConfig(telemetryConfig),
237+
appName: aiConfig.AppName,
238+
appVersion: aiConfig.AppVersion,
239+
diagListener: messageListener(),
240+
disableMetadataRefreshThread: aiConfig.DisableMetadataRefreshThread,
241+
refreshTimeout: aiConfig.RefreshTimeout,
242+
}
243+
244+
if th.disableMetadataRefreshThread {
245+
getMetadata(th)
246+
} else {
247+
go getMetadata(th)
248+
}
249+
250+
return th, nil
251+
}
252+
217253
// TrackLog function sends report (trace) to appinsights resource. It overrides few of the existing columns with app information
218254
// and for rest it uses custom dimesion
219255
func (th *telemetryHandle) TrackLog(report Report) {

aitelemetry/telemetrywrapper_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
hostAgentUrl = "localhost:3501"
2020
getCloudResponse = "AzurePublicCloud"
2121
httpURL = "http://" + hostAgentUrl
22+
connectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingestion.endpoint.com/;LiveEndpoint=https://live.endpoint.com/;ApplicationId=11111111-1111-1111-1111-111111111111"
2223
)
2324

2425
func TestMain(m *testing.M) {
@@ -89,7 +90,12 @@ func TestEmptyAIKey(t *testing.T) {
8990
}
9091
_, err = NewAITelemetry(httpURL, "", aiConfig)
9192
if err == nil {
92-
t.Errorf("Error intializing AI telemetry:%v", err)
93+
t.Errorf("Error initializing AI telemetry:%v", err)
94+
}
95+
96+
_, err = NewAITelemetryWithConnectionString("", aiConfig)
97+
if err == nil {
98+
t.Errorf("Error initializing AI telemetry with connection string:%v", err)
9399
}
94100
}
95101

@@ -107,9 +113,14 @@ func TestNewAITelemetry(t *testing.T) {
107113
DebugMode: true,
108114
DisableMetadataRefreshThread: true,
109115
}
110-
th, err = NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
111-
if th == nil {
112-
t.Errorf("Error intializing AI telemetry: %v", err)
116+
th1, err := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
117+
if th1 == nil {
118+
t.Errorf("Error initializing AI telemetry: %v", err)
119+
}
120+
121+
th2, err := NewAITelemetryWithConnectionString(connectionString, aiConfig)
122+
if th2 == nil {
123+
t.Errorf("Error initializing AI telemetry with connection string: %v", err)
113124
}
114125
}
115126

@@ -171,8 +182,14 @@ func TestClosewithoutSend(t *testing.T) {
171182

172183
thtest, err := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
173184
if thtest == nil {
174-
t.Errorf("Error intializing AI telemetry:%v", err)
185+
t.Errorf("Error initializing AI telemetry:%v", err)
186+
}
187+
188+
thtest2, err := NewAITelemetryWithConnectionString(connectionString, aiConfig)
189+
if thtest2 == nil {
190+
t.Errorf("Error initializing AI telemetry with connection string:%v", err)
175191
}
176192

177193
thtest.Close(10)
194+
thtest2.Close(10)
178195
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ require (
250250
)
251251

252252
replace (
253+
github.com/microsoft/ApplicationInsights-Go => github.com/beegiik/ApplicationInsights-Go v1.8.0
253254
github.com/onsi/ginkgo => github.com/onsi/ginkgo v1.12.0
254255
github.com/onsi/gomega => github.com/onsi/gomega v1.10.0
255256
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ github.com/avast/retry-go/v3 v3.1.1 h1:49Scxf4v8PmiQ/nY0aY3p0hDueqSmc7++cBbtiDGu
6767
github.com/avast/retry-go/v3 v3.1.1/go.mod h1:6cXRK369RpzFL3UQGqIUp9Q7GDrams+KsYWrfNA1/nQ=
6868
github.com/avast/retry-go/v4 v4.6.1 h1:VkOLRubHdisGrHnTu89g08aQEWEgRU7LVEop3GbIcMk=
6969
github.com/avast/retry-go/v4 v4.6.1/go.mod h1:V6oF8njAwxJ5gRo1Q7Cxab24xs5NCWZBeaHHBklR8mA=
70+
github.com/beegiik/ApplicationInsights-Go v1.8.0 h1:9eQ7wk7o03GA7HM/oDSOf/STOq5YA09cJfUiZa0yobU=
71+
github.com/beegiik/ApplicationInsights-Go v1.8.0/go.mod h1:wGv9tvjn4hfY0O95MzNM7ftYfphBi0BGqknkBdFF/cM=
7072
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
7173
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
7274
github.com/billgraziano/dpapi v0.5.0 h1:pcxA17vyjbDqYuxCFZbgL9tYIk2xgbRZjRaIbATwh+8=
@@ -348,6 +350,7 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
348350
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
349351
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
350352
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
353+
<<<<<<< HEAD
351354
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
352355
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
353356
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
@@ -356,6 +359,8 @@ github.com/microsoft/ApplicationInsights-Go v0.4.4 h1:G4+H9WNs6ygSCe6sUyxRc2U81T
356359
github.com/microsoft/ApplicationInsights-Go v0.4.4/go.mod h1:fKRUseBqkw6bDiXTs3ESTiU/4YTIHsQS4W3fP2ieF4U=
357360
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible h1:aKW/4cBs+yK6gpqU3K/oIwk9Q/XICqd3zOX/UFuvqmk=
358361
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
362+
=======
363+
>>>>>>> 4ec6dd349 (feat: create new telemetry handle with connection strings)
359364
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
360365
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
361366
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@@ -471,7 +476,10 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
471476
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
472477
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
473478
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
479+
<<<<<<< HEAD
474480
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
481+
=======
482+
>>>>>>> 4ec6dd349 (feat: create new telemetry handle with connection strings)
475483
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
476484
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
477485
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -481,6 +489,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
481489
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
482490
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
483491
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
492+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
484493
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
485494
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
486495
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=

0 commit comments

Comments
 (0)