Skip to content

Commit fa88a0b

Browse files
committed
feat: create new telemetry handle with connection strings
1 parent 8d75d8b commit fa88a0b

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

aitelemetry/telemetrywrapper.go

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

164164
debugLog("GetAzureCloud returned err :%v", err)
@@ -211,6 +211,42 @@ func NewAITelemetry(
211211
return th, nil
212212
}
213213

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

190190
replace (
191+
github.com/microsoft/ApplicationInsights-Go => github.com/beegiik/ApplicationInsights-Go v1.8.0
191192
github.com/onsi/ginkgo => github.com/onsi/ginkgo v1.12.0
192193
github.com/onsi/gomega => github.com/onsi/gomega v1.10.0
193194
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ github.com/avast/retry-go/v3 v3.1.1 h1:49Scxf4v8PmiQ/nY0aY3p0hDueqSmc7++cBbtiDGu
5757
github.com/avast/retry-go/v3 v3.1.1/go.mod h1:6cXRK369RpzFL3UQGqIUp9Q7GDrams+KsYWrfNA1/nQ=
5858
github.com/avast/retry-go/v4 v4.6.1 h1:VkOLRubHdisGrHnTu89g08aQEWEgRU7LVEop3GbIcMk=
5959
github.com/avast/retry-go/v4 v4.6.1/go.mod h1:V6oF8njAwxJ5gRo1Q7Cxab24xs5NCWZBeaHHBklR8mA=
60+
github.com/beegiik/ApplicationInsights-Go v1.8.0 h1:9eQ7wk7o03GA7HM/oDSOf/STOq5YA09cJfUiZa0yobU=
61+
github.com/beegiik/ApplicationInsights-Go v1.8.0/go.mod h1:wGv9tvjn4hfY0O95MzNM7ftYfphBi0BGqknkBdFF/cM=
6062
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
6163
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
6264
github.com/billgraziano/dpapi v0.5.0 h1:pcxA17vyjbDqYuxCFZbgL9tYIk2xgbRZjRaIbATwh+8=
@@ -267,8 +269,6 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
267269
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
268270
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
269271
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
270-
github.com/microsoft/ApplicationInsights-Go v0.4.4 h1:G4+H9WNs6ygSCe6sUyxRc2U81TI5Es90b2t/MwX5KqY=
271-
github.com/microsoft/ApplicationInsights-Go v0.4.4/go.mod h1:fKRUseBqkw6bDiXTs3ESTiU/4YTIHsQS4W3fP2ieF4U=
272272
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
273273
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
274274
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@@ -356,13 +356,15 @@ github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqj
356356
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
357357
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
358358
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
359+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
359360
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
360361
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
361362
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
362363
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
363364
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
364365
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
365366
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
367+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
366368
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
367369
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
368370
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=

0 commit comments

Comments
 (0)