/prom-graphql

A GraphQL JS wrapper for the Prometheus REST API

Primary LanguageJavaScriptMIT LicenseMIT

Prom-GraphQL

Language: JavaScript License: MIT

This is an open source GraphQL wrapper for the Prometheus REST API.

Installation (soon)

npm install prom-graphql

Usage

This module exports an executable schema (built with graphql-tools) that can be used either with the graphql or express-graphql to contruct a graphql server. The module, when required, brings a factory function that accepts an optional base URL (the URL of the Prometheus service). If not supplied, the prometheus base query will require an argument to be passed informing the URL of the service. The following snippet demostrates a simple use of the module (passing a pseudo URL) with the express-graphql.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { schema } = require('prom-graphql')('http://prometheus:9090'); //no need to inform the /api/v1 endpoint's resource

const app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');

Additionally, the type definition and the resolvers are also included in the exported object so prom-graphql can easily be extended or integrated with other schemas or tools (like graphql-compose - see Migration from graphql-tools section).

const { schema, typeDefs, resolvers } = require('prom-graphql')('http://prometheus:9090');

Types Definition

All the available operations are wrapped in a top level type called prometheus. Every operation in the prometheus type has been modelled as close as possible to the descriptions available in the Prometheus REST docs and in the source code as well. For example, the prometheus type has a field called query (just like the REST endpoint) that, when issued in a query(pun intended), executes an instant query; this operation receives the same set of arguments (with the same nomenclature) described in the Prometheus docs. The image bellow shows an example of an instant query being used in the GraphiQL interface to query the container_memory_usage_bytes metric in that specific moment; note that an inline fragment was needed inside the data field since the result can be of types: Scalar, String, Vector, or Matrix (following Prometheus result types); also, since every type in the schema implements a common interface, the resultType field can be used outside the inline fragment.

Example 1 - Instant Query

Due to GraphQL's type system, some adaptations had to be made. For example, every metric result in an instant/range query carries an attribute called __name__ that denotes the name of the metric. Unfortunately, GraphQL doesn't allow identifiers starting with __, so __name__ is just name. Also, since each metric may have an undefined number of labels+values, each label/value is represented with a type, and each metric can have an array of those types.

Examples

Instant Query

Example 2 - Instant Query

Range Query

GraphiQL_3

Mixing Instant and Range Queries

Example 4 - Mixing Queries

Current Support

By now, the module only wraps the expression queries. Shortly, all the remaining endpoints will be included.

License

Prom-GraphQL is available under the MIT license. See the LICENSE file for more info.