Files
doneit-web/src/app/services/monitoring/opentelemetry/tracer.ts
T

221 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-06-17 09:02:10 +01:00
import { v4 as uuidv4 } from 'uuid';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { Tracer, Span } from '@opentelemetry/sdk-trace-base';
import { OpentelemetryAgendaProvider } from './opentelemetry';
import { Device, DeviceInfo } from '@capacitor/device';
const tracerInstance = OpentelemetryAgendaProvider.getTracer('example-tracer-hole', '111', {
})
// const logger: ILoggerAdapter = new ColoredLoggerService()
let device: DeviceInfo;
Device.getInfo().then(e => {
device = e
});
2024-06-17 10:33:35 +01:00
export function XTracerAsync({ name, log, bugPrint }: any, p0?: any) {
2024-06-17 09:02:10 +01:00
return (
target: unknown,
propertyKey: string,
descriptor: PropertyDescriptor,
) => {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: unknown[]) {
const requestId = uuidv4()
const span = tracerInstance.startSpan(name);
let hasBug:Boolean
2024-06-17 10:33:35 +01:00
const data = {
event: {},
tags: {}
}
2024-06-17 09:02:10 +01:00
2024-06-17 10:33:35 +01:00
const createTracingInstance = (): TracingType => {
2024-06-17 09:02:10 +01:00
return {
span: span as any,
tracer: tracerInstance,
tracerId: requestId,
attributes: SemanticAttributes,
setStatus: (status: any) => {
span.setStatus(status);
},
addEvent: (context: string, message?: any, obj?: any) => {
2024-06-17 10:33:35 +01:00
data.event[context] = message;
2024-06-17 09:02:10 +01:00
const value = [JSON.stringify(message)] as any
span.addEvent(context, value);
},
LocalLogEvent:(context: string, message: any, obj: any) => {
2024-06-17 10:33:35 +01:00
data.tags[context] = message;
2024-06-17 09:02:10 +01:00
},
setAttribute: (key: string, value: string) => {
2024-06-17 10:33:35 +01:00
data.tags[key] = value;
2024-06-17 09:02:10 +01:00
span.setAttribute(key, value);
},
finish: () => {
span.end();
if(bugPrint && hasBug) {
2024-06-17 10:33:35 +01:00
console.error(name, data)
2024-06-17 09:02:10 +01:00
}
},
bugFlag:() => {
hasBug = true
},
createSpan: (name, parent?: any) => {
return tracerInstance.startSpan(name, { root: false }, parent) as Span;
}
}
}
const tracing = createTracingInstance()
tracing.setAttribute('current.page', window.location.pathname);
2024-06-17 10:33:35 +01:00
tracing.setAttribute('device.name', device?.name || device?.model)
2024-06-17 09:02:10 +01:00
args.push(tracing)
try {
const result = await originalMethod.apply(this, args);
tracing.finish()
return result
} catch (e) {
tracing.finish()
console.error(e);
return false
}
};
};
}
export function XTracer({name, log, bugPrint}: any) {
return (
target: unknown,
propertyKey: string,
descriptor: PropertyDescriptor,
) => {
const originalMethod = descriptor.value;
descriptor.value = function (...args: unknown[]) {
const requestId = uuidv4()
const span = tracerInstance.startSpan(name);
let hasBug:Boolean
2024-06-17 10:33:35 +01:00
const data = {
event: {},
tags: {}
}
2024-06-17 09:02:10 +01:00
2024-06-17 10:33:35 +01:00
const createTracingInstance = (): TracingType => {
2024-06-17 09:02:10 +01:00
return {
span: span as any,
tracer: tracerInstance,
tracerId: requestId,
attributes: SemanticAttributes,
setStatus: (status: any) => {
span.setStatus(status);
},
addEvent: (context: string, message?: any, obj?: any) => {
2024-06-17 10:33:35 +01:00
data.event[context] = message;
2024-06-17 09:02:10 +01:00
const value = [JSON.stringify(message)] as any
span.addEvent(context, value);
},
LocalLogEvent:(context: string, message: any, obj: any) => {
2024-06-17 10:33:35 +01:00
data.tags[context] = message;
2024-06-17 09:02:10 +01:00
},
2024-06-17 10:33:35 +01:00
setAttribute: (key: string, value: string) => {
data.tags[key] = value;
2024-06-17 09:02:10 +01:00
span.setAttribute(key, value);
},
finish: () => {
span.end();
if(bugPrint && hasBug) {
2024-06-17 10:33:35 +01:00
console.error(name, data)
2024-06-17 09:02:10 +01:00
}
},
bugFlag:() => {
hasBug = true
},
createSpan: (name, parent?: any) => {
return tracerInstance.startSpan(name, { root: false }, parent) as Span;
}
}
}
const tracing = createTracingInstance()
tracing.setAttribute('current.page', window.location.pathname);
2024-06-17 10:33:35 +01:00
tracing.setAttribute('device.name', device?.name || device?.model)
2024-06-17 09:02:10 +01:00
args.push(tracing)
try {
const result = originalMethod.apply(this, args);
tracing.finish()
return result
} catch (e) {
tracing.finish()
console.error(e);
return false
}
};
};
}
export type TracingType = {
span: Span;
tracer: Tracer;
tracerId: string;
attributes: typeof SemanticAttributes;
// axios: (config?: AxiosRequestConfig) => AxiosInstance;
setStatus: (status: any) => void;
//logEvent: (name: string, attributesOrStartTime?: AttributeValue | TimeInput) => void;
addEvent: (context: string, message?: any, obj?: any) => void;
setAttribute: (key: string, value: string) => void;
LocalLogEvent: (name: string, attributesOrStartTime: any, obj?:any) => void;
finish: () => void;
bugFlag:() => void;
createSpan:(name, parent?: any) => Span;
};
export interface UserInteraction {
readonly params: any;
readonly tracing: TracingType;
readonly user: Pick<any, 'login'>;
readonly headers: Headers & { authorization: string };
}
export type InteractionTrancingInput = Pick<UserInteraction, 'user' | 'tracing'>;
export const getPathWithoutUUID = (path: string) =>
path.replace(
/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
'uuid',
);