UNPKG

@treelab/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

103 lines 4.42 kB
"use strict"; /*! * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 * * http://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. * */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var ContextManager_1 = tslib_1.__importDefault(require("../trace/context/ContextManager")); var Component_1 = require("../trace/Component"); function extractDetails(document, operationName) { var queries = []; var operation; if (document && Array.isArray(document.definitions)) { document.definitions.some(function (definition) { if (!definition || definition.kind !== 'OperationDefinition') return; if (!operationName && operation) return; if (!operationName || (definition.name && definition.name.value === operationName)) { operation = definition; return true; } }); var selections = operation && operation.selectionSet && operation.selectionSet.selections; if (selections && Array.isArray(selections)) { for (var _i = 0, selections_1 = selections; _i < selections_1.length; _i++) { var selection = selections_1[_i]; var kind = selection.name && selection.name.kind; if (kind === 'Name') { var queryName = selection.name.value; if (queryName) queries.push(queryName); } } queries = queries.sort(function (a, b) { if (a > b) return 1; else if (a < b) return -1; return 0; }); } } else { // console.log('unexpected document format - skipping graphql query extraction') } return { queries: queries, operation: operation }; } var GraphqlPlugin = /** @class */ (function () { function GraphqlPlugin() { this.module = 'graphql'; this.versions = '*'; } GraphqlPlugin.prototype.install = function (installer) { this.interceptServerRequest(installer); }; GraphqlPlugin.prototype.interceptServerRequest = function (installer) { try { // catch no graphql var graphqlExecute = installer.require('graphql/execution/execute'); var _execute_1 = graphqlExecute.execute; graphqlExecute.execute = function () { var span = ContextManager_1.default.currentSpan; if (span && arguments[0] && arguments[0].operationName) { span.setOperation(arguments[0].operationName); span.component = Component_1.Component.GRAPHQL; } else if (span && arguments[1]) { var operationName = arguments[5]; var details = extractDetails(arguments[1], arguments[5]); var queries = details.queries; operationName = operationName || (details.operation && details.operation.name && details.operation.name.value); span.component = Component_1.Component.GRAPHQL; if (operationName) { span.setOperation(operationName); } else if (queries.length > 0) { span.setOperation(queries.join(', ')); } } return _execute_1.apply(this, arguments); }; } catch (_a) { } }; return GraphqlPlugin; }()); // noinspection JSUnusedGlobalSymbols exports.default = new GraphqlPlugin(); //# sourceMappingURL=GraphqlPlugin.js.map