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
26 changes: 26 additions & 0 deletions examples/graphql/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const baseConfig = require('../../eslint.config');

module.exports = {
...baseConfig,
env: {
node: true,
},
};
1 change: 1 addition & 0 deletions examples/graphql/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
22 changes: 19 additions & 3 deletions examples/graphql/client-federation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const url = require('url');
Expand Down Expand Up @@ -27,13 +43,13 @@ function makeRequest(query) {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
const req = http.request(options, res => {
const data = [];
res.on('data', (chunk) => data.push(chunk));
res.on('data', chunk => data.push(chunk));
res.on('end', () => {
resolve(data.toString());
});
res.on('error', (err) => {
res.on('error', err => {
reject(err);
});
});
Expand Down
22 changes: 19 additions & 3 deletions examples/graphql/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const url = require('url');
Expand Down Expand Up @@ -32,13 +48,13 @@ function makeRequest(query) {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
const req = http.request(options, res => {
const data = [];
res.on('data', (chunk) => data.push(chunk));
res.on('data', chunk => data.push(chunk));
res.on('end', () => {
resolve(data.toString());
});
res.on('error', (err) => {
res.on('error', err => {
reject(err);
});
});
Expand Down
16 changes: 16 additions & 0 deletions examples/graphql/countries-service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const { fetch } = require('cross-fetch');
Expand Down
2 changes: 2 additions & 0 deletions examples/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"description": "Example of using @opentelemetry/plugin-graphql with OpenTelemetry",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext=ts,js,mjs",
"lint:fix": "eslint . --ext=ts,js,mjs --fix",
"client": "node ./client.js",
"client:federation": "node ./client-federation.js",
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
Expand Down
51 changes: 36 additions & 15 deletions examples/graphql/schema.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

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

const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json';
const url1 =
'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/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));
https
.get(url, response => {
let data = '';
response.on('data', chunk => {
data += chunk;
});
response.on('end', () => {
resolve(JSON.parse(data));
});
})
.on('error', err => {
reject(err);
});
}).on('error', (err) => {
reject(err);
});
});
}

Expand All @@ -27,7 +46,7 @@ const books = [];
function addBook(name, authorIds) {
let authorIdsLocal = authorIds;
if (typeof authorIdsLocal === 'string') {
authorIdsLocal = authorIdsLocal.split(',').map((id) => parseInt(id, 10));
authorIdsLocal = authorIdsLocal.split(',').map(id => parseInt(id, 10));
}
const id = books.length;
books.push({
Expand Down Expand Up @@ -90,7 +109,7 @@ module.exports = function buildSchema() {
type: graphql.GraphQLString,
resolve(_obj, _args) {
return new Promise((resolve, reject) => {
getData(url1).then((response) => {
getData(url1).then(response => {
resolve(response.description);
}, reject);
});
Expand Down Expand Up @@ -139,7 +158,7 @@ module.exports = function buildSchema() {
authors: {
type: new graphql.GraphQLList(Author),
resolve(obj, _args) {
return obj.authorIds.map((id) => authors[id]);
return obj.authorIds.map(id => authors[id]);
},
},
},
Expand Down Expand Up @@ -188,7 +207,9 @@ module.exports = function buildSchema() {
type: Book,
args: {
name: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) },
authorIds: { 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));
Expand Down
16 changes: 16 additions & 0 deletions examples/graphql/server-apollo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require('./tracer');
Expand Down
27 changes: 23 additions & 4 deletions examples/graphql/server-express.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require('./tracer');
Expand All @@ -9,10 +25,13 @@ const buildSchema = require('./schema');
const schema = buildSchema();

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

app.listen(4000);

Expand Down
16 changes: 16 additions & 0 deletions examples/graphql/server-federation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

require('./tracer');
Expand Down
29 changes: 26 additions & 3 deletions examples/graphql/tracer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { GraphQLInstrumentation } = require('@opentelemetry/instrumentation-graphql');
const { ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const {
GraphQLInstrumentation,
} = require('@opentelemetry/instrumentation-graphql');
const {
ConsoleSpanExporter,
SimpleSpanProcessor,
} = require('@opentelemetry/sdk-trace-base');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-http');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
const {
ExpressInstrumentation,
} = require('@opentelemetry/instrumentation-express');
const { Resource } = require('@opentelemetry/resources');
const { ATTR_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');

Expand Down