Skip to content

Commit

Permalink
fix(analytics): don't use promises and silently fail
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jun 13, 2019
1 parent b174dc4 commit 85c80f2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
27 changes: 13 additions & 14 deletions garden-service/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export class AnalyticsHandler {
firstRun: true,
optedIn: false,
}
this.localConfig = {
projectId: "",
}
this.systemConfig = {
platform: platform(),
platformVersion: release(),
Expand Down Expand Up @@ -181,15 +184,13 @@ export class AnalyticsHandler {

/**
* The actual segment track method.
* The segment client works with callbacks, therefore the need of wrapping the function
* with Promises.
*
* @private
* @param {AnalyticsEvent} event The event to track
* @returns
* @memberof Analytics
*/
private async track(event: AnalyticsEvent) {
private track(event: AnalyticsEvent) {
if (this.segment && this.hasOptedIn() && !ci.isCI) {
const segmentEvent: SegmentEvent = {
userId: this.globalConfig.userId,
Expand All @@ -200,16 +201,14 @@ export class AnalyticsHandler {
}

const trackToRemote = (eventToTrack: SegmentEvent) => {
return new Promise(
(resolve, reject) => {
this.segment.track(eventToTrack, function(error) {
if (error) { reject(error) }
resolve(true)
})
})
this.segment.track(eventToTrack, (err) => {
if (err) {
this.garden.log.debug(`Error sending tracking event: ${err}`)
}
})
}

return await trackToRemote(segmentEvent)
return trackToRemote(segmentEvent)
}
return false
}
Expand All @@ -221,7 +220,7 @@ export class AnalyticsHandler {
* @returns
* @memberof Analytics
*/
async trackCommand(commandName: string) {
trackCommand(commandName: string) {
return this.track({
type: AnalyticsType.COMMAND,
properties: {
Expand All @@ -240,7 +239,7 @@ export class AnalyticsHandler {
* @returns
* @memberof Analytics
*/
async trackTask(taskName: string, taskType: string) {
trackTask(taskName: string, taskType: string) {
const properties: AnalyticsTaskEventProperties = {
name: taskType,
taskName: md5(taskName),
Expand All @@ -264,7 +263,7 @@ export class AnalyticsHandler {
* @returns
* @memberof Analytics
*/
async trackApi(method: string, path: string, body: ApiRequestBody) {
trackApi(method: string, path: string, body: ApiRequestBody) {
const properties: AnalyticsApiEventProperties = {
name: `${method} request`,
path,
Expand Down
2 changes: 0 additions & 2 deletions garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ export class GardenCli {

// Init Analytics, track command if user opted-in
const analytics = await new AnalyticsHandler(garden).init()

// tslint:disable-next-line: no-floating-promises
analytics.trackCommand(command.getFullName())

// TODO: enforce that commands always output DeepPrimitiveMap
Expand Down
1 change: 0 additions & 1 deletion garden-service/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class GardenServer {
this.analytics = await new AnalyticsHandler(this.garden).init()
}

// tslint:disable-next-line: no-floating-promises
this.analytics.trackApi("POST", ctx.originalUrl, { ...ctx.request.body })

// TODO: set response code when errors are in result object?
Expand Down
1 change: 0 additions & 1 deletion garden-service/src/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export class TaskGraph {
result = await node.process(dependencyResults)

// Track task if user has opted-in
// tslint:disable-next-line: no-floating-promises
analytics.trackTask(result.key, result.type)

this.garden.events.emit("taskComplete", result)
Expand Down

0 comments on commit 85c80f2

Please sign in to comment.