问题描述
我正在使用Elastic APM。我发现当以某种方式与GraphQL / Apollo Server一起使用时,猫鼬集成无法正常工作。我写了一个这样的apollo-server插件来启动/停止事务:
import apm from 'elastic-apm-node/start';
import _ from 'lodash';
import { gql } from 'apollo-server-express';
const apmPlugin = {
requestDidStart: (requestContext) => {
const query = gql`${requestContext.request.query}`;
const type = _.get(query,'deFinitions[0].operation','');
const funcName = _.get(query,'deFinitions[0].name.value','');
const txnName = type && funcName ? `${type} ${funcName}` : '/gql';
const txn = apm.startTransaction(txnName,'graphql');
return {
didEncounterErrors: (err) => {
if (txn) {
txn.result = err.errors.length === 1 ? err.errors[0].name : 'error';
}
},willSendResponse: (res) => {
if (Array.isArray(res.response.errors).length === 0) {
txn.result = 'success';
}
if (typeof txn?.end === 'function') txn.end();
},};
},};
它有效,但是我缺少猫鼬的跨度,当我启用跟踪时,这是我看到的:
start trace {
trans: 'ddc03cb86cf71448',parent: undefined,trace: '7127e63abe8e6d5086d36c13deee8183',name: 'unnamed',type: 'graphql',subtype: null,action: null
}
start span {
span: '94fc2307e19dd187',parent: 'ddc03cb86cf71448',name: 'GraphQL: UnkNown Query',type: 'db',subtype: 'graphql',action: 'execute'
}
intercepted call to graphql.execute { id: 'ddc03cb86cf71448' }
Mongoose: yyy.findOne({ domainSlug: 'xxx' },{ projection: {} })
no active transaction found - cannot build new span
Mongoose: yyy.findOne({ email: 'xxx',company: ObjectId("5f1ab19010469e8eb952e754") }},{ projection: {} })
no active transaction found - cannot build new span
Mongoose: roles.find({ _id: { '$in': [ ObjectId("5f1ab19010469e8eb952e786") ] } },{ skip: undefined,limit: undefined,perDocumentLimit: undefined,projection: {}})
no active transaction found - cannot build new span
ended span {
span: '94fc2307e19dd187',name: 'GraphQL: xxx',action: 'execute'
}
encoding span {
span: '94fc2307e19dd187',type: 'db'
}
sending span {
span: '94fc2307e19dd187',type: 'db'
}
sending transaction {
trans: 'ddc03cb86cf71448',trace: '7127e63abe8e6d5086d36c13deee8183'
}
ended transaction {
trans: 'ddc03cb86cf71448',result: 'success',name: 'mutation doLogin'
}
请注意,在那些猫鼬调用之前和之后创建的跨度都很好,但是猫鼬调用似乎无法以某种方式找到活动交易。
no active transaction found - cannot build new span
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)