Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/myunisoft/loki

Node.js Loki SDK
https://github.com/myunisoft/loki

api grafana loki nodejs sdk

Last synced: about 3 hours ago
JSON representation

Node.js Loki SDK

Awesome Lists containing this project

README

        


Loki


Node.js Grafana API SDK (Loki, Datasources ..)



npm version


license


ossf scorecard


github ci workflow


size

## 🚧 Requirements

- [Node.js](https://nodejs.org/en/) version 18 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({
// Note: if not provided, it will load process.env.GRAFANA_API_TOKEN
apiToken: "...",
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
export interface GrafanaApiOptions {
/**
* Grafana API Token
*/
apiToken?: string;
/**
* 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;
}
```

### Sub-class

- [Loki](./docs/Loki.md)
- [Datasources](./docs/Datasources.md)

## Contributors ✨

[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Thomas.G
Thomas.G

💻 🛡️ 📖
PierreDemailly
PierreDemailly

💻 ⚠️ 📖

## License
MIT