From 85c80f24653f0f894168dbbd924942455bc6c347 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Thu, 13 Jun 2019 22:45:44 +0200 Subject: [PATCH] fix(analytics): don't use promises and silently fail --- garden-service/src/analytics/analytics.ts | 27 +++++++++++------------ garden-service/src/cli/cli.ts | 2 -- garden-service/src/server/server.ts | 1 - garden-service/src/task-graph.ts | 1 - 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/garden-service/src/analytics/analytics.ts b/garden-service/src/analytics/analytics.ts index 1a4bc2d40b..3b6287948e 100644 --- a/garden-service/src/analytics/analytics.ts +++ b/garden-service/src/analytics/analytics.ts @@ -104,6 +104,9 @@ export class AnalyticsHandler { firstRun: true, optedIn: false, } + this.localConfig = { + projectId: "", + } this.systemConfig = { platform: platform(), platformVersion: release(), @@ -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, @@ -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 } @@ -221,7 +220,7 @@ export class AnalyticsHandler { * @returns * @memberof Analytics */ - async trackCommand(commandName: string) { + trackCommand(commandName: string) { return this.track({ type: AnalyticsType.COMMAND, properties: { @@ -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), @@ -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, diff --git a/garden-service/src/cli/cli.ts b/garden-service/src/cli/cli.ts index ccb8eb7617..ddda55569b 100644 --- a/garden-service/src/cli/cli.ts +++ b/garden-service/src/cli/cli.ts @@ -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 diff --git a/garden-service/src/server/server.ts b/garden-service/src/server/server.ts index 70f3940bea..57f589bd05 100644 --- a/garden-service/src/server/server.ts +++ b/garden-service/src/server/server.ts @@ -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? diff --git a/garden-service/src/task-graph.ts b/garden-service/src/task-graph.ts index d2532e404e..8499282dd7 100644 --- a/garden-service/src/task-graph.ts +++ b/garden-service/src/task-graph.ts @@ -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)