Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node CI
name: build

on: [push]

Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: publish

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: yarn install
- run: yarn build
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github
src
node_modules
examples
tsconfig.json
dist/__tests__
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# graphql-lightstep-middleware

[![NPM](https://nodei.co/npm/graphql-lightstep-middleware.png)](https://npmjs.org/package/graphql-lightstep-middleware)

GraphQL middleware to instrument resolvers with opentracing traces for lightstep collector

## Usage
![build](https://github.com/addityasingh/graphql-lightstep-middleware/workflows/build/badge.svg)
[![downloads](https://img.shields.io/npm/dt/graphql-lightstep-middleware.svg)](https://npmjs.org/package/graphql-lightstep-middleware?cacheSeconds=3600)
[![version](https://img.shields.io/npm/v/graphql-lightstep-middleware.svg)](https://npmjs.org/package/graphql-lightstep-middleware?cacheSeconds=3600)

## Table of contents

- [Getting started](#getting-started)
- [API](#api)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [Contributing](#contributing)
- [Good first issues](#good-first-issues)
- [License](#licence)

## Getting started

1. Install the package and graphql-middleware

Expand Down Expand Up @@ -85,4 +101,32 @@ app.use(
);
```

## Development
## API

### middleware = graphqlLightstepMiddleware([options])

- `options`
- `tracer`: An optional `lightstep` tracer object
- `hooks`: Lost of `PreResolve` and `PostResolve` hooks

Refer the [examples](./examples) for more usage examples

## Contributing

`graphql-lightstep-middleware` package intends to support contribution and support and thanks the open source community to making it better. Read below to learn how you can improve this repository and package

### Code of Conduct

Please check the [CODE OF CONDUCT](./CODE_OF_CONDUCT) which we have in place to ensure safe and supportive environment for contributors

### Contributing

Feel free to create issues and bugs in the issues section using issues and bugs template. Please also ensure that there are not existing issues created on the same topic

### Good first issues

Please check issues labeled [#good-first-issues](https://github.com/addityasingh/graphql-lightstep-middleware/labels/good%20first%20issue) under the issues section

## Licence

`graphql-lightstep-middleware` uses [MIT License](./LICENSE)
20 changes: 20 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Verifying `graphql-lightstep-middleware` example can be done with below setup

## Run lightstep satelite

To test lightstep locally, we need to first run `lightstep` satelite for developer mode. This is documented on lightstep site [Use Developer mode](https://docs.lightstep.com/docs/use-developer-mode-to-quickly-see-traces)

## Run example

To run the example for `graphql-with-lightstep` run

```sh
yarn run tsc && node dist/graphql-with-lightstep.js
```

## Result

If the setup works correctly, you would see below event in lightstep dashboard
![trace event](https://user-images.githubusercontent.com/5351262/73496357-adf85d00-43b8-11ea-85c2-468f10afbf2f.png)
and the corresponding trace as below
![trace view](https://user-images.githubusercontent.com/5351262/73496358-adf85d00-43b8-11ea-9b56-f53c5a6b2220.png)
22 changes: 22 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "examples",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsc",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql-tools": "^4.0.6",
"lightstep-tracer": "^0.25.1"
},
"devDependencies": {
"typescript": "^3.7.5"
}
}
59 changes: 59 additions & 0 deletions examples/src/graphql-with-lightstep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express from "express";
import { applyMiddleware } from "graphql-middleware";
import { makeExecutableSchema } from 'graphql-tools';
const graphqlExpressHttp = require("express-graphql");
import { initGlobalTracer, globalTracer } from "opentracing";
import { Tracer as LightstepTracer } from "lightstep-tracer";
import graphqlLightstepMiddleware from '../../dist/index.js';


// Initialise the lightstep tracer
initGlobalTracer(new LightstepTracer({
access_token: "developer",
component_name: "graphql-lightstep-middleware",
collector_host: "localhost",
collector_port: 8360,
plaintext: true,
collector_encryption: "none"
} as any));

// Construct a schema, using GraphQL schema language
const typeDefs = `
type Query {
hello(name: String): String
}
`;

// The root provides a resolver function for each API endpoint
const resolvers = {
Query: {
hello: (parent, args, context) => {
const result = `Hello ${args.name ? args.name : "world"}!`;
// The rootSpan is available in the context now
context.tracing.rootSpan.addTags({
helloResolver: result
});
return result;
},
}
};

const lightstepMiddleware = graphqlLightstepMiddleware({
tracer: globalTracer()
});

const schema = applyMiddleware(
makeExecutableSchema({typeDefs, resolvers}),
lightstepMiddleware,
)

const app = express();
app.use("/graphql", graphqlExpressHttp({
schema: schema,
rootValue: resolvers,
graphiql: true,
}))

app.listen(4001, () => {
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
});
18 changes: 18 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"declaration": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": false,
"esModuleInterop": true,
"pretty": true,
"removeComments": true,
"lib": ["es6", "esnext.asynciterable"]
},
"exclude": ["node_modules", "dist"]
}
Loading