Compare commits

...

15 Commits

Author SHA1 Message Date
4febec9e89 1.0.22 2018-11-05 23:52:40 +01:00
b65f3af9cf fix(core): update 2018-11-05 23:52:39 +01:00
373b02adb4 1.0.21 2018-11-05 23:41:43 +01:00
11b08cc0a3 fix(core): update 2018-11-05 23:41:43 +01:00
a586069305 1.0.20 2018-11-05 23:29:01 +01:00
8a8c4efcbb fix(core): update 2018-11-05 23:29:01 +01:00
dbb5843f52 1.0.19 2018-11-05 23:25:53 +01:00
d021e934b9 fix(readme): update 2018-11-05 23:25:53 +01:00
3bd8ebb29d 1.0.18 2018-11-04 21:21:34 +01:00
2429e7b7c1 fix(core): update 2018-11-04 21:21:34 +01:00
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
8 changed files with 199 additions and 56 deletions

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/logdna", "name": "@mojoio/logdna",
"version": "1.0.14", "version": "1.0.22",
"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",

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/logdna", "name": "@mojoio/logdna",
"version": "1.0.14", "version": "1.0.22",
"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"

68
readme.md Normal file
View File

@ -0,0 +1,68 @@
# @mojoio/logdna
anunoffical package for the logdna api
## Availabililty
[![npm](https://mojoio.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/@mojoio/logdna)
[![git](https://mojoio.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/mojoio/logdna)
[![git](https://mojoio.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/mojoio/logdna)
[![docs](https://mojoio.gitlab.io/assets/repo-button-docs.svg)](https://mojoio.gitlab.io/logdna/)
## Status for master
[![build status](https://GitLab.com/mojoio/logdna/badges/master/build.svg)](https://GitLab.com/mojoio/logdna/commits/master)
[![coverage report](https://GitLab.com/mojoio/logdna/badges/master/coverage.svg)](https://GitLab.com/mojoio/logdna/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@mojoio/logdna.svg)](https://www.npmjs.com/package/@mojoio/logdna)
[![Known Vulnerabilities](https://snyk.io/test/npm/@mojoio/logdna/badge.svg)](https://snyk.io/test/npm/@mojoio/logdna)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
Use TypeScript for best in class instellisense.
This package is an unofficial package for the logdna. It comes with the following festures:
* aggregates logs that require the same uri query parameters and sends them as bundle. This ensures the correct order of logs
* resends logs that failed to send.
* supports smartlog messages and the smartlog ecosystem
```typescript
import { ILogPackage } from '@pushrocks/smartlog-interfaces'
import { LogdnaAccount, LogdnaMessage } from '@mojoio/logdna';
// lets create a logDnaAccount
const logDnaAccount = new LogdnaAccount(process.env.LOGDNA_APIKEY);
// lets create a smartlog message (smartlog normally takes care of creating those objects)
const smartlogPackage: 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'
};
const logdnaMessage = LogdnaMessage.fromSmartLogPackage(smartlogPackage);
logDnaAccount.sendLogdnaMessage(logdnaMessage);
// alternatively simply send the smartlogPackage
// creation of the LogdnaMessage is done for you
logDnaAccount.sendSmartlogPackage(smartlogPackage)
// most of the above funtions return promises should you want to wait for a log to be fully sent
```
For further information read the linked docs at the top of this README.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://mojoio.gitlab.io/assets/repo-footer.svg)](https://mojo.io)

View File

@ -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',
@ -24,12 +26,36 @@ tap.test('should create a standard log message', async () => {
runtime: 'node', runtime: 'node',
zone: 'shipzone' zone: 'shipzone'
}, },
message: 'this is an awesome log message :)' message: 'this is an awesome log message sent by the tapbundle test'
}); });
}); });
tap.test('should send the message', async () => { 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();

57
ts/logdna.aggregator.ts Normal file
View File

@ -0,0 +1,57 @@
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
const response = await plugins.smartrequest.postJson(`${this.baseUrl}${logCandidate.urlIdentifier}&now=${Date.now()}` , {
headers: {
Authorization: this.createBasicAuth(),
charset: 'UTF-8'
},
requestBody: {
lines: logCandidate.logLines
}
});
if(response.statusCode !== 200) {
console.log(response.body);
}
}
}

View File

@ -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,

View File

@ -1,86 +1,70 @@
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);
} }
/** /**
* sends a logmessage * sends a logmessage
* async, so it returns a Promise. In most cases it does not make sense to wait for it though. * async, so it returns a Promise. In most cases it does not make sense to wait for it though.
*/ */
public async sendLogDnaMessage(logdnaMessageArg: LogdnaMessage) { public async sendLogdnaMessage(logdnaMessageArg: LogdnaMessage) {
const lm = logdnaMessageArg; const lm = logdnaMessageArg;
const euc = encodeURIComponent; const euc = encodeURIComponent;
// let construct the request uri const uriHostname = euc(lm.options.hostname);
const requestUrlWithParams = `${this.baseUrl}?hostname=${euc(lm.options.hostname)}&mac=${euc( const uriMac = euc(lm.options.mac);
lm.options.mac const uriIp = euc(lm.options.ip);
)}&ip=1${euc(lm.options.ip)}&now=${Date.now()}`; const uriTags = euc(
(() => {
return lm.options.tags.reduce((reduced, newItem) => {
return `${reduced},${newItem}`;
});
})()
);
const requestBodyObject = { // let construct the request uri
lines: [ const requestUrlWithParams = `?hostname=${uriHostname}&mac=${uriMac}&ip=1${uriIp}&tags=${uriTags}`;
{
line: lm.options.line, const logLine = {
app: lm.options.app, timestamp: lm.options.timestamp,
level: lm.options.level, line: lm.options.line,
env: lm.options.env, app: lm.options.app,
meta: lm.options.meta, level: lm.options.level,
tags: (() => { env: lm.options.env,
return lm.options.tags.reduce((reduced, newItem) => { meta: lm.options.meta
return `${reduced},${newItem}`;
});
})()
}
]
}; };
// 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);
} }
}; };

View File

@ -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 };