Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Graphql #226

Merged
merged 16 commits into from
Oct 30, 2020
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
57 changes: 57 additions & 0 deletions examples/graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Overview OpenTelemetry GraphQL Instrumentation Example

This example shows how to use 2 popular graphql servers

- [Apollo GraphQL](https://www.npmjs.com/package/apollo-server)
- [GraphQL HTTP Server Middleware](https://www.npmjs.com/package/express-graphql)

and [@opentelemetry/instrumentation-graphql](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-instrumentation-graphql) to instrument a simple Node.js application.

This instrumentation should work with any graphql server as it instruments graphql directly.

This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-exporter-collector).

## Installation

```shell script
# from this directory
npm install
```

## Run the Application

1. Run docker

```shell script
# from this directory
npm run docker:start
```

2. Run server - depends on your preference

```shell script
# from this directory
npm run server:express
// or
npm run server:apollo
```

3. Open page at <http://localhost:9411/zipkin/> - you should be able to see the spans in zipkin

4. Run example client

```shell script
# from this directory
npm run client
```

5. You can also write your own queries, open page `http://localhost:4000/graphql`

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on tracing, visit: <https://github.com/open-telemetry/opentelemetry-js>

## LICENSE

Apache License 2.0
49 changes: 49 additions & 0 deletions examples/graphql/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const url = require('url');
const http = require('http');
// Construct a schema, using GraphQL schema language

const source = `
query {
books {
name
authors {
name
address {
country
}
}
}
}
`;

makeRequest(source).then(console.log);

function makeRequest(query) {
return new Promise((resolve, reject) => {
const parsedUrl = new url.URL('http://localhost:4000/graphql');
const options = {
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
const data = [];
res.on('data', (chunk) => data.push(chunk));
res.on('end', () => {
resolve(data.toString());
});
res.on('error', (err) => {
reject(err);
});
});

req.write(JSON.stringify({ query }));
req.end();
});
}
29 changes: 29 additions & 0 deletions examples/graphql/docker/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
receivers:
otlp:
protocols:
grpc:
http:
cors_allowed_origins:
- http://*
- https://*

exporters:
zipkin:
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
prometheus:
endpoint: "0.0.0.0:9464"

processors:
batch:
queued_retry:

service:
pipelines:
traces:
receivers: [otlp]
exporters: [zipkin]
processors: [batch, queued_retry]
metrics:
receivers: [otlp]
exporters: [prometheus]
processors: [batch, queued_retry]
30 changes: 30 additions & 0 deletions examples/graphql/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "3"
services:
# Collector
collector:
# image: otel/opentelemetry-collector:latest
image: otel/opentelemetry-collector:0.13.0
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
volumes:
- ./collector-config.yaml:/conf/collector-config.yaml
ports:
- "9464:9464"
- "55680:55680"
- "55681:55681"
depends_on:
- zipkin-all-in-one

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
ports:
- "9411:9411"

# Prometheus
prometheus:
container_name: prometheus
image: prom/prometheus:latest
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
9 changes: 9 additions & 0 deletions examples/graphql/docker/prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global:
scrape_interval: 15s # Default is every 1 minute.

scrape_configs:
- job_name: 'collector'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['collector:9464']
52 changes: 52 additions & 0 deletions examples/graphql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "opentelemetry-plugin-graphql-example",
"private": true,
"version": "0.11.0",
"description": "Example of using @opentelemetry/plugin-graphql with OpenTelemetry",
"main": "index.js",
"scripts": {
"client": "node ./client.js",
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
"docker:startd": "cd ./docker && docker-compose down && docker-compose up -d",
"docker:stop": "cd ./docker && docker-compose down",
"server:express": "node ./server-express.js",
"server:apollo": "node ./server-apollo.js"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git"
},
"keywords": [
"opentelemetry",
"http",
"tracing",
"graphql"
],
"engines": {
"node": ">=8"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^0.12.0",
"@opentelemetry/exporter-collector": "^0.12.0",
"@opentelemetry/node": "^0.12.0",
"@opentelemetry/plugin-express": "^0.10.0",
"@opentelemetry/instrumentation-graphql": "^0.11.0",
"@opentelemetry/plugin-http": "^0.12.0",
"@opentelemetry/plugin-https": "^0.12.0",
"@opentelemetry/tracing": "^0.12.0",
"apollo-server": "^2.18.1",
"express": "^4.17.1",
"express-graphql": "^0.11.0",
"graphql": "^15.3.0",
"qs-middleware": "^1.0.3"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme",
"devDependencies": {
"cross-env": "^6.0.0"
}
}
Loading