Skip to content

Commit 6937e53

Browse files
[POA-1346] Add serviceID in telemetry events (#19)
In this PR, we are adding support to send `serviceID` as an event property for the telemetry events _(wherever possible)_ Follow changes are done: * Add a `serviceID` var and `SetServiceID()` public function, which is used by `apidump`, `ecs` and `kube` commands to set serviceID once it is parsed from the command line * Done changes in the `CommandLine()` event function to fetch _(using regexp)_ and parse `serviceID` from command line data since this function is called before execution of main command function Amplitude PR, with changes in events: https://app.amplitude.com/data/postman/Postman/branch-activity/POA-1346/latest
1 parent c196466 commit 6937e53

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

telemetry/telemetry.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"fmt"
66
"os"
77
"os/user"
8+
"regexp"
89
"strconv"
910
"sync"
1011
"time"
1112

13+
"github.com/akitasoftware/akita-libs/akid"
1214
"github.com/akitasoftware/akita-libs/analytics"
1315
"github.com/akitasoftware/go-utils/maps"
1416
"github.com/postmanlabs/postman-insights-agent/cfg"
@@ -33,6 +35,9 @@ var (
3335
userID string
3436
teamID string
3537

38+
serviceID akid.ServiceID
39+
serviceIDRegex = regexp.MustCompile(`^svc_[A-Za-z0-9]{22}$`)
40+
3641
// Timeout talking to API.
3742
// Shorter than normal because we don't want the CLI to be slow.
3843
userAPITimeout = 2 * time.Second
@@ -106,7 +111,7 @@ func doInit() {
106111
IsLoggingEnabled: false,
107112
},
108113
App: analytics.AppInfo{
109-
Name: "akita-cli",
114+
Name: "postman-insights-agent",
110115
Version: version.ReleaseVersion().String(),
111116
Build: version.GitVersion(),
112117
Namespace: "",
@@ -300,7 +305,16 @@ func WorkflowStep(workflow string, message string) {
300305
}
301306

302307
// Report command line flags (before any error checking.)
308+
// This event will be sent before any other events and only once per agent invocation.
303309
func CommandLine(command string, commandLine []string) {
310+
// Look for a service ID in the command line, and assign it to package level variable.
311+
for _, arg := range commandLine {
312+
if serviceIDRegex.MatchString(arg) {
313+
_ = akid.ParseIDAs(arg, &serviceID)
314+
break
315+
}
316+
}
317+
304318
tryTrackingEvent(
305319
"Command - Executed",
306320
map[string]any{
@@ -333,8 +347,8 @@ func Shutdown() {
333347
}
334348
}
335349

336-
// Attempts to track an event using the provided event name and properties. It adds the user ID
337-
// and team ID to the event properties, and then sends the event to the analytics client.
350+
// Attempts to track an event using the provided event name and properties. It adds the user ID,
351+
// team ID and service ID to the event properties, and then sends the event to the analytics client.
338352
// If there is an error sending the event, a warning message is printed.
339353
func tryTrackingEvent(eventName string, eventProperties maps.Map[string, any]) {
340354
// precondition: analyticsClient is initialized
@@ -346,6 +360,10 @@ func tryTrackingEvent(eventName string, eventProperties maps.Map[string, any]) {
346360
eventProperties.Upsert("team_id", teamID, func(v, newV any) any { return v })
347361
}
348362

363+
if serviceID != (akid.ServiceID{}) {
364+
eventProperties.Upsert("service_id", serviceID, func(v, newV any) any { return v })
365+
}
366+
349367
err := analyticsClient.Track(userID, eventName, eventProperties)
350368
if err != nil {
351369
printer.Warningf("Error sending analytics event %q: %v\n", eventName, err)

0 commit comments

Comments
 (0)