Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
3bd8ebb29d | |||
2429e7b7c1 | |||
2bf5665fea | |||
7d62306913 | |||
484e5d51cc | |||
a8bac181a4 | |||
76d3926ba4 | |||
0310c03ecb | |||
9e8efc7dea |
8
package-lock.json
generated
8
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/logdna",
|
"name": "@mojoio/logdna",
|
||||||
"version": "1.0.13",
|
"version": "1.0.18",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -137,9 +137,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@pushrocks/smartlog-interfaces": {
|
"@pushrocks/smartlog-interfaces": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-2.0.2.tgz",
|
||||||
"integrity": "sha512-c9onE52z/5fGX5uEvaI/rXbcC6n7PkLrNjehRM+6JsK7HIbdAzrgY1PGrqUfW0a03hSe03mFcggORID+fQI4tA=="
|
"integrity": "sha512-kJNQ/6kfljgtwebhoiD8WtRWfdVhOoE1nr8FoUJLlOjLphU8SPa42Hg6/yPkSTaGxWwDhk6PkMJl64O7HNjRUQ=="
|
||||||
},
|
},
|
||||||
"@pushrocks/smartpath": {
|
"@pushrocks/smartpath": {
|
||||||
"version": "4.0.1",
|
"version": "4.0.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/logdna",
|
"name": "@mojoio/logdna",
|
||||||
"version": "1.0.13",
|
"version": "1.0.18",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "anunoffical package for the logdna api",
|
"description": "anunoffical package for the logdna api",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -22,7 +22,8 @@
|
|||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartlog-interfaces": "^2.0.1",
|
"@pushrocks/lik": "^3.0.1",
|
||||||
|
"@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",
|
||||||
"@pushrocks/taskbuffer": "^2.0.5"
|
"@pushrocks/taskbuffer": "^2.0.5"
|
||||||
|
32
test/test.ts
32
test/test.ts
@ -2,6 +2,7 @@ import { expect, tap } from '@pushrocks/tapbundle';
|
|||||||
import * as logdna from '../ts/index';
|
import * as logdna from '../ts/index';
|
||||||
|
|
||||||
import { Qenv } from '@pushrocks/qenv';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
|
import { ILogPackage } from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
const testQenv = new Qenv('./', './.nogit');
|
const testQenv = new Qenv('./', './.nogit');
|
||||||
|
|
||||||
@ -14,7 +15,8 @@ tap.test('should create a valid logDna account', async () => {
|
|||||||
|
|
||||||
tap.test('should create a standard log message', async () => {
|
tap.test('should create a standard log message', async () => {
|
||||||
testLogMessage = logdna.LogdnaMessage.fromSmartLogPackage({
|
testLogMessage = logdna.LogdnaMessage.fromSmartLogPackage({
|
||||||
type: 'log',
|
timestamp: Date.now(),
|
||||||
|
type: 'log',
|
||||||
level: 'info',
|
level: 'info',
|
||||||
context: {
|
context: {
|
||||||
company: 'Lossless GmbH',
|
company: 'Lossless GmbH',
|
||||||
@ -22,9 +24,9 @@ tap.test('should create a standard log message', async () => {
|
|||||||
containerName: 'ci-mojoio-logdna',
|
containerName: 'ci-mojoio-logdna',
|
||||||
environment: 'test',
|
environment: 'test',
|
||||||
runtime: 'node',
|
runtime: 'node',
|
||||||
zone: 'ship.zone'
|
zone: 'shipzone'
|
||||||
},
|
},
|
||||||
message: 'this is an awesome log message :)'
|
message: 'this is an awesome log message sent by the tapbundle test'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -32,4 +34,28 @@ tap.test('should send the message', async () => {
|
|||||||
await testLogDnaAccount.sendLogDnaMessage(testLogMessage);
|
await testLogDnaAccount.sendLogDnaMessage(testLogMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should send in order', async () => {
|
||||||
|
let i = 1;
|
||||||
|
while (i < 21) {
|
||||||
|
const testSmartlogMessage: ILogPackage = {
|
||||||
|
timestamp: Date.now(),
|
||||||
|
type: 'log',
|
||||||
|
level: 'info',
|
||||||
|
context: {
|
||||||
|
company: 'Lossless GmbH',
|
||||||
|
companyunit: 'lossless.cloud',
|
||||||
|
containerName: 'ci-mojoio-logdna',
|
||||||
|
environment: 'test',
|
||||||
|
runtime: 'node',
|
||||||
|
zone: 'shipzone'
|
||||||
|
},
|
||||||
|
message: `this is an awesome log message sent by the tapbundle test #${i}`
|
||||||
|
};
|
||||||
|
testLogDnaAccount.sendSmartlogPackage(testSmartlogMessage);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,11 @@ import { TLogLevel, TEnvironment, ILogPackage } from '@pushrocks/smartlog-interf
|
|||||||
* the constructor options for LogdnaMessage
|
* the constructor options for LogdnaMessage
|
||||||
*/
|
*/
|
||||||
export interface ILogdnaMessageContructorOptions {
|
export interface ILogdnaMessageContructorOptions {
|
||||||
|
/**
|
||||||
|
* a timestamp for then the log message was created
|
||||||
|
*/
|
||||||
|
timestamp: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the hostname where the log message was created
|
* the hostname where the log message was created
|
||||||
*/
|
*/
|
||||||
@ -60,8 +65,9 @@ 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,
|
||||||
line: smartlogPackageArg.message,
|
line: smartlogPackageArg.message,
|
||||||
meta: {
|
meta: {
|
||||||
...smartlogPackageArg.context,
|
...smartlogPackageArg.context,
|
||||||
@ -70,7 +76,7 @@ export class LogdnaMessage {
|
|||||||
env: smartlogPackageArg.context.environment,
|
env: smartlogPackageArg.context.environment,
|
||||||
hostname: smartlogPackageArg.context.zone,
|
hostname: smartlogPackageArg.context.zone,
|
||||||
level: smartlogPackageArg.level,
|
level: smartlogPackageArg.level,
|
||||||
app: smartlogPackageArg.context.zone,
|
app: smartlogPackageArg.context.containerName,
|
||||||
tags: (() => {
|
tags: (() => {
|
||||||
const tagArray: string[] = [];
|
const tagArray: string[] = [];
|
||||||
tagArray.push(smartlogPackageArg.context.company);
|
tagArray.push(smartlogPackageArg.context.company);
|
||||||
|
@ -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,52 +27,42 @@ 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()}`;
|
)}&ip=1${euc(lm.options.ip)}&tags=${euc(
|
||||||
|
(() => {
|
||||||
const requestBodyObject = {
|
return lm.options.tags.reduce((reduced, newItem) => {
|
||||||
lines: [
|
return `${reduced},${newItem}`;
|
||||||
{
|
});
|
||||||
line: lm.options.line,
|
})()
|
||||||
app: lm.options.app,
|
)}`;
|
||||||
level: lm.options.level,
|
|
||||||
env: lm.options.env,
|
const logLine = {
|
||||||
meta: lm.options.meta,
|
timestamp: lm.options.timestamp,
|
||||||
tags: (() => {
|
line: lm.options.line,
|
||||||
return lm.options.tags.reduce((reduced, newItem) => {
|
app: lm.options.app,
|
||||||
return `${reduced},${newItem}`;
|
level: lm.options.level,
|
||||||
});
|
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
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* convenience function for smartlog
|
* convenience function for smartlog
|
||||||
*/
|
*/
|
||||||
public async sendSmartlogPackage (smartlogPackageArg: ILogPackage) {
|
public async sendSmartlogPackage(smartlogPackageArg: ILogPackage) {
|
||||||
this.sendLogDnaMessage(LogdnaMessage.fromSmartLogPackage(smartlogPackageArg));
|
this.sendLogDnaMessage(LogdnaMessage.fromSmartLogPackage(smartlogPackageArg));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a smartlog compatible log destination
|
* returns a smartlog compatible log destination
|
||||||
*/
|
*/
|
||||||
public get smartlogDestination (): ILogDestination {
|
public get smartlogDestination(): ILogDestination {
|
||||||
return {
|
return {
|
||||||
handleLog: (logPackageArg) => {
|
handleLog: logPackageArg => {
|
||||||
this.sendSmartlogPackage(logPackageArg);
|
this.sendSmartlogPackage(logPackageArg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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 };
|
||||||
|
Reference in New Issue
Block a user