https://github.com/myunisoft/loki
Node.js Loki SDK
https://github.com/myunisoft/loki
api grafana loki nodejs sdk
Last synced: 2 months ago
JSON representation
Node.js Loki SDK
- Host: GitHub
- URL: https://github.com/myunisoft/loki
- Owner: MyUnisoft
- License: mit
- Created: 2023-06-30T21:45:20.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-01T07:50:30.000Z (3 months ago)
- Last Synced: 2025-05-01T08:39:18.661Z (3 months ago)
- Topics: api, grafana, loki, nodejs, sdk
- Language: TypeScript
- Homepage:
- Size: 431 KB
- Stars: 19
- Watchers: 0
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
Loki
Node.js Grafana API SDK (Loki, Datasources ..)## 🚧 Requirements
- [Node.js](https://nodejs.org/en/) version 20 or higher
- A [reverse-proxy](https://github.com/MyUnisoft/loki-reverse-proxy) to safely expose Loki to internet (if required).## 🚀 Getting Started
This package is available in the Node Package Repository and can be easily installed with [npm](https://doc.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com)
```bash
$ npm i @myunisoft/loki
# or
$ yarn add @myunisoft/loki
```## 📚 Usage
```ts
import { GrafanaApi } from "@myunisoft/loki";
import { LogQL, StreamSelector } from "@sigyn/logql";const api = new GrafanaApi({
authentication: {
type: "bearer",
token: process.env.GRAFANA_API_TOKEN!
},
remoteApiURL: "https://name.loki.com"
});const ql = new LogQL(
new StreamSelector({ app: "serviceName", env: "production" })
);
const logs = await api.Loki.queryRange(
ql, // or string `{app="serviceName", env="production"}`
{
start: "1d",
limit: 200
}
);
console.log(logs);
```You can also provide a Loki pattern to automatically parse logs (and infer the right type with TypeScript)
```ts
const logs = await api.Loki.queryRange(
`{app="serviceName", env="production"}`
{
pattern: " <_> "
}
);
for (const { verb, endpoint } of logs) {
console.log({verb, endpoint });
}
```## API
### GrafanaAPI
```ts
interface GrafanaApiOptions {
/**
* If omitted then no authorization is dispatched to requests
*/
authentication?: ApiCredentialAuthorizationOptions;
/**
* User-agent HTTP header to forward to Grafana/Loki API
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agents
*/
userAgent?: string;
/**
* Remote Grafana root API URL
*/
remoteApiURL: string | URL;
}type ApiCredentialAuthorizationOptions = {
type: "bearer";
token: string;
} | {
type: "classic";
username: string;
password: string;
} | {
type: "custom";
authorization: string;
};
```### Sub-class
- [Loki](./docs/Loki.md)
- [Datasources](./docs/Datasources.md)## Contributors ✨
[](#contributors-)
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Thomas.G
💻 🛡️ 📖
PierreDemailly
💻 ⚠️ 📖
## License
MIT