mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
add open telemetry
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
||||
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
|
||||
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
|
||||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
|
||||
import { Resource } from '@opentelemetry/resources';
|
||||
|
||||
function createProvider(serviceName) {
|
||||
const provider = new WebTracerProvider({
|
||||
resource: new Resource({
|
||||
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
|
||||
}),
|
||||
});
|
||||
|
||||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
|
||||
provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({
|
||||
url: 'https://5.180.182.151/zipkin-endpoint/api/v2/spans',
|
||||
serviceName: serviceName,
|
||||
// Uncomment and customize the following if needed
|
||||
// getExportRequestHeaders: () => {
|
||||
// return {
|
||||
// foo: 'bar',
|
||||
// };
|
||||
// }
|
||||
})));
|
||||
|
||||
provider.register();
|
||||
return provider;
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
export const OpentelemetryChatProvider = createProvider('FO-chat-service');
|
||||
export const OpentelemetryAgendaProvider = createProvider('FO-agenda-service');
|
||||
export const OpentelemetryPublicationProvider = createProvider('FO-publication-service');
|
||||
@@ -0,0 +1,223 @@
|
||||
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
|
||||
});
|
||||
|
||||
export function XTracerAsync({name, log, bugPrint}: any) {
|
||||
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
|
||||
|
||||
const createTracingInstance = (): TracingType => {
|
||||
const logs: any[] = []
|
||||
|
||||
|
||||
return {
|
||||
span: span as any,
|
||||
tracer: tracerInstance,
|
||||
tracerId: requestId,
|
||||
attributes: SemanticAttributes,
|
||||
setStatus: (status: any) => {
|
||||
span.setStatus(status);
|
||||
},
|
||||
addEvent: (context: string, message?: any, obj?: any) => {
|
||||
if(log == true) {
|
||||
// logger.error(obj, context, message)
|
||||
} else {
|
||||
logs.push({context, message: message, obj})
|
||||
}
|
||||
|
||||
const value = [JSON.stringify(message)] as any
|
||||
span.addEvent(context, value);
|
||||
},
|
||||
LocalLogEvent:(context: string, message: any, obj: any) => {
|
||||
logs.push({context, message, obj})
|
||||
},
|
||||
setAttribute: (key: string, value: string) => {
|
||||
span.setAttribute(key, value);
|
||||
},
|
||||
finish: () => {
|
||||
span.end();
|
||||
if(bugPrint && hasBug) {
|
||||
for(const {context, message, obj} of logs) {
|
||||
// logger.error(obj, context, message)
|
||||
}
|
||||
}
|
||||
},
|
||||
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);
|
||||
tracing.setAttribute('device.name', device.name || device.model)
|
||||
|
||||
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
|
||||
|
||||
const createTracingInstance = (): TracingType => {
|
||||
const logs: any[] = []
|
||||
|
||||
|
||||
return {
|
||||
span: span as any,
|
||||
tracer: tracerInstance,
|
||||
tracerId: requestId,
|
||||
attributes: SemanticAttributes,
|
||||
setStatus: (status: any) => {
|
||||
span.setStatus(status);
|
||||
},
|
||||
addEvent: (context: string, message?: any, obj?: any) => {
|
||||
if(log == true) {
|
||||
// logger.error(obj, context, message)
|
||||
} else {
|
||||
logs.push({context, message: message, obj})
|
||||
}
|
||||
|
||||
const value = [JSON.stringify(message)] as any
|
||||
span.addEvent(context, value);
|
||||
},
|
||||
LocalLogEvent:(context: string, message: any, obj: any) => {
|
||||
logs.push({context, message, obj})
|
||||
},
|
||||
setAttribute: (key: string, value: any) => {
|
||||
span.setAttribute(key, value);
|
||||
},
|
||||
finish: () => {
|
||||
span.end();
|
||||
if(bugPrint && hasBug) {
|
||||
for(const {context, message, obj} of logs) {
|
||||
// logger.error(obj, context, message)
|
||||
}
|
||||
}
|
||||
},
|
||||
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);
|
||||
tracing.setAttribute('device.name', device.name || device.model)
|
||||
|
||||
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',
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user