fix(core): update
This commit is contained in:
parent
484e5d51cc
commit
7d62306913
@ -22,6 +22,7 @@
|
|||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/lik": "^3.0.1",
|
||||||
"@pushrocks/smartlog-interfaces": "^2.0.2",
|
"@pushrocks/smartlog-interfaces": "^2.0.2",
|
||||||
"@pushrocks/smartrequest": "^1.1.14",
|
"@pushrocks/smartrequest": "^1.1.14",
|
||||||
"@pushrocks/smartstring": "^3.0.4",
|
"@pushrocks/smartstring": "^3.0.4",
|
||||||
|
54
ts/logdna.aggregator.ts
Normal file
54
ts/logdna.aggregator.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import * as plugins from './logdna.plugins';
|
||||||
|
import { LogdnaAccount } from './logdna.logdnaaccount';
|
||||||
|
|
||||||
|
export interface ILogCandidate {
|
||||||
|
urlIdentifier: string;
|
||||||
|
logLines: any[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LogAggregator {
|
||||||
|
private apiKey: string;
|
||||||
|
private baseUrl = 'https://logs.logdna.com/logs/ingest';
|
||||||
|
|
||||||
|
private logObjectMap = new plugins.lik.Objectmap<ILogCandidate>();
|
||||||
|
|
||||||
|
constructor(apiKeyArg: string) {
|
||||||
|
this.apiKey = apiKeyArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create basic authentication
|
||||||
|
*/
|
||||||
|
private createBasicAuth() {
|
||||||
|
const userNamePasswordString = `${this.apiKey}:`;
|
||||||
|
return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addLog(urlIdentifierArg: string, logLineArg: any) {
|
||||||
|
let existinglogCandidate = this.logObjectMap.find(logCandidate => {
|
||||||
|
return logCandidate.urlIdentifier === urlIdentifierArg;
|
||||||
|
});
|
||||||
|
if (!existinglogCandidate) {
|
||||||
|
existinglogCandidate = { urlIdentifier: urlIdentifierArg, logLines: [] };
|
||||||
|
this.logObjectMap.add(existinglogCandidate);
|
||||||
|
setTimeout(() => {
|
||||||
|
this.sendAggregatedLogs(existinglogCandidate);
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
existinglogCandidate.logLines.push(logLineArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async sendAggregatedLogs(logCandidate: ILogCandidate) {
|
||||||
|
this.logObjectMap.remove(logCandidate);
|
||||||
|
// lets post the message to logdna
|
||||||
|
await plugins.smartrequest.postJson(`${this.baseUrl}${logCandidate.urlIdentifier}&now=${Date.now()}` , {
|
||||||
|
headers: {
|
||||||
|
Authorization: this.createBasicAuth(),
|
||||||
|
charset: 'UTF-8'
|
||||||
|
},
|
||||||
|
requestBody: {
|
||||||
|
lines: logCandidate.logLines
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -65,7 +65,7 @@ export class LogdnaMessage {
|
|||||||
* create lgdna messages from smartlog package
|
* create lgdna messages from smartlog package
|
||||||
* @param smartlogPackageArg
|
* @param smartlogPackageArg
|
||||||
*/
|
*/
|
||||||
static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage {
|
public static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage {
|
||||||
return new LogdnaMessage({
|
return new LogdnaMessage({
|
||||||
timestamp: smartlogPackageArg.timestamp,
|
timestamp: smartlogPackageArg.timestamp,
|
||||||
line: smartlogPackageArg.message,
|
line: smartlogPackageArg.message,
|
||||||
|
@ -1,29 +1,21 @@
|
|||||||
import * as plugins from './logdna.plugins';
|
import * as plugins from './logdna.plugins';
|
||||||
|
|
||||||
import { LogdnaMessage } from './logdna.classes.logmessage';
|
import { LogdnaMessage } from './logdna.classes.logmessage';
|
||||||
|
import { LogAggregator } from './logdna.aggregator';
|
||||||
import { ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
import { ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the main logdna account
|
* the main logdna account
|
||||||
*/
|
*/
|
||||||
export class LogdnaAccount {
|
export class LogdnaAccount {
|
||||||
private apiKey: string;
|
private logAggregator: LogAggregator;
|
||||||
private baseUrl = 'https://logs.logdna.com/logs/ingest';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create basic authentication
|
|
||||||
*/
|
|
||||||
private createBasicAuth() {
|
|
||||||
const userNamePasswordString = `${this.apiKey}:`;
|
|
||||||
return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param apiKeyArg
|
* @param apiKeyArg
|
||||||
*/
|
*/
|
||||||
constructor(apiKeyArg: string) {
|
constructor(apiKeyArg: string) {
|
||||||
this.apiKey = apiKeyArg;
|
this.logAggregator = new LogAggregator(apiKeyArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,39 +27,27 @@ export class LogdnaAccount {
|
|||||||
const euc = encodeURIComponent;
|
const euc = encodeURIComponent;
|
||||||
|
|
||||||
// let construct the request uri
|
// let construct the request uri
|
||||||
const requestUrlWithParams = `${this.baseUrl}?hostname=${euc(lm.options.hostname)}&mac=${euc(
|
const requestUrlWithParams = `?hostname=${euc(lm.options.hostname)}&mac=${euc(
|
||||||
lm.options.mac
|
lm.options.mac
|
||||||
)}&ip=1${euc(lm.options.ip)}&now=${Date.now()}&tags=${euc(
|
)}&ip=1${euc(lm.options.ip)}&tags=${euc(
|
||||||
(() => {
|
(() => {
|
||||||
return lm.options.tags.reduce((reduced, newItem) => {
|
return lm.options.tags.reduce((reduced, newItem) => {
|
||||||
return `${reduced},${newItem}`;
|
return `${reduced},${newItem}`;
|
||||||
});
|
});
|
||||||
})()
|
})()
|
||||||
)}`;
|
)}`;
|
||||||
|
|
||||||
const requestBodyObject = {
|
const logLine = {
|
||||||
lines: [
|
timestamp: lm.options.timestamp,
|
||||||
{
|
line: lm.options.line,
|
||||||
timestamp: lm.options.timestamp,
|
app: lm.options.app,
|
||||||
line: lm.options.line,
|
level: lm.options.level,
|
||||||
app: lm.options.app,
|
env: lm.options.env,
|
||||||
level: lm.options.level,
|
meta: lm.options.meta
|
||||||
env: lm.options.env,
|
|
||||||
meta: lm.options.meta
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// console.log(requestBodyObject);
|
this.logAggregator.addLog(requestUrlWithParams, logLine);
|
||||||
|
|
||||||
// lets post the message to logdna
|
|
||||||
await plugins.smartrequest.postJson(requestUrlWithParams, {
|
|
||||||
headers: {
|
|
||||||
Authorization: this.createBasicAuth(),
|
|
||||||
charset: 'UTF-8'
|
|
||||||
},
|
|
||||||
requestBody: requestBodyObject
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
import * as lik from '@pushrocks/lik';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
import * as smartstring from '@pushrocks/smartstring';
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
export { smartrequest, smartstring, smartlogInterfaces };
|
export { lik, smartrequest, smartstring, smartlogInterfaces };
|
||||||
|
Loading…
Reference in New Issue
Block a user