• Stars
    star
    183
  • Rank 209,504 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Performance trace your Apollo GraphQL server with Opentracing

Apollo Opentracing npm version Build Status semantic-release All Contributors

Apollo Opentracing allows you to integrate open source baked performance tracing to your Apollo server based on industry standards for tracing.

  • 🚀 Request & Field level resolvers are traced out of the box
  • 🔍 Queries and results are logged, to make debugging easier
  • ⚙️ Select which requests you want to trace
  • 🔗 Spans transmitted through the HTTP Headers are picked up
  • 🔧 Use the opentracing compatible tracer you like, e.g.
  • 🦖 Support from node 6 on

Installation

Run npm install --save apollo-opentracing given that you already setup an opentracing tracer accordingly.

Setup

We need two types of tracer (which could be identical if you like):

  • server: Only used for the root (the first span we will start)
  • local: Used to start every other span
const { graphqlExpress } = require("apollo-server-express");
const {serverTracer, localTracer} = require("./tracer");
+const OpentracingPlugin = require("apollo-opentracing").default;

app.use(
  "/graphql",
  bodyParser.json(),
  graphqlExpress({
    schema,
+   plugins: [OpentracingPlugin({
+     server: serverTracer,
+     local: localTracer,
+   })]
  })
)

Connecting Services

example image

To connect other services you need to use the opentracing inject function of your tracer. We pass the current span down to your resolvers as info.span, so you should use it.

You can also make use of it and add new logs or tags on the fly if you like. This may look something like this:

myFieldResolver(source, args, context, info) {
  const headers = {...};

  const parentSpan = info.span;
  // please use the same tracer you passed down to the extension
  const networkSpan = tracer.startSpan("NetworkRequest:" + endpoint, {
    childOf: parentSpan
  });

  // Let's transfer the span information to the headers
  tracer.inject(
    networkSpan,
    YourOpentracingImplementation.FORMAT_HTTP_HEADERS,
    headers
  );

  return doNetworkRequest(endpoint, headers).then(result => {
    networkSpan.finish()
    return result;
  }, err => {
    networkSpan.log({
      error: true,
      errorMessage: err
    });

    networkSpan.finish();
    return err;
  });
}

Selective Tracing

Sometimes you don't want to trace everything, so we provide ways to select if you want to start a span right now or not.

By Request

If you construct the extension with shouldTraceRequest you get the option to opt-in or out on a request basis. When you don't start the span for the request the field resolvers will also not be used.

The function is called with the same arguments as the requestDidStart function extensions can provide, which is documented here.

When the request is not traced there will also be no traces of the field resolvers.

By Field

There might be certain field resolvers that are not worth the tracing, e.g. when they get a value out of an object and need no further tracing. To control if you want a field resolver to be traced you can pass the shouldTraceFieldResolver option to the constructor. The function is called with the same arguments as your field resolver and you can get the name of the field by info.fieldName. When you return false no traces will be made of this field resolvers and all underlying ones.

Modifying span metadata

If you'd like to add custom tags or logs to span you can construct the extension with onRequestResolve. The function is called with two arguments: span and infos onRequestResolve?: (span: Span, info: RequestStart)

Using your own request span

If you need to take control of initializing the request span (e.g because you need to use it during context initialization) you can do so by having creating it as context.requestSpan.

Options

  • server: Opentracing Tracer for the incoming request
  • local: Opentracing Tracer for the local and outgoing requests
  • onFieldResolve(source: any, args: { [argName: string]: any }, context: SpanContext, info: GraphQLResolveInfo): Allow users to add extra information to the span
  • onFieldResolveFinish(error: Error | null, result: any, span: Span): Callback after a field was resolved
  • shouldTraceRequest & shouldTraceFieldResolver: See Selective Tracing
  • onRequestResolve(span: Span, info: GraphQLRequestContext): Add extra information to the request span
  • createCustomSpanName(name: String, info: GraphQLResolveInfo): Allow users to provide customized span name
  • onRequestError(rootSpan: Span, info: GraphQLRequestContextDidEncounterErrors): Callback when a request errors

Contributing

Please feel free to add issues with new ideas, bugs and anything that might come up. Let's make performance measurement to everyone <3

Contributors

Thanks goes to these wonderful people (emoji key):


Daniel Schmidt

💻 🤔

Ciaran Liedeman

🐛 💻 ⚠️

Jens Ulrich Hjuler Pedersen

🐛 🤔 👀

Francesca

💻

Ricardo Casares

💻

Michał Wieczorek

💻

Koen Punt

💻

Zeke Nierenberg

💻

Tomáš Voslař

💻

Ben Kimball

💻

Jiapei Liang

💻

Richard W

🤔 🔬

Lee Weisberger

💻

Dustin Deus

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT

More Repositories

1

react-native-dismiss-keyboard

A simple way to dismiss the keyboard programmatically in a react native application
JavaScript
95
star
2

flow-bro

Some helpers (least coverage) for your flow workflow
JavaScript
33
star
3

objective-c-parser

Get the JSON representation of an Objective-C header file
Objective-C
20
star
4

zipkin-javascript-opentracing

Opentracing implementation for Zipkin in Javascript
JavaScript
19
star
5

react-native-video-upload-demo

This is a project demoing how to upload videos to a node express web server
JavaScript
18
star
6

reactive-graphql-react

React bindings for reactive-graphql
TypeScript
9
star
7

ExpoDrawerLayoutExample

An example application for react-native-drawer-layout
JavaScript
8
star
8

jest-runner-cypress-io

Jest runner for cypress
JavaScript
7
star
9

generator-react-native-module

Generates a node module to include in a react-native application
JavaScript
7
star
10

cdktf-cdk8s

A compatability layer for using cdk8s constructs within Terraform CDK
TypeScript
6
star
11

alfred-pomodoro

Start a pomodoro without interruptions through Alfred
JavaScript
6
star
12

eslint-plugin-test-names

Validates if the test names are set up correctly
JavaScript
5
star
13

generator-gitbook

JavaScript
5
star
14

jest-evaluation

This project is a test to evaluate testing React and React Native applications with jest.
JavaScript
5
star
15

babel-generate-guard-clauses

A helper to generate different guard clauses
JavaScript
3
star
16

jest-environment-artillery

A jest environment for running load tests on your application.
JavaScript
3
star
17

cdktf-multi-stack-tfe

Setting up Terraform Cloud / Terraform Enterprise workspaces can be tiring when dealing with CDK for Terraform applications spanning multiple stacks and therefore workspaces. This library aims to automate this.
TypeScript
3
star
18

react-opentracing

HOC for tracing react components with opentracing
2
star
19

cdktf-local-build

A construct that encapsulates different building methods, e.g. for Node, Rust, Docker.
TypeScript
2
star
20

projen-cdktf-hybrid-construct

Projen template for CDKTF Constructs that should also be used as Terraform Modules.
TypeScript
2
star
21

jest-runner-multi

JavaScript
2
star
22

DrawerLayoutPolyfillExample

JavaScript
2
star
23

cdktf-integration-docker-example

TypeScript
2
star
24

dev-bro

Tiny helper that runs jest and flow in parallel watch mode
JavaScript
2
star
25

zipkin-playground

This is a repo full of examples with zipkin opentracing compatible APIs that I want to use for a talk of mine about client side tracing.
JavaScript
2
star
26

typescript-all-in-converter

TypeScript
2
star
27

ansible-role-react-native

This is an ansible role to build the dev setup for a react-native application
Shell
2
star
28

turnierApp

[NO LONGER MAINTAINED] This is an organisation web app for competitive ballroom dancers.
Ruby
1
star
29

it-sec

IT-Security at CAU Kiel
C
1
star
30

terraform-cdk-terraform-module-publishing-on-gh-packages

TypeScript
1
star
31

blogpost-pdf-form

Objective-C
1
star
32

react-native-keyboard-shown-example

Objective-C
1
star
33

ki

This is a script of the KI course at CAU Kiel.
Python
1
star
34

cdktf-issue-1724

Java
1
star
35

clorg

Club Organizer
Ruby
1
star
36

Snake

React Native Snake Game (WIP)
Objective-C
1
star
37

jsii-rs

Rust
1
star
38

backporter

Backports PRs to older versions
JavaScript
1
star
39

zipkin-proxy

Reroute all request, but with zipkin traces attached
JavaScript
1
star
40

vergleichsportal

Ruby
1
star
41

mapReference

This is a react native project, which shows a map with some random pins in order to test this functionality
Objective-C
1
star
42

asteroids-java

Asteroids clone in Java
Java
1
star
43

java-method-parser

A node utility to get the methods out of java files
Java
1
star
44

docusaurus-onclick

Demo the onclick not being called but in docusaurus
JavaScript
1
star
45

chat

RN Chat
Go
1
star
46

react-native-rain-effect

JavaScript
1
star
47

terraform-cdk-hybrid-module-publishing-on-gh-packages

TypeScript
1
star
48

react-native-drawer-layout-issue26

JavaScript
1
star
49

graphql-yoga-pubsub-rxjs

TypeScript
1
star
50

jsii-workbench

When working with JSII you sometimes just want to drop in an interface and inspect all generated code in differnet languages.
JavaScript
1
star
51

fp-jargon

A Electron example for showing and quizzing functional programming jargon
JavaScript
1
star
52

mermaid-demo

A small demo of mermaid
JavaScript
1
star
53

keyword-notifier

keyword-notifierI'd like to know if folks mention my project online, the goal is to fetch from different sources and send it via e-mail or slack. And learn rust in the process
Rust
1
star
54

cdktf-tf-module-stack

A drop-in replacement for cdktf.TerraformStack that let's you define Terraform modules as construct
TypeScript
1
star
55

cdktf-local-exec

A simple construct that executes a command locally. This is useful to run build steps within your CDKTF Program or to run a post action after a resource is created.
TypeScript
1
star
56

choreo-builder

This is an application, which builds a choreography via drag and drop.
JavaScript
1
star