@spinajs/log-source-graphana-loki 2.0.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.js +134 -0
- package/lib/index.js.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IInstanceCheck } from "@spinajs/di";
|
|
3
|
+
import { ILog, ILogEntry, LogTarget, ICommonTargetOptions } from "@spinajs/log-common";
|
|
4
|
+
export interface IGraphanaOptions extends ICommonTargetOptions {
|
|
5
|
+
options: {
|
|
6
|
+
interval: number;
|
|
7
|
+
bufferSize: number;
|
|
8
|
+
timeout: number;
|
|
9
|
+
host: string;
|
|
10
|
+
labels: {
|
|
11
|
+
app: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
declare enum TargetStatus {
|
|
16
|
+
WRITTING = 0,
|
|
17
|
+
PENDING = 1,
|
|
18
|
+
IDLE = 2
|
|
19
|
+
}
|
|
20
|
+
export declare class GraphanaLokiLogTarget extends LogTarget<IGraphanaOptions> implements IInstanceCheck {
|
|
21
|
+
protected Log: ILog;
|
|
22
|
+
protected Entries: ILogEntry[];
|
|
23
|
+
protected Status: TargetStatus;
|
|
24
|
+
protected FlushTimer: NodeJS.Timer;
|
|
25
|
+
constructor(options: IGraphanaOptions);
|
|
26
|
+
__checkInstance__(creationOptions: IGraphanaOptions): boolean;
|
|
27
|
+
resolve(): void;
|
|
28
|
+
write(data: ILogEntry): void;
|
|
29
|
+
dispose(): Promise<void>;
|
|
30
|
+
protected flush(): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.GraphanaLokiLogTarget = void 0;
|
|
16
|
+
/* eslint-disable promise/always-return */
|
|
17
|
+
/* eslint-disable security/detect-object-injection */
|
|
18
|
+
const configuration_common_1 = require("@spinajs/configuration-common");
|
|
19
|
+
const di_1 = require("@spinajs/di");
|
|
20
|
+
const log_common_1 = require("@spinajs/log-common");
|
|
21
|
+
const log_1 = require("@spinajs/log");
|
|
22
|
+
const axios_1 = __importDefault(require("axios"));
|
|
23
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
24
|
+
var TargetStatus;
|
|
25
|
+
(function (TargetStatus) {
|
|
26
|
+
TargetStatus[TargetStatus["WRITTING"] = 0] = "WRITTING";
|
|
27
|
+
TargetStatus[TargetStatus["PENDING"] = 1] = "PENDING";
|
|
28
|
+
TargetStatus[TargetStatus["IDLE"] = 2] = "IDLE";
|
|
29
|
+
})(TargetStatus || (TargetStatus = {}));
|
|
30
|
+
// we mark per instance check becouse we can have multiple file targes
|
|
31
|
+
// for different files/paths/logs but we dont want to create every time writer for same.
|
|
32
|
+
let GraphanaLokiLogTarget = class GraphanaLokiLogTarget extends log_common_1.LogTarget {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super(options);
|
|
35
|
+
this.Entries = [];
|
|
36
|
+
this.Status = TargetStatus.IDLE;
|
|
37
|
+
this.Options.options = Object.assign({
|
|
38
|
+
interval: 3000,
|
|
39
|
+
bufferSize: 10,
|
|
40
|
+
timeout: 1000,
|
|
41
|
+
}, this.Options.options);
|
|
42
|
+
}
|
|
43
|
+
__checkInstance__(creationOptions) {
|
|
44
|
+
return this.Options.name === creationOptions.name;
|
|
45
|
+
}
|
|
46
|
+
resolve() {
|
|
47
|
+
var _a;
|
|
48
|
+
this.FlushTimer = setInterval(() => {
|
|
49
|
+
// do not flush, if we already writting to file
|
|
50
|
+
if (this.Status !== TargetStatus.IDLE) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
setImmediate(() => {
|
|
54
|
+
this.flush();
|
|
55
|
+
});
|
|
56
|
+
}, (_a = this.Options.options.interval) !== null && _a !== void 0 ? _a : 3000);
|
|
57
|
+
}
|
|
58
|
+
write(data) {
|
|
59
|
+
if (!this.Options.enabled) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
data.Variables["n_timestamp"] = new Date().getTime() * 1000000;
|
|
63
|
+
this.Entries.push(data);
|
|
64
|
+
// if we already writting, skip buffer check & write to file
|
|
65
|
+
// wait until write is finished
|
|
66
|
+
if (this.Status !== TargetStatus.IDLE) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (this.Entries.length >= this.Options.options.bufferSize) {
|
|
70
|
+
this.Status = TargetStatus.PENDING;
|
|
71
|
+
// write at end of nodejs event loop all buffered messages at once
|
|
72
|
+
setImmediate(() => {
|
|
73
|
+
this.flush();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async dispose() {
|
|
78
|
+
// stop flush timer
|
|
79
|
+
clearInterval(this.FlushTimer);
|
|
80
|
+
// write all messages from buffer
|
|
81
|
+
this.flush();
|
|
82
|
+
}
|
|
83
|
+
flush() {
|
|
84
|
+
const batch = [];
|
|
85
|
+
if (this.Entries.length === 0) {
|
|
86
|
+
this.Status = TargetStatus.IDLE;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.Status = TargetStatus.WRITTING;
|
|
90
|
+
this.Entries.forEach((entry) => {
|
|
91
|
+
let stream = batch.find((b) => lodash_1.default.isEqual(b.labels, Object.assign({ app: this.Options.options.labels.app, logger: entry.Variables.logger, level: entry.Variables.level }, this.Options.options.labels)));
|
|
92
|
+
if (!stream) {
|
|
93
|
+
stream = {
|
|
94
|
+
labels: Object.assign({ app: this.Options.options.labels.app, logger: entry.Variables.logger, level: entry.Variables.level }, this.Options.options.labels),
|
|
95
|
+
values: [],
|
|
96
|
+
};
|
|
97
|
+
batch.push(stream);
|
|
98
|
+
}
|
|
99
|
+
stream.values.push([
|
|
100
|
+
entry.Variables["n_timestamp"],
|
|
101
|
+
JSON.stringify((0, configuration_common_1.format)(entry.Variables, this.Options.layout)),
|
|
102
|
+
]);
|
|
103
|
+
});
|
|
104
|
+
axios_1.default
|
|
105
|
+
.post(this.Options.options.host + "/loki/api/v1/push", {
|
|
106
|
+
headers: {
|
|
107
|
+
"Content-Type": "application/json",
|
|
108
|
+
},
|
|
109
|
+
timeout: this.Options.options.timeout,
|
|
110
|
+
data: batch,
|
|
111
|
+
})
|
|
112
|
+
.then(() => {
|
|
113
|
+
this.Entries = [];
|
|
114
|
+
this.Status = TargetStatus.IDLE;
|
|
115
|
+
this.Log.trace(`Wrote buffered messages to graphana target at url ${this.Options.options.host}`);
|
|
116
|
+
})
|
|
117
|
+
.catch((err) => {
|
|
118
|
+
// log error message to others if applicable eg. console
|
|
119
|
+
this.Log.error(err, `Cannot write log messages to graphana target`);
|
|
120
|
+
this.Status = TargetStatus.IDLE;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
__decorate([
|
|
125
|
+
(0, log_1.Logger)("LogLokiTarget"),
|
|
126
|
+
__metadata("design:type", Object)
|
|
127
|
+
], GraphanaLokiLogTarget.prototype, "Log", void 0);
|
|
128
|
+
GraphanaLokiLogTarget = __decorate([
|
|
129
|
+
(0, di_1.PerInstanceCheck)(),
|
|
130
|
+
(0, di_1.Injectable)("GraphanaLogTarget"),
|
|
131
|
+
__metadata("design:paramtypes", [Object])
|
|
132
|
+
], GraphanaLokiLogTarget);
|
|
133
|
+
exports.GraphanaLokiLogTarget = GraphanaLokiLogTarget;
|
|
134
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0CAA0C;AAC1C,qDAAqD;AACrD,wEAAuD;AACvD,oCAA2E;AAC3E,oDAK6B;AAC7B,sCAAsC;AAEtC,kDAA0B;AAC1B,oDAAuB;AAwBvB,IAAK,YAIJ;AAJD,WAAK,YAAY;IACf,uDAAQ,CAAA;IACR,qDAAO,CAAA;IACP,+CAAI,CAAA;AACN,CAAC,EAJI,YAAY,KAAZ,YAAY,QAIhB;AAED,sEAAsE;AACtE,wFAAwF;AAGxF,IAAa,qBAAqB,GAAlC,MAAa,qBACX,SAAQ,sBAA2B;IAYnC,YAAY,OAAyB;QACnC,KAAK,CAAC,OAAO,CAAC,CAAC;QAPP,YAAO,GAAgB,EAAE,CAAC;QAE1B,WAAM,GAAiB,YAAY,CAAC,IAAI,CAAC;QAOjD,IAAI,CAAC,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAClC;YACE,QAAQ,EAAE,IAAI;YACd,UAAU,EAAE,EAAE;YACd,OAAO,EAAE,IAAI;SACd,EACD,IAAI,CAAC,OAAO,CAAC,OAAO,CACrB,CAAC;IACJ,CAAC;IAED,iBAAiB,CAAC,eAAiC;QACjD,OAAO,IAAI,CAAC,OAAO,CAA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C,CAAC,CAAC;QAEH,eAAK;aACF,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG,mBAAmB,EAAE;YACrD,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO;YACrC,IAAI,EAAE,KAAK;SACZ,CAAC;aACD,IAAI,CAAC,GAAG,EAAE;YACT,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;YAClB,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC;YAEhC,IAAI,CAAC,GAAG,CAAC,KAAK,CACZ,qDAAqD,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CACjF,CAAC;QACJ,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,wDAAwD;YACxD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,+CAA+C,CAAC,CAAC;YACrE,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC;QAClC,CAAC,CAAC,CAAC;IACP,CAAC;CACF,CAAA;AAnIC;IADC,IAAA,YAAM,EAAC,eAAe,CAAC;;kDACJ;AALT,qBAAqB;IAFjC,IAAA,qBAAgB,GAAE;IAClB,IAAA,eAAU,EAAC,mBAAmB,CAAC;;GACnB,qBAAqB,CAwIjC;AAxIY,sDAAqB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "2.0.44",
|
|
3
|
+
"description": "Log lib for all spinejs related libs",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"private": false,
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "ts-mocha -p tsconfig.json test/**/*.test.ts",
|
|
8
|
+
"coverage": "nyc npm run test",
|
|
9
|
+
"build-docs": "rimraf docs && typedoc --options typedoc.json src/",
|
|
10
|
+
"build": "npm run clean && npm run compile",
|
|
11
|
+
"compile": "tsc -p tsconfig.build.json",
|
|
12
|
+
"clean": "",
|
|
13
|
+
"prepare": "npm run build",
|
|
14
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
15
|
+
"lint": "eslint -c .eslintrc.js --ext .ts src",
|
|
16
|
+
"prepublishOnly": "npm test && npm run lint",
|
|
17
|
+
"preversion": "npm run lint",
|
|
18
|
+
"version": "npm run format && git add -A src",
|
|
19
|
+
"postversion": "git push && git push --tags"
|
|
20
|
+
},
|
|
21
|
+
"author": "SpinaJS <spinajs@coderush.pl> (https://github.com/spinajs/log)",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"name": "@spinajs/log-source-graphana-loki",
|
|
24
|
+
"homepage": "https://github.com/spinajs/main#readme",
|
|
25
|
+
"directories": {
|
|
26
|
+
"lib": "lib",
|
|
27
|
+
"test": "__tests__"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"lib"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/spinajs/main.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/spinajs/main/issues"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "1857a6e3626e0fed22ffb0b44b48d587c79898db",
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@spinajs/configuration-common": "^2.0.44",
|
|
45
|
+
"@spinajs/di": "^2.0.44",
|
|
46
|
+
"@spinajs/log": "^2.0.44",
|
|
47
|
+
"@spinajs/log-common": "^2.0.44",
|
|
48
|
+
"axios": "^1.2.2",
|
|
49
|
+
"lodash": "^4.17.21"
|
|
50
|
+
}
|
|
51
|
+
}
|