Compare commits

..

19 Commits

Author SHA1 Message Date
2bf5665fea 1.0.17 2018-11-04 21:11:48 +01:00
7d62306913 fix(core): update 2018-11-04 21:11:48 +01:00
484e5d51cc 1.0.16 2018-11-04 18:44:28 +01:00
a8bac181a4 fix(core): update 2018-11-04 18:44:28 +01:00
76d3926ba4 1.0.15 2018-11-04 15:49:03 +01:00
0310c03ecb 1.0.14 2018-11-04 15:37:55 +01:00
9e8efc7dea fix(core): update 2018-11-04 15:37:55 +01:00
00e1b8d862 1.0.13 2018-11-04 15:25:27 +01:00
c5cedb027d fix(core): update 2018-11-04 15:25:27 +01:00
f075f7f23a 1.0.12 2018-11-04 14:56:25 +01:00
809df76043 fix(core): update 2018-11-04 14:56:25 +01:00
d1dde293f6 1.0.11 2018-11-04 14:24:13 +01:00
b7ee6bb9a6 fix(smartlog): add getSmartlogDestination() 2018-11-04 14:24:12 +01:00
15cfa8fe88 1.0.10 2018-11-04 02:41:46 +01:00
1e222c7ad8 fix(core): update 2018-11-04 02:41:45 +01:00
91a1571635 1.0.9 2018-11-04 00:05:36 +01:00
2e39906fd2 fix(core): update 2018-11-04 00:05:36 +01:00
7950cb1649 1.0.8 2018-11-03 23:32:31 +01:00
b085dc32db fix(core): update 2018-11-03 23:32:31 +01:00
8 changed files with 117 additions and 54 deletions

4
.snyk Normal file
View File

@ -0,0 +1,4 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.12.0
ignore: {}
patch: {}

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/logdna",
"version": "1.0.7",
"version": "1.0.17",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -137,9 +137,9 @@
}
},
"@pushrocks/smartlog-interfaces": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-2.0.0.tgz",
"integrity": "sha512-rk3uEp78AXLULS81SUe6YtZvyQiDImuJu/zxnIzzUFDB6ciisqtJ1qVcHYbVsW/kImeo8vBFlQyKY9/YaNgkDw=="
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-2.0.2.tgz",
"integrity": "sha512-kJNQ/6kfljgtwebhoiD8WtRWfdVhOoE1nr8FoUJLlOjLphU8SPa42Hg6/yPkSTaGxWwDhk6PkMJl64O7HNjRUQ=="
},
"@pushrocks/smartpath": {
"version": "4.0.1",

View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/logdna",
"version": "1.0.7",
"version": "1.0.17",
"private": false,
"description": "anunoffical package for the logdna api",
"main": "dist/index.js",
@ -22,7 +22,8 @@
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {
"@pushrocks/smartlog-interfaces": "^2.0.0",
"@pushrocks/lik": "^3.0.1",
"@pushrocks/smartlog-interfaces": "^2.0.2",
"@pushrocks/smartrequest": "^1.1.14",
"@pushrocks/smartstring": "^3.0.4",
"@pushrocks/taskbuffer": "^2.0.5"

View File

@ -14,7 +14,8 @@ tap.test('should create a valid logDna account', async () => {
tap.test('should create a standard log message', async () => {
testLogMessage = logdna.LogdnaMessage.fromSmartLogPackage({
type: 'log',
timestamp: Date.now(),
type: 'log',
level: 'info',
context: {
company: 'Lossless GmbH',
@ -22,9 +23,9 @@ tap.test('should create a standard log message', async () => {
containerName: 'ci-mojoio-logdna',
environment: 'test',
runtime: 'node',
zone: 'ship.zone'
zone: 'shipzone'
},
message: 'this is a awesome log message :)'
message: 'this is an awesome log message sent by the tapbundle test'
});
});

54
ts/logdna.aggregator.ts Normal file
View 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
}
});
}
}

View File

@ -6,6 +6,11 @@ import { TLogLevel, TEnvironment, ILogPackage } from '@pushrocks/smartlog-interf
* the constructor options for LogdnaMessage
*/
export interface ILogdnaMessageContructorOptions {
/**
* a timestamp for then the log message was created
*/
timestamp: number;
/**
* the hostname where the log message was created
*/
@ -60,14 +65,18 @@ export class LogdnaMessage {
* create lgdna messages from smartlog package
* @param smartlogPackageArg
*/
static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage {
public static fromSmartLogPackage(smartlogPackageArg: ILogPackage): LogdnaMessage {
return new LogdnaMessage({
timestamp: smartlogPackageArg.timestamp,
line: smartlogPackageArg.message,
meta: smartlogPackageArg.context,
meta: {
...smartlogPackageArg.context,
logType: smartlogPackageArg.type
},
env: smartlogPackageArg.context.environment,
hostname: smartlogPackageArg.context.zone,
level: smartlogPackageArg.level,
app: smartlogPackageArg.context.zone,
app: smartlogPackageArg.context.containerName,
tags: (() => {
const tagArray: string[] = [];
tagArray.push(smartlogPackageArg.context.company);

View File

@ -1,29 +1,21 @@
import * as plugins from './logdna.plugins';
import { LogdnaMessage } from './logdna.classes.logmessage';
import { ILogPackage } from '@pushrocks/smartlog-interfaces';
import { LogAggregator } from './logdna.aggregator';
import { ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
/**
* the main logdna account
*/
export class LogdnaAccount {
private apiKey: string;
private baseUrl = 'https://logs.logdna.com/logs/ingest';
/**
* Create basic authentication
*/
private createBasicAuth() {
const userNamePasswordString = `${this.apiKey}:`;
return `Basic ${plugins.smartstring.base64.encode(userNamePasswordString)}`;
}
private logAggregator: LogAggregator;
/**
*
* @param apiKeyArg
*/
constructor(apiKeyArg: string) {
this.apiKey = apiKeyArg;
this.logAggregator = new LogAggregator(apiKeyArg);
}
/**
@ -35,43 +27,44 @@ export class LogdnaAccount {
const euc = encodeURIComponent;
// 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
)}&ip=1${euc(lm.options.ip)}&now=${Date.now()}`;
const requestBodyObject = {
lines: [
{
line: lm.options.line,
app: lm.options.app,
level: lm.options.level,
env: lm.options.env,
meta: lm.options.meta,
tags: (() => {
return lm.options.tags.reduce((reduced, newItem) => {
return `${reduced},${newItem}`;
});
})()
}
]
)}&ip=1${euc(lm.options.ip)}&tags=${euc(
(() => {
return lm.options.tags.reduce((reduced, newItem) => {
return `${reduced},${newItem}`;
});
})()
)}`;
const logLine = {
timestamp: lm.options.timestamp,
line: lm.options.line,
app: lm.options.app,
level: lm.options.level,
env: lm.options.env,
meta: lm.options.meta
};
// console.log(requestBodyObject);
// lets post the message to logdna
await plugins.smartrequest.postJson(requestUrlWithParams, {
headers: {
Authorization: this.createBasicAuth(),
charset: 'UTF-8'
},
requestBody: requestBodyObject
});
this.logAggregator.addLog(requestUrlWithParams, logLine);
}
/**
* convenience function for smartlog
*/
async sendSmartlogPackage (smartlogPackageArg: ILogPackage) {
public async sendSmartlogPackage(smartlogPackageArg: ILogPackage) {
this.sendLogDnaMessage(LogdnaMessage.fromSmartLogPackage(smartlogPackageArg));
}
/**
* returns a smartlog compatible log destination
*/
public get smartlogDestination(): ILogDestination {
return {
handleLog: logPackageArg => {
this.sendSmartlogPackage(logPackageArg);
}
};
}
}

View File

@ -1,5 +1,6 @@
import * as lik from '@pushrocks/lik';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartstring from '@pushrocks/smartstring';
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
export { smartrequest, smartstring, smartlogInterfaces };
export { lik, smartrequest, smartstring, smartlogInterfaces };