@paimaexample/grafana-loki 0.3.105

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.
Files changed (3) hide show
  1. package/index.js +119 -0
  2. package/loki-config.yaml +32 -0
  3. package/package.json +15 -0
package/index.js ADDED
@@ -0,0 +1,119 @@
1
+ import path from 'node:path';
2
+ import { platform, arch } from 'node:os';
3
+ import BinWrapper from '@xhmikosr/bin-wrapper';
4
+ import { spawn } from 'node:child_process';
5
+ import process from 'node:process';
6
+ import fs from 'node:fs';
7
+
8
+ console.log(`Starting Grafana Loki on ${platform()} ${arch()}`);
9
+
10
+ // Get binary name.
11
+ // This is the default name after the binary is downloaded and extracted.
12
+ function getBinaryName() {
13
+ const osArch = arch();
14
+ const osPlatform = platform();
15
+ switch (osPlatform) {
16
+ case 'win32':
17
+ return (osArch === 'x64') ? 'loki-windows-amd64.exe' :
18
+ undefined;
19
+
20
+ case 'darwin':
21
+ return (osArch === 'arm64') ? 'loki-darwin-arm64' :
22
+ (osArch === 'x64') ? 'loki-darwin-amd64' :
23
+ undefined;
24
+ case 'linux':
25
+ return (osArch === 'arm64') ? 'loki-linux-arm64' :
26
+ (osArch === 'x64') ? 'loki-linux-amd64' :
27
+ undefined;
28
+ }
29
+ // Throw error if binary name is not found, we don't have the binary for this platform.
30
+ throw new Error(`Unsupported platform: ${osPlatform} - ${osArch}`);
31
+ }
32
+
33
+ function cleanBinary() {
34
+ console.log(`Cleaning binary folder: ${path.join(import.meta.dirname, 'vendor')}`);
35
+ fs.rmSync(path.join(import.meta.dirname, 'vendor'), { recursive: true, force: true });
36
+ }
37
+
38
+ function cleanData() {
39
+ console.log(`Cleaning data folder: ${path.join(import.meta.dirname, 'data')}`);
40
+ fs.rmSync(path.join(import.meta.dirname, 'data'), { recursive: true, force: true });
41
+ }
42
+
43
+ const binaryName = getBinaryName();
44
+
45
+ // Download the binary from the GitHub releases page.
46
+ const version = '3.5.8';
47
+ const base = `https://github.com/grafana/loki/releases/download/v${version}/`;
48
+
49
+ const bin = new BinWrapper()
50
+ .src(`${base}/loki-darwin-arm64.zip`, 'darwin', 'arm64')
51
+ .src(`${base}/loki-darwin-amd64.zip`, 'darwin', 'x64')
52
+ .src(`${base}/loki-linux-arm64.zip`, 'linux', 'arm64')
53
+ .src(`${base}/loki-linux-amd64.zip`, 'linux', 'x64')
54
+ .src(`${base}/loki-windows-amd64.exe.zip`, 'win32', 'x64')
55
+ .dest(path.join(import.meta.dirname, 'vendor'))
56
+ .use(binaryName)
57
+ .version(`>=${version}`);
58
+
59
+
60
+ async function runBinary(args = []) {
61
+ // Check if the binary is installed working.
62
+ await bin.run(['--version']);
63
+
64
+ const message = `Loki URL: http://localhost:3100`
65
+ // Binary command to launch.
66
+ const command = {
67
+ bin: bin.path(),
68
+ args: [
69
+ `-config.file=${path.join(import.meta.dirname, 'loki-config.yaml')}`,
70
+ `-common.path-prefix=${path.join(import.meta.dirname, 'data', 'loki')}`,
71
+ `-common.storage.filesystem.chunk-directory=${path.join(import.meta.dirname, 'data', 'loki', 'chunks')}`,
72
+ `-common.storage.filesystem.rules-directory=${path.join(import.meta.dirname, 'data', 'loki', 'rules')}`,
73
+ ...args
74
+ ]
75
+ };
76
+ const line = Array(80).fill('=').join('');
77
+ console.log(`${line}\nLaunching command:\n${command.bin} ${command.args.join(' ')}\n\n${message}\n${line}`);
78
+ const child = spawn(command.bin, command.args);
79
+
80
+ child.stdout.setEncoding('utf8');
81
+ child.stdout.on('data', (chunk) => {
82
+ console.log(chunk.toString());
83
+ });
84
+
85
+ child.stderr.setEncoding('utf8');
86
+ child.stderr.on('data', (chunk) => {
87
+ console.error(chunk.toString());
88
+ });
89
+
90
+ child.on('close', (code) => {
91
+ console.log(`child process exited with code ${code}`);
92
+ });
93
+
94
+ return child;
95
+ }
96
+
97
+ // Launch the binary.
98
+ // Custom options:
99
+ // --no-clean-data to not remove the data storage folder.
100
+ // --clean-binary to remove the binary folder.
101
+ (async () => {
102
+ // This script is launched with:
103
+ // deno -A @paimaexample/grafana-loki grafana-loki <optional-args>
104
+ const optionalArgs = process.argv.slice(3);
105
+
106
+ // Clean the data if the flag is not present.
107
+ if (!optionalArgs.includes('--no-clean-data')) cleanData();
108
+
109
+ // Clean the binary folder if the flag is present.
110
+ if (optionalArgs.includes('--clean-binary')) cleanBinary();
111
+
112
+ optionalArgs.indexOf('--no-clean-data') !== -1 && optionalArgs.splice(optionalArgs.indexOf('--no-clean-data'), 1);
113
+ optionalArgs.indexOf('--clean-binary') !== -1 && optionalArgs.splice(optionalArgs.indexOf('--clean-binary'), 1);
114
+
115
+ // Run the binary with the remaining optional arguments.
116
+ await runBinary(optionalArgs);
117
+ })();
118
+
119
+ export default runBinary;
@@ -0,0 +1,32 @@
1
+ # Example config file for Loki.
2
+ auth_enabled: false
3
+
4
+ server:
5
+ http_listen_port: 3100
6
+ grpc_listen_port: 9096
7
+
8
+ common:
9
+ instance_addr: 127.0.0.1
10
+ # Setup dynamically for relative paths to work
11
+ # path_prefix: ./data/loki
12
+ # storage:
13
+ # filesystem:
14
+ # chunks_directory: ./data/loki/chunks
15
+ # rules_directory: ./data/loki/rules
16
+ replication_factor: 1
17
+ ring:
18
+ kvstore:
19
+ store: inmemory
20
+
21
+ schema_config:
22
+ configs:
23
+ - from: 2025-01-01
24
+ store: tsdb
25
+ object_store: filesystem
26
+ schema: v13
27
+ index:
28
+ prefix: index_
29
+ period: 24h
30
+
31
+ limits_config:
32
+ allow_structured_metadata: true
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@paimaexample/grafana-loki",
3
+ "version": "0.3.105",
4
+ "description": "A wrapper for the Grafana Loki binary",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "grafana-loki": "index.js"
8
+ },
9
+ "scripts": {
10
+ "start": "node index.js"
11
+ },
12
+ "dependencies": {
13
+ "@xhmikosr/bin-wrapper": "^13.2.0"
14
+ }
15
+ }