@loglayer/transport-victoria-logs 1.0.1
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/LICENSE +21 -0
- package/README.md +81 -0
- package/dist/index.cjs +109 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Theo Gravity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# VictoriaLogs Transport for LogLayer
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@loglayer/transport-victoria-logs)
|
|
4
|
+
[](https://www.npmjs.com/package/@loglayer/transport-victoria-logs)
|
|
5
|
+
[](http://www.typescriptlang.org/)
|
|
6
|
+
|
|
7
|
+
A [VictoriaLogs](https://victoriametrics.com/products/victorialogs/) transport for the [LogLayer](https://loglayer.dev) logging library.
|
|
8
|
+
|
|
9
|
+
This transport is a wrapper around the [HTTP transport](https://loglayer.dev/transports/http) using the [VictoriaLogs JSON stream API](https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install loglayer @loglayer/transport-victoria-logs serialize-error
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { LogLayer } from 'loglayer'
|
|
21
|
+
import { VictoriaLogsTransport } from "@loglayer/transport-victoria-logs"
|
|
22
|
+
import { serializeError } from "serialize-error";
|
|
23
|
+
|
|
24
|
+
const log = new LogLayer({
|
|
25
|
+
errorSerializer: serializeError,
|
|
26
|
+
transport: new VictoriaLogsTransport({
|
|
27
|
+
url: "http://localhost:9428", // optional, defaults to http://localhost:9428
|
|
28
|
+
// Configure stream-level fields for better performance
|
|
29
|
+
streamFields: () => ({
|
|
30
|
+
service: "my-app",
|
|
31
|
+
environment: process.env.NODE_ENV || "development",
|
|
32
|
+
instance: process.env.HOSTNAME || "unknown",
|
|
33
|
+
}),
|
|
34
|
+
// Custom timestamp function (optional)
|
|
35
|
+
timestamp: () => new Date().toISOString(),
|
|
36
|
+
// Custom HTTP parameters for VictoriaLogs ingestion
|
|
37
|
+
httpParameters: {
|
|
38
|
+
_time_field: "_time",
|
|
39
|
+
_msg_field: "_msg",
|
|
40
|
+
},
|
|
41
|
+
// All other HttpTransport options are available and optional
|
|
42
|
+
compression: false, // optional, defaults to false
|
|
43
|
+
maxRetries: 3, // optional, defaults to 3
|
|
44
|
+
retryDelay: 1000, // optional, defaults to 1000
|
|
45
|
+
respectRateLimit: true, // optional, defaults to true
|
|
46
|
+
enableBatchSend: true, // optional, defaults to true
|
|
47
|
+
batchSize: 100, // optional, defaults to 100
|
|
48
|
+
batchSendTimeout: 5000, // optional, defaults to 5000ms
|
|
49
|
+
onError: (err) => {
|
|
50
|
+
console.error('Failed to send logs to VictoriaLogs:', err);
|
|
51
|
+
},
|
|
52
|
+
onDebug: (entry) => {
|
|
53
|
+
console.log('Log entry being sent to VictoriaLogs:', entry);
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
// Use the logger
|
|
59
|
+
log.info("This is a test message");
|
|
60
|
+
log.withMetadata({ userId: "123" }).error("User not found");
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Vibe code notice
|
|
64
|
+
|
|
65
|
+
99% of this code was vibe-coded using Cursor and in the agent `auto` mode with some supervision.
|
|
66
|
+
|
|
67
|
+
See the [Prompts](PROMPTS.md) file for the prompts used.
|
|
68
|
+
|
|
69
|
+
## Documentation
|
|
70
|
+
|
|
71
|
+
For more details, visit [https://loglayer.dev/transports/victoria-logs](https://loglayer.dev/transports/victoria-logs)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## Related
|
|
75
|
+
|
|
76
|
+
- [LogLayer Documentation](https://loglayer.dev) - Main LogLayer documentation
|
|
77
|
+
- [HTTP Transport](/transports/http) - The underlying HTTP transport
|
|
78
|
+
- [VictoriaLogs Documentation](https://docs.victoriametrics.com/victorialogs/) - Official VictoriaLogs documentation
|
|
79
|
+
- [VictoriaLogs JSON Stream API](https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api) - API documentation for the JSON stream endpoint
|
|
80
|
+
- [VictoriaLogs HTTP Parameters](https://docs.victoriametrics.com/victorialogs/data-ingestion/#http-parameters) - HTTP query parameters documentation
|
|
81
|
+
- [VictoriaLogs Stream Fields](https://docs.victoriametrics.com/victorialogs/keyconcepts/#stream-fields) - Stream fields documentation
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
VictoriaLogsTransport: () => VictoriaLogsTransport
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/VictoriaLogsTransport.ts
|
|
28
|
+
var import_transport_http = require("@loglayer/transport-http");
|
|
29
|
+
var VictoriaLogsTransport = class extends import_transport_http.HttpTransport {
|
|
30
|
+
constructor(config = {}) {
|
|
31
|
+
const {
|
|
32
|
+
url = "http://localhost:9428",
|
|
33
|
+
method = "POST",
|
|
34
|
+
headers = {
|
|
35
|
+
"Content-Type": "application/stream+json"
|
|
36
|
+
},
|
|
37
|
+
contentType = "application/stream+json",
|
|
38
|
+
batchContentType = "application/stream+json",
|
|
39
|
+
streamFields = () => ({}),
|
|
40
|
+
timestamp = () => (/* @__PURE__ */ new Date()).toISOString(),
|
|
41
|
+
httpParameters = {},
|
|
42
|
+
payloadTemplate = ({ logLevel, message, data }) => {
|
|
43
|
+
const streamFieldsData2 = streamFields();
|
|
44
|
+
const timeValue = timestamp();
|
|
45
|
+
const msgField = httpParameters._msg_field || "_msg";
|
|
46
|
+
const timeField = httpParameters._time_field || "_time";
|
|
47
|
+
return JSON.stringify({
|
|
48
|
+
[msgField]: message || "(no message)",
|
|
49
|
+
[timeField]: timeValue,
|
|
50
|
+
level: logLevel,
|
|
51
|
+
...streamFieldsData2,
|
|
52
|
+
...data
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
compression = false,
|
|
56
|
+
maxRetries = 3,
|
|
57
|
+
retryDelay = 1e3,
|
|
58
|
+
respectRateLimit = true,
|
|
59
|
+
enableBatchSend = true,
|
|
60
|
+
batchSize = 100,
|
|
61
|
+
batchSendTimeout = 5e3,
|
|
62
|
+
batchSendDelimiter = "\n",
|
|
63
|
+
maxLogSize = 1048576,
|
|
64
|
+
// 1MB
|
|
65
|
+
maxPayloadSize = 5242880,
|
|
66
|
+
// 5MB
|
|
67
|
+
enableNextJsEdgeCompat = false,
|
|
68
|
+
onError,
|
|
69
|
+
onDebug,
|
|
70
|
+
...restConfig
|
|
71
|
+
} = config;
|
|
72
|
+
const streamFieldsData = streamFields();
|
|
73
|
+
const streamFieldsKeys = Object.keys(streamFieldsData);
|
|
74
|
+
const finalHttpParameters = {
|
|
75
|
+
...httpParameters,
|
|
76
|
+
...streamFieldsKeys.length > 0 && { _stream_fields: streamFieldsKeys.join(",") }
|
|
77
|
+
};
|
|
78
|
+
const baseUrl = `${url.replace(/\/$/, "")}/insert/jsonline`;
|
|
79
|
+
const queryParams = new URLSearchParams(finalHttpParameters);
|
|
80
|
+
const fullUrl = queryParams.toString() ? `${baseUrl}?${queryParams.toString()}` : baseUrl;
|
|
81
|
+
super({
|
|
82
|
+
url: fullUrl,
|
|
83
|
+
method,
|
|
84
|
+
headers,
|
|
85
|
+
contentType,
|
|
86
|
+
batchContentType,
|
|
87
|
+
payloadTemplate,
|
|
88
|
+
compression,
|
|
89
|
+
maxRetries,
|
|
90
|
+
retryDelay,
|
|
91
|
+
respectRateLimit,
|
|
92
|
+
enableBatchSend,
|
|
93
|
+
batchSize,
|
|
94
|
+
batchSendTimeout,
|
|
95
|
+
batchSendDelimiter,
|
|
96
|
+
maxLogSize,
|
|
97
|
+
maxPayloadSize,
|
|
98
|
+
enableNextJsEdgeCompat,
|
|
99
|
+
onError,
|
|
100
|
+
onDebug,
|
|
101
|
+
...restConfig
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
106
|
+
0 && (module.exports = {
|
|
107
|
+
VictoriaLogsTransport
|
|
108
|
+
});
|
|
109
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/VictoriaLogsTransport.ts"],"sourcesContent":["export type { VictoriaLogsTransportConfig } from \"./VictoriaLogsTransport.js\";\nexport { VictoriaLogsTransport } from \"./VictoriaLogsTransport.js\";\n","import type { HttpTransportConfig } from \"@loglayer/transport-http\";\nimport { HttpTransport } from \"@loglayer/transport-http\";\n\n/**\n * Configuration options for the VictoriaLogs transport.\n * This is essentially a wrapper around HttpTransport with VictoriaLogs specific defaults.\n */\nexport interface VictoriaLogsTransportConfig extends Omit<HttpTransportConfig, \"url\" | \"payloadTemplate\"> {\n /**\n * The VictoriaLogs host URL (e.g., http://localhost:9428)\n * The /insert/jsonline path will be automatically appended\n * @default \"http://localhost:9428\"\n */\n url?: string;\n /**\n * Function to transform log data into the payload format (optional, defaults to VictoriaLogs format)\n */\n payloadTemplate?: (data: { logLevel: string; message: string; data?: Record<string, any> }) => string;\n /**\n * Function to generate stream-level fields for VictoriaLogs\n * The keys of the returned object will be used as the values for the _stream_fields parameter\n * @default () => ({})\n */\n streamFields?: () => Record<string, string>;\n /**\n * Function to generate the timestamp for the _time field\n * @default () => new Date().toISOString()\n */\n timestamp?: () => string;\n /**\n * Custom HTTP query parameters for VictoriaLogs ingestion\n * @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#http-parameters\n */\n httpParameters?: Record<string, string>;\n}\n\n/**\n * VictoriaLogs transport for LogLayer.\n *\n * This transport is a thin wrapper around the HttpTransport that provides\n * VictoriaLogs specific defaults and configuration. It uses the VictoriaLogs\n * JSON stream API to send logs.\n *\n * @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api\n */\nexport class VictoriaLogsTransport extends HttpTransport {\n constructor(config: VictoriaLogsTransportConfig = {}) {\n const {\n url = \"http://localhost:9428\",\n method = \"POST\",\n headers = {\n \"Content-Type\": \"application/stream+json\",\n },\n contentType = \"application/stream+json\",\n batchContentType = \"application/stream+json\",\n streamFields = () => ({}),\n timestamp = () => new Date().toISOString(),\n httpParameters = {},\n payloadTemplate = ({ logLevel, message, data }) => {\n const streamFieldsData = streamFields();\n const timeValue = timestamp();\n\n // Determine field names based on HTTP parameters\n const msgField = httpParameters._msg_field || \"_msg\";\n const timeField = httpParameters._time_field || \"_time\";\n\n return JSON.stringify({\n [msgField]: message || \"(no message)\",\n [timeField]: timeValue,\n level: logLevel,\n ...streamFieldsData,\n ...data,\n });\n },\n compression = false,\n maxRetries = 3,\n retryDelay = 1000,\n respectRateLimit = true,\n enableBatchSend = true,\n batchSize = 100,\n batchSendTimeout = 5000,\n batchSendDelimiter = \"\\n\",\n maxLogSize = 1048576, // 1MB\n maxPayloadSize = 5242880, // 5MB\n enableNextJsEdgeCompat = false,\n onError,\n onDebug,\n ...restConfig\n } = config;\n\n // Get stream fields and use their keys as _stream_fields parameter\n const streamFieldsData = streamFields();\n const streamFieldsKeys = Object.keys(streamFieldsData);\n\n // Merge HTTP parameters, adding _stream_fields if streamFields has keys\n const finalHttpParameters = {\n ...httpParameters,\n ...(streamFieldsKeys.length > 0 && { _stream_fields: streamFieldsKeys.join(\",\") }),\n };\n\n // Construct the full URL with the VictoriaLogs JSON stream API endpoint and query parameters\n const baseUrl = `${url.replace(/\\/$/, \"\")}/insert/jsonline`;\n const queryParams = new URLSearchParams(finalHttpParameters);\n const fullUrl = queryParams.toString() ? `${baseUrl}?${queryParams.toString()}` : baseUrl;\n\n super({\n url: fullUrl,\n method,\n headers,\n contentType,\n batchContentType,\n payloadTemplate,\n compression,\n maxRetries,\n retryDelay,\n respectRateLimit,\n enableBatchSend,\n batchSize,\n batchSendTimeout,\n batchSendDelimiter,\n maxLogSize,\n maxPayloadSize,\n enableNextJsEdgeCompat,\n onError,\n onDebug,\n ...restConfig,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;AA4CvB,IAAM,wBAAN,cAAoC,oCAAc;AAAA,EACvD,YAAY,SAAsC,CAAC,GAAG;AACpD,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,SAAS;AAAA,MACT,UAAU;AAAA,QACR,gBAAgB;AAAA,MAClB;AAAA,MACA,cAAc;AAAA,MACd,mBAAmB;AAAA,MACnB,eAAe,OAAO,CAAC;AAAA,MACvB,YAAY,OAAM,oBAAI,KAAK,GAAE,YAAY;AAAA,MACzC,iBAAiB,CAAC;AAAA,MAClB,kBAAkB,CAAC,EAAE,UAAU,SAAS,KAAK,MAAM;AACjD,cAAMA,oBAAmB,aAAa;AACtC,cAAM,YAAY,UAAU;AAG5B,cAAM,WAAW,eAAe,cAAc;AAC9C,cAAM,YAAY,eAAe,eAAe;AAEhD,eAAO,KAAK,UAAU;AAAA,UACpB,CAAC,QAAQ,GAAG,WAAW;AAAA,UACvB,CAAC,SAAS,GAAG;AAAA,UACb,OAAO;AAAA,UACP,GAAGA;AAAA,UACH,GAAG;AAAA,QACL,CAAC;AAAA,MACH;AAAA,MACA,cAAc;AAAA,MACd,aAAa;AAAA,MACb,aAAa;AAAA,MACb,mBAAmB;AAAA,MACnB,kBAAkB;AAAA,MAClB,YAAY;AAAA,MACZ,mBAAmB;AAAA,MACnB,qBAAqB;AAAA,MACrB,aAAa;AAAA;AAAA,MACb,iBAAiB;AAAA;AAAA,MACjB,yBAAyB;AAAA,MACzB;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,IAAI;AAGJ,UAAM,mBAAmB,aAAa;AACtC,UAAM,mBAAmB,OAAO,KAAK,gBAAgB;AAGrD,UAAM,sBAAsB;AAAA,MAC1B,GAAG;AAAA,MACH,GAAI,iBAAiB,SAAS,KAAK,EAAE,gBAAgB,iBAAiB,KAAK,GAAG,EAAE;AAAA,IAClF;AAGA,UAAM,UAAU,GAAG,IAAI,QAAQ,OAAO,EAAE,CAAC;AACzC,UAAM,cAAc,IAAI,gBAAgB,mBAAmB;AAC3D,UAAM,UAAU,YAAY,SAAS,IAAI,GAAG,OAAO,IAAI,YAAY,SAAS,CAAC,KAAK;AAElF,UAAM;AAAA,MACJ,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;","names":["streamFieldsData"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { HttpTransportConfig, HttpTransport } from '@loglayer/transport-http';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the VictoriaLogs transport.
|
|
5
|
+
* This is essentially a wrapper around HttpTransport with VictoriaLogs specific defaults.
|
|
6
|
+
*/
|
|
7
|
+
interface VictoriaLogsTransportConfig extends Omit<HttpTransportConfig, "url" | "payloadTemplate"> {
|
|
8
|
+
/**
|
|
9
|
+
* The VictoriaLogs host URL (e.g., http://localhost:9428)
|
|
10
|
+
* The /insert/jsonline path will be automatically appended
|
|
11
|
+
* @default "http://localhost:9428"
|
|
12
|
+
*/
|
|
13
|
+
url?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Function to transform log data into the payload format (optional, defaults to VictoriaLogs format)
|
|
16
|
+
*/
|
|
17
|
+
payloadTemplate?: (data: {
|
|
18
|
+
logLevel: string;
|
|
19
|
+
message: string;
|
|
20
|
+
data?: Record<string, any>;
|
|
21
|
+
}) => string;
|
|
22
|
+
/**
|
|
23
|
+
* Function to generate stream-level fields for VictoriaLogs
|
|
24
|
+
* The keys of the returned object will be used as the values for the _stream_fields parameter
|
|
25
|
+
* @default () => ({})
|
|
26
|
+
*/
|
|
27
|
+
streamFields?: () => Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Function to generate the timestamp for the _time field
|
|
30
|
+
* @default () => new Date().toISOString()
|
|
31
|
+
*/
|
|
32
|
+
timestamp?: () => string;
|
|
33
|
+
/**
|
|
34
|
+
* Custom HTTP query parameters for VictoriaLogs ingestion
|
|
35
|
+
* @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#http-parameters
|
|
36
|
+
*/
|
|
37
|
+
httpParameters?: Record<string, string>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* VictoriaLogs transport for LogLayer.
|
|
41
|
+
*
|
|
42
|
+
* This transport is a thin wrapper around the HttpTransport that provides
|
|
43
|
+
* VictoriaLogs specific defaults and configuration. It uses the VictoriaLogs
|
|
44
|
+
* JSON stream API to send logs.
|
|
45
|
+
*
|
|
46
|
+
* @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api
|
|
47
|
+
*/
|
|
48
|
+
declare class VictoriaLogsTransport extends HttpTransport {
|
|
49
|
+
constructor(config?: VictoriaLogsTransportConfig);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { VictoriaLogsTransport, type VictoriaLogsTransportConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { HttpTransportConfig, HttpTransport } from '@loglayer/transport-http';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the VictoriaLogs transport.
|
|
5
|
+
* This is essentially a wrapper around HttpTransport with VictoriaLogs specific defaults.
|
|
6
|
+
*/
|
|
7
|
+
interface VictoriaLogsTransportConfig extends Omit<HttpTransportConfig, "url" | "payloadTemplate"> {
|
|
8
|
+
/**
|
|
9
|
+
* The VictoriaLogs host URL (e.g., http://localhost:9428)
|
|
10
|
+
* The /insert/jsonline path will be automatically appended
|
|
11
|
+
* @default "http://localhost:9428"
|
|
12
|
+
*/
|
|
13
|
+
url?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Function to transform log data into the payload format (optional, defaults to VictoriaLogs format)
|
|
16
|
+
*/
|
|
17
|
+
payloadTemplate?: (data: {
|
|
18
|
+
logLevel: string;
|
|
19
|
+
message: string;
|
|
20
|
+
data?: Record<string, any>;
|
|
21
|
+
}) => string;
|
|
22
|
+
/**
|
|
23
|
+
* Function to generate stream-level fields for VictoriaLogs
|
|
24
|
+
* The keys of the returned object will be used as the values for the _stream_fields parameter
|
|
25
|
+
* @default () => ({})
|
|
26
|
+
*/
|
|
27
|
+
streamFields?: () => Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Function to generate the timestamp for the _time field
|
|
30
|
+
* @default () => new Date().toISOString()
|
|
31
|
+
*/
|
|
32
|
+
timestamp?: () => string;
|
|
33
|
+
/**
|
|
34
|
+
* Custom HTTP query parameters for VictoriaLogs ingestion
|
|
35
|
+
* @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#http-parameters
|
|
36
|
+
*/
|
|
37
|
+
httpParameters?: Record<string, string>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* VictoriaLogs transport for LogLayer.
|
|
41
|
+
*
|
|
42
|
+
* This transport is a thin wrapper around the HttpTransport that provides
|
|
43
|
+
* VictoriaLogs specific defaults and configuration. It uses the VictoriaLogs
|
|
44
|
+
* JSON stream API to send logs.
|
|
45
|
+
*
|
|
46
|
+
* @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api
|
|
47
|
+
*/
|
|
48
|
+
declare class VictoriaLogsTransport extends HttpTransport {
|
|
49
|
+
constructor(config?: VictoriaLogsTransportConfig);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { VictoriaLogsTransport, type VictoriaLogsTransportConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// src/VictoriaLogsTransport.ts
|
|
2
|
+
import { HttpTransport } from "@loglayer/transport-http";
|
|
3
|
+
var VictoriaLogsTransport = class extends HttpTransport {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
const {
|
|
6
|
+
url = "http://localhost:9428",
|
|
7
|
+
method = "POST",
|
|
8
|
+
headers = {
|
|
9
|
+
"Content-Type": "application/stream+json"
|
|
10
|
+
},
|
|
11
|
+
contentType = "application/stream+json",
|
|
12
|
+
batchContentType = "application/stream+json",
|
|
13
|
+
streamFields = () => ({}),
|
|
14
|
+
timestamp = () => (/* @__PURE__ */ new Date()).toISOString(),
|
|
15
|
+
httpParameters = {},
|
|
16
|
+
payloadTemplate = ({ logLevel, message, data }) => {
|
|
17
|
+
const streamFieldsData2 = streamFields();
|
|
18
|
+
const timeValue = timestamp();
|
|
19
|
+
const msgField = httpParameters._msg_field || "_msg";
|
|
20
|
+
const timeField = httpParameters._time_field || "_time";
|
|
21
|
+
return JSON.stringify({
|
|
22
|
+
[msgField]: message || "(no message)",
|
|
23
|
+
[timeField]: timeValue,
|
|
24
|
+
level: logLevel,
|
|
25
|
+
...streamFieldsData2,
|
|
26
|
+
...data
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
compression = false,
|
|
30
|
+
maxRetries = 3,
|
|
31
|
+
retryDelay = 1e3,
|
|
32
|
+
respectRateLimit = true,
|
|
33
|
+
enableBatchSend = true,
|
|
34
|
+
batchSize = 100,
|
|
35
|
+
batchSendTimeout = 5e3,
|
|
36
|
+
batchSendDelimiter = "\n",
|
|
37
|
+
maxLogSize = 1048576,
|
|
38
|
+
// 1MB
|
|
39
|
+
maxPayloadSize = 5242880,
|
|
40
|
+
// 5MB
|
|
41
|
+
enableNextJsEdgeCompat = false,
|
|
42
|
+
onError,
|
|
43
|
+
onDebug,
|
|
44
|
+
...restConfig
|
|
45
|
+
} = config;
|
|
46
|
+
const streamFieldsData = streamFields();
|
|
47
|
+
const streamFieldsKeys = Object.keys(streamFieldsData);
|
|
48
|
+
const finalHttpParameters = {
|
|
49
|
+
...httpParameters,
|
|
50
|
+
...streamFieldsKeys.length > 0 && { _stream_fields: streamFieldsKeys.join(",") }
|
|
51
|
+
};
|
|
52
|
+
const baseUrl = `${url.replace(/\/$/, "")}/insert/jsonline`;
|
|
53
|
+
const queryParams = new URLSearchParams(finalHttpParameters);
|
|
54
|
+
const fullUrl = queryParams.toString() ? `${baseUrl}?${queryParams.toString()}` : baseUrl;
|
|
55
|
+
super({
|
|
56
|
+
url: fullUrl,
|
|
57
|
+
method,
|
|
58
|
+
headers,
|
|
59
|
+
contentType,
|
|
60
|
+
batchContentType,
|
|
61
|
+
payloadTemplate,
|
|
62
|
+
compression,
|
|
63
|
+
maxRetries,
|
|
64
|
+
retryDelay,
|
|
65
|
+
respectRateLimit,
|
|
66
|
+
enableBatchSend,
|
|
67
|
+
batchSize,
|
|
68
|
+
batchSendTimeout,
|
|
69
|
+
batchSendDelimiter,
|
|
70
|
+
maxLogSize,
|
|
71
|
+
maxPayloadSize,
|
|
72
|
+
enableNextJsEdgeCompat,
|
|
73
|
+
onError,
|
|
74
|
+
onDebug,
|
|
75
|
+
...restConfig
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
export {
|
|
80
|
+
VictoriaLogsTransport
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/VictoriaLogsTransport.ts"],"sourcesContent":["import type { HttpTransportConfig } from \"@loglayer/transport-http\";\nimport { HttpTransport } from \"@loglayer/transport-http\";\n\n/**\n * Configuration options for the VictoriaLogs transport.\n * This is essentially a wrapper around HttpTransport with VictoriaLogs specific defaults.\n */\nexport interface VictoriaLogsTransportConfig extends Omit<HttpTransportConfig, \"url\" | \"payloadTemplate\"> {\n /**\n * The VictoriaLogs host URL (e.g., http://localhost:9428)\n * The /insert/jsonline path will be automatically appended\n * @default \"http://localhost:9428\"\n */\n url?: string;\n /**\n * Function to transform log data into the payload format (optional, defaults to VictoriaLogs format)\n */\n payloadTemplate?: (data: { logLevel: string; message: string; data?: Record<string, any> }) => string;\n /**\n * Function to generate stream-level fields for VictoriaLogs\n * The keys of the returned object will be used as the values for the _stream_fields parameter\n * @default () => ({})\n */\n streamFields?: () => Record<string, string>;\n /**\n * Function to generate the timestamp for the _time field\n * @default () => new Date().toISOString()\n */\n timestamp?: () => string;\n /**\n * Custom HTTP query parameters for VictoriaLogs ingestion\n * @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#http-parameters\n */\n httpParameters?: Record<string, string>;\n}\n\n/**\n * VictoriaLogs transport for LogLayer.\n *\n * This transport is a thin wrapper around the HttpTransport that provides\n * VictoriaLogs specific defaults and configuration. It uses the VictoriaLogs\n * JSON stream API to send logs.\n *\n * @see https://docs.victoriametrics.com/victorialogs/data-ingestion/#json-stream-api\n */\nexport class VictoriaLogsTransport extends HttpTransport {\n constructor(config: VictoriaLogsTransportConfig = {}) {\n const {\n url = \"http://localhost:9428\",\n method = \"POST\",\n headers = {\n \"Content-Type\": \"application/stream+json\",\n },\n contentType = \"application/stream+json\",\n batchContentType = \"application/stream+json\",\n streamFields = () => ({}),\n timestamp = () => new Date().toISOString(),\n httpParameters = {},\n payloadTemplate = ({ logLevel, message, data }) => {\n const streamFieldsData = streamFields();\n const timeValue = timestamp();\n\n // Determine field names based on HTTP parameters\n const msgField = httpParameters._msg_field || \"_msg\";\n const timeField = httpParameters._time_field || \"_time\";\n\n return JSON.stringify({\n [msgField]: message || \"(no message)\",\n [timeField]: timeValue,\n level: logLevel,\n ...streamFieldsData,\n ...data,\n });\n },\n compression = false,\n maxRetries = 3,\n retryDelay = 1000,\n respectRateLimit = true,\n enableBatchSend = true,\n batchSize = 100,\n batchSendTimeout = 5000,\n batchSendDelimiter = \"\\n\",\n maxLogSize = 1048576, // 1MB\n maxPayloadSize = 5242880, // 5MB\n enableNextJsEdgeCompat = false,\n onError,\n onDebug,\n ...restConfig\n } = config;\n\n // Get stream fields and use their keys as _stream_fields parameter\n const streamFieldsData = streamFields();\n const streamFieldsKeys = Object.keys(streamFieldsData);\n\n // Merge HTTP parameters, adding _stream_fields if streamFields has keys\n const finalHttpParameters = {\n ...httpParameters,\n ...(streamFieldsKeys.length > 0 && { _stream_fields: streamFieldsKeys.join(\",\") }),\n };\n\n // Construct the full URL with the VictoriaLogs JSON stream API endpoint and query parameters\n const baseUrl = `${url.replace(/\\/$/, \"\")}/insert/jsonline`;\n const queryParams = new URLSearchParams(finalHttpParameters);\n const fullUrl = queryParams.toString() ? `${baseUrl}?${queryParams.toString()}` : baseUrl;\n\n super({\n url: fullUrl,\n method,\n headers,\n contentType,\n batchContentType,\n payloadTemplate,\n compression,\n maxRetries,\n retryDelay,\n respectRateLimit,\n enableBatchSend,\n batchSize,\n batchSendTimeout,\n batchSendDelimiter,\n maxLogSize,\n maxPayloadSize,\n enableNextJsEdgeCompat,\n onError,\n onDebug,\n ...restConfig,\n });\n }\n}\n"],"mappings":";AACA,SAAS,qBAAqB;AA4CvB,IAAM,wBAAN,cAAoC,cAAc;AAAA,EACvD,YAAY,SAAsC,CAAC,GAAG;AACpD,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,SAAS;AAAA,MACT,UAAU;AAAA,QACR,gBAAgB;AAAA,MAClB;AAAA,MACA,cAAc;AAAA,MACd,mBAAmB;AAAA,MACnB,eAAe,OAAO,CAAC;AAAA,MACvB,YAAY,OAAM,oBAAI,KAAK,GAAE,YAAY;AAAA,MACzC,iBAAiB,CAAC;AAAA,MAClB,kBAAkB,CAAC,EAAE,UAAU,SAAS,KAAK,MAAM;AACjD,cAAMA,oBAAmB,aAAa;AACtC,cAAM,YAAY,UAAU;AAG5B,cAAM,WAAW,eAAe,cAAc;AAC9C,cAAM,YAAY,eAAe,eAAe;AAEhD,eAAO,KAAK,UAAU;AAAA,UACpB,CAAC,QAAQ,GAAG,WAAW;AAAA,UACvB,CAAC,SAAS,GAAG;AAAA,UACb,OAAO;AAAA,UACP,GAAGA;AAAA,UACH,GAAG;AAAA,QACL,CAAC;AAAA,MACH;AAAA,MACA,cAAc;AAAA,MACd,aAAa;AAAA,MACb,aAAa;AAAA,MACb,mBAAmB;AAAA,MACnB,kBAAkB;AAAA,MAClB,YAAY;AAAA,MACZ,mBAAmB;AAAA,MACnB,qBAAqB;AAAA,MACrB,aAAa;AAAA;AAAA,MACb,iBAAiB;AAAA;AAAA,MACjB,yBAAyB;AAAA,MACzB;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,IAAI;AAGJ,UAAM,mBAAmB,aAAa;AACtC,UAAM,mBAAmB,OAAO,KAAK,gBAAgB;AAGrD,UAAM,sBAAsB;AAAA,MAC1B,GAAG;AAAA,MACH,GAAI,iBAAiB,SAAS,KAAK,EAAE,gBAAgB,iBAAiB,KAAK,GAAG,EAAE;AAAA,IAClF;AAGA,UAAM,UAAU,GAAG,IAAI,QAAQ,OAAO,EAAE,CAAC;AACzC,UAAM,cAAc,IAAI,gBAAgB,mBAAmB;AAC3D,UAAM,UAAU,YAAY,SAAS,IAAI,GAAG,OAAO,IAAI,YAAY,SAAS,CAAC,KAAK;AAElF,UAAM;AAAA,MACJ,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;","names":["streamFieldsData"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loglayer/transport-victoria-logs",
|
|
3
|
+
"description": "VictoriaLogs transport for the LogLayer logging library.",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@loglayer/transport-http": "1.0.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"dotenv": "16.5.0",
|
|
22
|
+
"hash-runner": "2.0.1",
|
|
23
|
+
"@types/node": "22.15.17",
|
|
24
|
+
"serialize-error": "12.0.0",
|
|
25
|
+
"tsx": "4.20.3",
|
|
26
|
+
"tsup": "8.5.0",
|
|
27
|
+
"typescript": "5.8.3",
|
|
28
|
+
"vitest": "3.2.4",
|
|
29
|
+
"loglayer": "6.4.3",
|
|
30
|
+
"@internal/tsconfig": "2.1.0"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"loglayer",
|
|
34
|
+
"transport",
|
|
35
|
+
"victoria",
|
|
36
|
+
"victorialogs",
|
|
37
|
+
"logging",
|
|
38
|
+
"logs"
|
|
39
|
+
],
|
|
40
|
+
"author": "Theo Gravity <theo@suteki.nu>",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/loglayer/loglayer.git",
|
|
45
|
+
"directory": "packages/transports/victoria-logs"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/loglayer/loglayer/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://loglayer.dev/transports/victoria-logs",
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"dev": "tsup --watch",
|
|
54
|
+
"test": "vitest --run",
|
|
55
|
+
"test:watch": "vitest",
|
|
56
|
+
"lint": "biome check --write --unsafe src && biome format src --write && biome lint src --fix"
|
|
57
|
+
}
|
|
58
|
+
}
|