Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[93] fix: Connected Grafana Api to the response-time-interceptor #150

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f4ec971
feat(wip): move response time interceptor to use grafana api
techsavvyash Mar 10, 2024
125e67d
feat(wip): interceptor changes w api
techsavvyash Mar 15, 2024
3b0e08e
fix: connected grafana api with response-time-interceptor
DarrenDsouza7273 Jul 4, 2024
3fa0a9c
Added Monitoring
DarrenDsouza7273 Jul 6, 2024
9154a6d
fix: fixed dashboard generation to row level generation
Savio629 Jul 25, 2024
eae1619
Merge branch 'dev' of https://github.com/DarrenDsouza7273/stencil int…
Savio629 Jul 25, 2024
f3bde9c
fix: removed extra repo
Savio629 Jul 25, 2024
03b87d2
fix: updated test
Savio629 Jul 27, 2024
9d0d4a1
fix: added grafana level test
Savio629 Jul 28, 2024
04dbe76
fix: fixed the failing ci
Savio629 Aug 10, 2024
b4560f4
fix: updated the naming convection
Savio629 Aug 18, 2024
10660ff
fix: updated test
Savio629 Aug 20, 2024
9913ebe
fix: updated test and increased the coverage
Savio629 Sep 22, 2024
e0f6699
Merge branch 'Response-time-grafana' of https://github.com/DarrenDsou…
Savio629 Sep 22, 2024
db9a51d
fix: updated suggested changes
Savio629 Sep 22, 2024
4b9e67a
fix: updated test
Savio629 Sep 24, 2024
e528e29
fix: updated ci test
Savio629 Sep 24, 2024
6e4dc55
fix: fixed ci test
Savio629 Sep 24, 2024
0a6a022
fix: updating the failing ci
Savio629 Sep 24, 2024
a51767e
fix: updated ci test
Savio629 Sep 24, 2024
f472c86
fix: updated test
Savio629 Sep 24, 2024
683deb5
fix: updated test
Savio629 Sep 24, 2024
0cb6522
fix: failing test
Savio629 Sep 28, 2024
abe17ae
fix: updated yarn.lock file
Savio629 Oct 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/common/src/controllers/prometheus.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Controller, Get, Res } from '@nestjs/common';
import { FastifyReply } from 'fastify';
import { register } from 'prom-client';

@Controller()
export class PrometheusController {
@Get('metrics')
async metrics(@Res() response: FastifyReply) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static Analysis: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods. You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

-  async metrics(@Res() response: FastifyReply) {
+  async metrics(response: FastifyReply) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async metrics(@Res() response: FastifyReply) {
async metrics(response: FastifyReply) {
Tools
Biome

[error] 8-8: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods.
You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

(parse)

response.headers({ 'Content-Type': register.contentType });
response.header('Content-Type', register.contentType);
response.send(await register.metrics());
}
}
110 changes: 79 additions & 31 deletions packages/common/src/interceptors/response-time.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,65 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
Logger,
} from '@nestjs/common';
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, Logger } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { Histogram, exponentialBuckets } from 'prom-client';
import * as fs from 'fs';
import { generateBaseJSON, generateRow } from './utils';
import axios from 'axios';

import {
generateBaseJSON,
generateRow,
getDashboardByUID,
getDashboardJSON,
} from './utils';

@Injectable()
export class ResponseTimeInterceptor implements NestInterceptor {
private histogram: Histogram;
private logger: Logger;
private dashboardUid: string;
private grafanaBaseURL: string;
private apiToken: string;

constructor(histogramTitle: string, jsonPath: string) {
constructor(
histogramTitle: string,
grafanaBaseURL: string,
apiToken: string,
) {
const name = histogramTitle + '_response_time';
this.logger = new Logger(name + '_interceptor');
this.dashboardUid = null;
this.grafanaBaseURL = grafanaBaseURL;
this.apiToken = apiToken;
this.init(histogramTitle, grafanaBaseURL);
}

async init(histogramTitle: string, grafanaBaseURL: string) {
const name = histogramTitle + '_response_time';
this.histogram = new Histogram({
name: name,
help: 'Response time of APIs',
buckets: exponentialBuckets(1, 1.5, 30),
labelNames: ['statusCode', 'endpoint'],
});

// updating the grafana JSON with the row for this panel
console.log("apiToken", this.apiToken);
console.log("grafanaBaseURL", grafanaBaseURL);
try {
// check if the path exists or not?
if (!fs.existsSync(jsonPath)) {
fs.writeFileSync(jsonPath, JSON.stringify(generateBaseJSON()), 'utf8'); // create file if not exists
}
const dashboardJSONSearchResp = await getDashboardJSON(
this.apiToken,
histogramTitle,
grafanaBaseURL,
);
this.dashboardUid =
dashboardJSONSearchResp.length > 0
? dashboardJSONSearchResp[0]['uid']
: undefined;
let parsedContent: any;

const parsedContent = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
if (this.dashboardUid === null || !this.dashboardUid) {
parsedContent = generateBaseJSON(histogramTitle);

let isPresent = false;

// skip creating the row if it already exists -- prevents multiple panels/rows on app restarts
let isPresent = false;
parsedContent.panels.forEach((panel: any) => {
// TODO: Make this verbose and add types -- convert the grafana JSON to TS Types/interface
parsedContent.dashboard.panels.forEach((panel: any) => {
if (
panel.title.trim() ===
name
Expand All @@ -52,13 +72,43 @@ export class ResponseTimeInterceptor implements NestInterceptor {
}
});
if (isPresent) return;
parsedContent.dashboard.panels.push(generateRow(name));
} else {
parsedContent = await getDashboardByUID(this.dashboardUid, grafanaBaseURL, this.apiToken);


let isPresent = false;
parsedContent.panels.forEach((panel: any) => {
if (
panel.title.trim() ===
name
.split('_')
.map((str) => str.charAt(0).toUpperCase() + str.slice(1))
.join(' ')
.trim()
) {
isPresent = true;
}
});
if (isPresent) return;

parsedContent.panels.push(generateRow(name));
}


parsedContent.panels.push(generateRow(name));
// write back to file
fs.writeFileSync(jsonPath, JSON.stringify(parsedContent));

const FINAL_JSON = parsedContent;
await axios.post(`${grafanaBaseURL}/api/dashboards/db`, FINAL_JSON, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiToken}`
}
});

this.logger.log('Successfully added histogram to dashboard!');
} catch (err) {
this.logger.error('Error updating grafana JSON!', err);
console.error('Error updating grafana JSON!', err);
}
}

Expand All @@ -70,26 +120,24 @@ export class ResponseTimeInterceptor implements NestInterceptor {
const startTime = performance.now();
return next.handle().pipe(
tap(() => {
// handles when there is no error propagating from the services to the controller
const endTime = performance.now();
const responseTime = endTime - startTime;
const statusCode = response.statusCode;
const endpoint = request.url;
this.histogram.labels({ statusCode, endpoint }).observe(responseTime);
}),
catchError((err) => {
// handles when an exception is to be returned to the client
this.logger.error('error: ', err);
const endTime = performance.now();
const responseTime = endTime - startTime;
const endpoint = request.url;
this.histogram
.labels({ statusCode: err.status, endpoint })
.observe(responseTime);
this.histogram.labels({ statusCode: err.status, endpoint }).observe(responseTime);
return throwError(() => {
throw err;
});
}),
);
}
}



165 changes: 122 additions & 43 deletions packages/common/src/interceptors/utils.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,90 @@
export const generateBaseJSON = () => {
import axios from 'axios';

export const generateBaseJSON = (histogramTitle) => {
return {
annotations: {
list: [
{
builtIn: 1,
datasource: {
type: 'grafana',
uid: '-- Grafana --',
},
enable: true,
hide: true,
iconColor: 'rgba(0, 211, 255, 1)',
name: 'Annotations & Alerts',
type: 'dashboard',
meta: {
type: "db",
canSave: true,
canEdit: true,
canAdmin: true,
canStar: true,
canDelete: true,
slug: histogramTitle,
url: `/d/ec1b884b/${histogramTitle}`,
expires: "0001-01-01T00:00:00Z",
created: "2024-07-03T04:01:29Z",
updated: "2024-07-03T04:01:29Z",
updatedBy: "admin",
createdBy: "admin",
version: 1,
hasAcl: false,
isFolder: false,
folderId: 0,
folderUid: "",
folderTitle: "General",
folderUrl: "",
provisioned: false,
provisionedExternalId: "",
annotationsPermissions: {
dashboard: {
canAdd: true,
canEdit: true,
canDelete: true,
},
],
},
description: 'Dashboard for API response time visualisations\n',
editable: true,
fiscalYearStartMonth: 0,
graphTooltip: 0,
id: 5,
links: [],
liveNow: false,
panels: [],
refresh: '',
schemaVersion: 38,
tags: [],
templating: {
list: [],
organization: {
canAdd: true,
canEdit: true,
canDelete: true,
},
},
},
time: {
from: 'now-6h',
to: 'now',
dashboard: {
annotations: {
list: [
{
builtIn: 1,
datasource: {
type: 'grafana',
uid: '-- Grafana --',
},
enable: true,
hide: true,
iconColor: 'rgba(0, 211, 255, 1)',
name: 'Annotations & Alerts',
type: 'dashboard',
},
],
},
description: 'Dashboard for API response time visualisations\n',
editable: true,
fiscalYearStartMonth: 0,
graphTooltip: 0,
id: null,
links: [],
liveNow: false,
panels: [],
refresh: '',
schemaVersion: 38,
tags: [],
templating: {
list: [],
},
time: {
from: 'now-6h',
to: 'now',
},
timepicker: {},
timezone: '',
title: histogramTitle,
uid: null,
version: 1,
weekStart: '',
},
timepicker: {},
timezone: '',
title: 'Response Times',
uid: 'ec1b884b-753e-405f-8f4b-e0a5d97cf167',
version: 6,
weekStart: '',
};
};


export const generateRow = (name: string) => {
const textPanel = generateTextPanel(
name,
// `# ${name} Response Time Information\n`,
name,
);
const gauagePanel = generateGaugePanel(name);
const heatmap = generateHeatmap(name);
const successfulUnsuccessful = generateSuccessfulUnSuccessfulPanel(name);
Expand Down Expand Up @@ -570,3 +606,46 @@ export const generateAverageResponseTimePanel = (label: string) => {
type: 'timeseries',
};
};





export const getDashboardByUID = async (
uid: string,
grafanaBaseURL: string,
apiToken : string
) => {
try {
const resp = await axios.get(grafanaBaseURL + `/api/dashboards/uid/${uid}`, {
headers: {
'Authorization': `Bearer ${apiToken}`
}
});
return resp.data.dashboard;
} catch (err) {
console.error('error getting dashboard by uid: ', err);
}
return ;
};

export const getDashboardJSON = async (
token: string,
dashboardTitle: string,
grafanaBaseURL: string,
) => {
try {
const resp: any = await axios.get(grafanaBaseURL + '/api/search', {
headers: {
Authorization: `Bearer ${token}`,
},
});
return (resp.data as any[]).filter((item) => {
return item.title && item.title === dashboardTitle;
});
} catch (err) {
console.error('Error searching dashboards: ', err);
}
return;
};

This file was deleted.

1 change: 0 additions & 1 deletion packages/nestjs-monitor/monitor/response_times_base.json

This file was deleted.

3 changes: 3 additions & 0 deletions sample/02-monitoring/env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

GRAFANA_API_TOKEN="" #your grafana api token
GRAFANA_BASE_URL="http://localhost:7889"

This file was deleted.

1 change: 0 additions & 1 deletion sample/02-monitoring/monitor/response_times_base.json

This file was deleted.

2 changes: 1 addition & 1 deletion sample/02-monitoring/nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@samagra-x/schematics",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
Expand Down
Loading
Loading