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

feat: creating one auto loader for instrumentation and old plugins #1731

Merged
merged 13 commits into from
Jan 7, 2021
Merged
1 change: 1 addition & 0 deletions examples/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Testing
49 changes: 49 additions & 0 deletions examples/test/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/test/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/test/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/test/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/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "opentelemetry-testing",
"private": true,
"version": "0.11.0",
"description": "Testing",
"main": "index.js",
"scripts": {
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
"docker:stop": "cd ./docker && docker-compose down",
"start:test": "node ./client.js",
"server:express": "node ./server-express.js"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/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/context-async-hooks": "^0.12.0",
"@opentelemetry/exporter-collector": "^0.12.0",
"@opentelemetry/node": "^0.12.0",
"@opentelemetry/plugin-express": "^0.11.0",
"@opentelemetry/instrumentation": "^0.12.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"
}
}
202 changes: 202 additions & 0 deletions examples/test/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
'use strict';

const https = require('https');
const graphql = require('graphql');

const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';

function getData(url) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', (err) => {
reject(err);
});
});
}

const authors = [];
const books = [];

function addBook(name, authorIds) {
let authorIdsLocal = authorIds;
if (typeof authorIdsLocal === 'string') {
authorIdsLocal = authorIdsLocal.split(',').map((id) => parseInt(id, 10));
}
const id = books.length;
books.push({
id,
name,
authorIds: authorIdsLocal,
});
return books[books.length - 1];
}

function addAuthor(name, country, city) {
const id = authors.length;
authors.push({
id,
name,
address: {
country,
city,
},
});
return authors[authors.length - 1];
}

function getBook(id) {
return books[id];
}

function getAuthor(id) {
return authors[id];
}

function prepareData() {
addAuthor('John', 'Poland', 'Szczecin');
addAuthor('Alice', 'Poland', 'Warsaw');
addAuthor('Bob', 'England', 'London');
addAuthor('Christine', 'France', 'Paris');
addBook('First Book', [0, 1]);
addBook('Second Book', [2]);
addBook('Third Book', [3]);
}

prepareData();
module.exports = function buildSchema() {
const Author = new graphql.GraphQLObjectType({
name: 'Author',
fields: {
id: {
type: graphql.GraphQLString,
resolve(obj, _args) {
return obj.id;
},
},
name: {
type: graphql.GraphQLString,
resolve(obj, _args) {
return obj.name;
},
},
description: {
type: graphql.GraphQLString,
resolve(_obj, _args) {
return new Promise((resolve, reject) => {
getData(url1).then((response) => {
resolve(response.description);
}, reject);
});
},
},
address: {
type: new graphql.GraphQLObjectType({
name: 'Address',
fields: {
country: {
type: graphql.GraphQLString,
resolve(obj, _args) {
return obj.country;
},
},
city: {
type: graphql.GraphQLString,
resolve(obj, _args) {
return obj.city;
},
},
},
}),
resolve(obj, _args) {
return obj.address;
},
},
},
});

const Book = new graphql.GraphQLObjectType({
name: 'Book',
fields: {
id: {
type: graphql.GraphQLInt,
resolve(obj, _args) {
return obj.id;
},
},
name: {
type: graphql.GraphQLString,
resolve(obj, _args) {
return obj.name;
},
},
authors: {
type: new graphql.GraphQLList(Author),
resolve(obj, _args) {
return obj.authorIds.map((id) => authors[id]);
},
},
},
});

const query = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
author: {
type: Author,
args: {
id: { type: graphql.GraphQLInt },
},
resolve(obj, args, _context) {
return Promise.resolve(getAuthor(args.id));
},
},
authors: {
type: new graphql.GraphQLList(Author),
resolve(_obj, _args, _context) {
return Promise.resolve(authors);
},
},
book: {
type: Book,
args: {
id: { type: graphql.GraphQLInt },
},
resolve(obj, args, _context) {
return Promise.resolve(getBook(args.id));
},
},
books: {
type: new graphql.GraphQLList(Book),
resolve(_obj, _args, _context) {
return Promise.resolve(books);
},
},
},
});

const mutation = new graphql.GraphQLObjectType({
name: 'Mutation',
fields: {
addBook: {
type: Book,
args: {
name: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
authorIds: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
},
resolve(obj, args, _context) {
return Promise.resolve(addBook(args.name, args.authorIds));
},
},
},
});

const schema = new graphql.GraphQLSchema({ query, mutation });
return schema;
};
19 changes: 19 additions & 0 deletions examples/test/server-express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

require('./tracer');

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const buildSchema = require('./schema');

const schema = buildSchema();

const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
}));

app.listen(4000);

console.log('Running a GraphQL API server at http://localhost:4000/graphql');
Loading