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

Feat/grafana tools #21

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions src/grafana/lokiclient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { LokiClient } from './lokiclient';
import 'dotenv/config';
const lokiApiKey = process.env.LOKI_API_KEY!;
const user = process.env.LOKI_USER!;
const lokiUrl = process.env.LOKI_URL!;
const isGithubActions = process.env.GITHUB_ACTIONS === 'true';
const maybe = !isGithubActions ? describe : describe.skip;
jest.setTimeout(30000);
maybe('LokiClient', () => {
let lokiClient: LokiClient;

beforeAll(() => {
lokiClient = new LokiClient(lokiUrl, lokiApiKey, user, "staging");
});

it ('can count logs by level for a service', async () => {
const service = "checkly-api";
const rangeMinutes = 60*12;
const data = await lokiClient.getLogCountByLevel(service, rangeMinutes);
expect(data).toBeDefined();
console.log(JSON.stringify(data.data.result));
expect(data).toHaveProperty('data');
//console.log(JSON.stringify(data.data.result[0].values));
})

it('should get available services', async () => {
const services = await lokiClient.getAllValuesForLabel("app");
expect(services).toBeDefined();
expect(services.length).toBeGreaterThan(0);
//console.log(services);
});

it('should run a query and return results', async () => {
const services = lokiClient.getAllValuesForLabel("app");
const data = await lokiClient.getErrorsForService(services[1], 10);
expect(data).toBeDefined();
expect(data).toHaveProperty('data');
//console.log(JSON.stringify(data.data.result[0].values));
});
});

94 changes: 94 additions & 0 deletions src/grafana/lokiclient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export class LokiClient {
private readonly lokiUrl: string;
private readonly lokiApiKey: string;
private readonly environment: string;
user: string;

constructor(lokiUrl: string, lokiApiKey: string, user: string, environment: string) {
this.lokiUrl = lokiUrl;
this.lokiApiKey = lokiApiKey;
this.environment = environment;
this.user = user;
}

queryError(service: string): string {
return `{app="${service}", env="${this.environment}"} |= "error"`;
}

async getLogCountByLevel(app: string, rangeMinutes: number): Promise<any> {
const query = `sum by (detected_level) (count_over_time({app="${app}", env="${this.environment}"}[5m]))`;
const end = new Date();
const start = new Date(end.getTime() - rangeMinutes * 60 * 1000);
const data = await this.queryLoki(query, start.toISOString(), end.toISOString());
return data;
}

async getAllEnvironments(): Promise<string[]> {
return this.getAllValuesForLabel('env');
}


async getAllApps(): Promise<string[]> {
return this.getAllValuesForLabel('app');
}

/**
* This function gets all available values for a label in Loki.
* @returns
*/
async getAllValuesForLabel(label: string): Promise<string[]> {
const url = new URL(`${this.lokiUrl}/loki/api/v1/label/${label}/values`);
const authHeader = 'Basic ' + btoa(`${this.user}:${this.lokiApiKey}`);

const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader
}
});

if (!response.ok) {
throw new Error(`Error fetching available services: ${response.statusText}`);
}

const data = await response.json();
return data.data; // Assuming the response structure is { "status": "success", "data": ["app1", "app2", ...] }
}

async getErrorsForService(service: string, rangeMinutes: number) {
// Get the current time and subtract "rangeMinutes" minutes
const end = new Date();
const start = new Date(end.getTime() - rangeMinutes * 60 * 1000);

// Convert to ISO string format
const startISOString = start.toISOString();
const endISOString = end.toISOString();
const query = this.queryError(service);
return this.queryLoki(query, startISOString, endISOString);
}
async queryLoki(query: string, start: string, end: string): Promise<any> {
const url = new URL(`${this.lokiUrl}/loki/api/v1/query_range`);
url.searchParams.append('query', query);
url.searchParams.append('start', start);
url.searchParams.append('end', end);
const authHeader = 'Basic ' + btoa(`${this.user}:${this.lokiApiKey}`);

const response = await fetch(url.toString(), {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: authHeader,
},
});

if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Error querying Loki: ${response.status} ${response.statusText} - ${errorText}`,
);
}
//https://grafana.com/docs/loki/latest/reference/loki-http-api/#query-logs-within-a-range-of-time
return response.json();
}
}