Skip to content

Commit

Permalink
Merge pull request #81 from rafaelkallis/feat-telemetryevents
Browse files Browse the repository at this point in the history
telemetry events
  • Loading branch information
rafaelkallis committed Mar 27, 2021
2 parents 7e9a1a7 + 7cb62bc commit 9f92240
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

const express = require("express");
const { Webhooks } = require("@octokit/webhooks");
const appInsights = require("applicationinsights");
const { Classifier } = require("./classifier");
const github = require("./github");
const config = require("./config");
const telemetry = require("./telemetry");

module.exports = async function App() {
const app = express();
Expand Down Expand Up @@ -64,12 +64,12 @@ module.exports = async function App() {
accessToken,
});

// appInsights.defaultClient.trackEvent({ name: "Classified" });
telemetry.event("Classified");
}
});

webhooks.on("installation.created", async () => {
appInsights.defaultClient.trackEvent({ name: "Installed" });
telemetry.event("Installed");
});
app.use(webhooks.middleware);

Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@

const appInsights = require("applicationinsights");
const config = require("./config");
const telemetry = require("./telemetry");

if (config.isDevelopment) {
telemetry.attachConsole();
}

if (config.APPINSIGHTS_INSTRUMENTATIONKEY) {
appInsights
.setup(config.APPINSIGHTS_INSTRUMENTATIONKEY)
.setSendLiveMetrics(true)
.start();
telemetry.attachAppInsights();
}

const App = require("./app");
Expand Down
52 changes: 52 additions & 0 deletions src/telemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Ticket Tagger automatically predicts and labels issue types.
* Copyright (C) 2018,2019,2020 Rafael Kallis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @file telemetry
* @author Rafael Kallis <[email protected]>
*/

"use strict";

const EventEmitter = require("events");
const appInsights = require("applicationinsights");

class Telemetry {
constructor() {
this.emitter = new EventEmitter();
}

event(name) {
this.emitter.emit("event", name);
}

onEvent(handler) {
this.emitter.on("event", handler);
}

attachConsole() {
this.onEvent((name) => console.info(`Event emitted: ${name}`));
}

attachAppInsights() {
if (!appInsights.defaultClient) {
return;
}
this.onEvent((name) => appInsights.defaultClient.trackEvent({ name }));
}
}

module.exports = new Telemetry();

0 comments on commit 9f92240

Please sign in to comment.