Skip to content

Commit 50f3bf6

Browse files
committed
feat: Create connection string helper function and update telemetry handle
1 parent a1f3cb6 commit 50f3bf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+172
-7867
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package aitelemetry
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type connectionVars struct {
9+
InstrumentationKey string
10+
IngestionUrl string
11+
}
12+
13+
func parseConnectionString(connectionString string) (*connectionVars, error) {
14+
connectionVars := &connectionVars{}
15+
16+
if connectionString == "" {
17+
return nil, fmt.Errorf("Connection string cannot be empty")
18+
}
19+
20+
pairs := strings.Split(connectionString, ";")
21+
for _, pair := range pairs {
22+
kv := strings.SplitN(pair, "=", 2)
23+
if len(kv) != 2 {
24+
return nil, fmt.Errorf("Invalid connection string format: %s", pair)
25+
}
26+
key, value := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
27+
28+
if key == "" {
29+
return nil, fmt.Errorf("Key in connection string cannot be empty")
30+
}
31+
32+
switch strings.ToLower(key) {
33+
case "instrumentationkey":
34+
connectionVars.InstrumentationKey = value
35+
case "ingestionendpoint":
36+
connectionVars.IngestionUrl = value + "v2.1/track"
37+
}
38+
}
39+
40+
if connectionVars.InstrumentationKey == "" || connectionVars.IngestionUrl == "" {
41+
return nil, fmt.Errorf("Missing required fields in connection string")
42+
}
43+
44+
return connectionVars, nil
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package aitelemetry
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
const connectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingestion.endpoint.com/;LiveEndpoint=https://live.endpoint.com/;ApplicationId=11111111-1111-1111-1111-111111111111"
10+
11+
func TestParseConnectionString(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
connectionString string
15+
want *connectionVars
16+
wantErr bool
17+
}{
18+
{
19+
name: "Valid connection string and instrumentation key",
20+
connectionString: connectionString,
21+
want: &connectionVars{
22+
InstrumentationKey: "00000000-0000-0000-0000-000000000000",
23+
IngestionUrl: "https://ingestion.endpoint.com/v2.1/track",
24+
},
25+
wantErr: false,
26+
},
27+
{
28+
name: "Invalid connection string format",
29+
connectionString: "InvalidConnectionString",
30+
want: nil,
31+
wantErr: true,
32+
},
33+
{
34+
name: "Valid instrumentation key with missing ingestion endpoint",
35+
connectionString: "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=;",
36+
want: nil,
37+
wantErr: true,
38+
},
39+
{
40+
name: "Missing instrumentation key with valid ingestion endpoint",
41+
connectionString: "InstrumentationKey=;IngestionEndpoint=https://ingestion.endpoint.com/;LiveEndpoint=https://live.endpoint.com/;",
42+
want: nil,
43+
wantErr: true,
44+
},
45+
{
46+
name: "Empty connection string",
47+
connectionString: "",
48+
want: nil,
49+
wantErr: true,
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
got, err := parseConnectionString(tt.connectionString)
56+
if tt.wantErr {
57+
require.Error(t, err, "Expected error but got none")
58+
} else {
59+
require.NoError(t, err, "Expected no error but got one")
60+
require.NotNil(t, got, "Expected a non-nil result")
61+
require.Equal(t, tt.want.InstrumentationKey, got.InstrumentationKey, "Instrumentation Key does not match")
62+
require.Equal(t, tt.want.IngestionUrl, got.IngestionUrl, "Ingestion URL does not match")
63+
}
64+
})
65+
}
66+
}

aitelemetry/telemetrywrapper.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,14 @@ func NewAITelemetryWithConnectionString(
228228

229229
setAIConfigDefaults(&aiConfig)
230230

231-
telemetryConfig := appinsights.NewTelemetryConfigurationWithConnectionString(cString)
231+
connectionVars, err := parseConnectionString(cString)
232+
if err != nil {
233+
debugLog("Error parsing connection string: %v", err)
234+
return nil, err
235+
}
236+
237+
telemetryConfig := appinsights.NewTelemetryConfiguration(connectionVars.InstrumentationKey)
238+
telemetryConfig.EndpointUrl = connectionVars.IngestionUrl
232239
telemetryConfig.MaxBatchSize = aiConfig.BatchSize
233240
telemetryConfig.MaxBatchInterval = time.Duration(aiConfig.BatchInterval) * time.Second
234241

aitelemetry/telemetrywrapper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +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"
22+
// connectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingestion.endpoint.com/;LiveEndpoint=https://live.endpoint.com/;ApplicationId=11111111-1111-1111-1111-111111111111"
2323
)
2424

2525
func TestMain(m *testing.M) {

application-insights/.gitignore

Lines changed: 0 additions & 31 deletions
This file was deleted.

application-insights/.travis.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

application-insights/CODE_OF_CONDUCT.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

application-insights/CONTRIBUTING.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

application-insights/LICENSE

Lines changed: 0 additions & 22 deletions
This file was deleted.

application-insights/PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)