-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start moving gatsby-telemetry to typescript (#25812)
* Fix camelCase * Start moving gatsby-telemetry to ts * Continue converting base to typescript * Debug transpilation issues * Debug transpilation issues * Fix telemetry tests
- Loading branch information
1 parent
b4da0c8
commit 72be86f
Showing
10 changed files
with
162 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...tsby-telemetry/src/__tests__/telemetry.js → ...tsby-telemetry/src/__tests__/telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type UUID = string | ||
|
||
declare namespace NodeJS { | ||
interface Process { | ||
gatsbyTelemetrySessionId: UUID; | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { AnalyticsTracker, IAggregateStats } from "./telemetry" | ||
import * as express from "express" | ||
|
||
const instance = new AnalyticsTracker() | ||
|
||
const flush = require(`./flush`)(instance.isTrackingEnabled()) | ||
|
||
process.on(`exit`, flush) | ||
|
||
// For long running commands we want to occasionally flush the data | ||
// | ||
// The data is also sent on exit. | ||
|
||
const intervalDuration = process.env.TELEMETRY_BUFFER_INTERVAL | ||
const interval = | ||
intervalDuration && Number.isFinite(+intervalDuration) | ||
? Math.max(Number(intervalDuration), 1000) | ||
: 10 * 60 * 1000 // 10 min | ||
|
||
function tick(): void { | ||
flush() | ||
.catch(console.error) | ||
.then(() => setTimeout(tick, interval)) | ||
} | ||
|
||
module.exports = { | ||
trackCli: (input, tags, opts): void => | ||
instance.captureEvent(input, tags, opts), | ||
trackError: (input, tags): void => instance.captureError(input, tags), | ||
trackBuildError: (input, tags): void => | ||
instance.captureBuildError(input, tags), | ||
setDefaultTags: (tags): void => instance.decorateAll(tags), | ||
decorateEvent: (event, tags): void => instance.decorateNextEvent(event, tags), | ||
setTelemetryEnabled: (enabled): void => instance.setTelemetryEnabled(enabled), | ||
startBackgroundUpdate: (): void => { | ||
setTimeout(tick, interval) | ||
}, | ||
isTrackingEnabled: (): boolean => instance.isTrackingEnabled(), | ||
aggregateStats: (data): IAggregateStats => instance.aggregateStats(data), | ||
addSiteMeasurement: (event, obj): void => | ||
instance.addSiteMeasurement(event, obj), | ||
expressMiddleware: function (source: string) { | ||
return function ( | ||
_req: express.Request, | ||
_res: express.Response, | ||
next | ||
): void { | ||
try { | ||
instance.trackActivity(`${source}_ACTIVE`) | ||
} catch (e) { | ||
// ignore | ||
} | ||
next() | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { AnalyticsTracker } from "./telemetry" | ||
const instance = new AnalyticsTracker() | ||
|
||
function flush(): void { | ||
instance.sendEvents().catch(_e => { | ||
// ignore | ||
}) | ||
} | ||
|
||
flush() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.