import * as apmCore from "elastic-apm-js-core"; import TracerTransaction from '../transaction/index' import TracerEvent from '../event/index' import TracerNetwork from "../network/index"; import TarcerElement from "../element/index"; import { UserContext } from './type' import { CUSTOM_EVENT_TASK } from './type' import * as Tools from "../tool"; export function start(){ const { ignoreRequestUrls,serverUrl,isCatchResponseContent,automaticStart, } = this._config if(this.isStart){ return Tools.warnError('服务已启动') } // start this.apmCoreServiceFactory = apmCore.createServiceFactory(); //new server this.tracerTransaction = new TracerTransaction( this.apmCoreServiceFactory,apmCore.createServiceFactory() ) this.tracerEvent = new TracerEvent() this.tarcerElement = new TarcerElement(); this.tracerNetwork = new TracerNetwork({ ignoreUrls:[ serverUrl, ...(ignoreRequestUrls?ignoreRequestUrls:[]) ], isCatchResponseContent:isCatchResponseContent?true:false }); //init begin this.initApmCore() this.initPageTracer() this.isStart = true } export function setUserInfo(userContext: UserContext){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} var configService = this.apmCoreConfigServer || this.apmCoreServiceFactory.getService("ConfigService"); configService.setUserContext(userContext); //set custom transaction this.tracerTransaction.setCustomTransactionUserInfo(userContext) }; export function createCustomLog(eventName:string){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} var partTask = this.tracerTransaction.createCustomEventTransaction(eventName,CUSTOM_EVENT_TASK,{ transactionSampleRate:1 }) if(!partTask){ return false; } return partTask } export function captureError (error) { if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} var errorLogging = this.apmCoreServiceFactory.getService('ErrorLogging') return errorLogging.logError(error) } //catch control export function stopElementActionCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tarcerElement.stopElementActionCatch() } export function resumeElementActionCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tarcerElement.resumeElementActionCatch() } export function stopPageChangeEventCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tracerEvent.stopPageChangeEventCatch() } export function resumePageChangeEventCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tracerEvent.resumePageChangeEventCatch() } export function stopRequestCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tracerNetwork.stopRequestCatch() } export function resumeRequestCatch(){ if(!this.isStart){return Tools.warnError('服务未启动,请先调用start()方法启动服务')} this.tracerNetwork.resumeRequestCatch() }