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,30 @@
|
||||
import { metrics } from '@opentelemetry/api';
|
||||
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
|
||||
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
// Initialize OpenTelemetry metrics
|
||||
const meterProvider = new MeterProvider();
|
||||
metrics.setGlobalMeterProvider(meterProvider);
|
||||
|
||||
if (window.location.protocol !== 'https:' && environment.apiURL != 'https://gdqas-api.oapr.gov.ao/api/') {
|
||||
const metricReader = new PeriodicExportingMetricReader({
|
||||
exporter: new OTLPMetricExporter({
|
||||
url: 'http://5-180-182-151.cloud-xip.com:4318/v1/metrics',
|
||||
// headers: {
|
||||
// 'Authorization': 'Basic ' + btoa('tabteste@006:tabteste@006'),
|
||||
// }
|
||||
}),
|
||||
exportIntervalMillis: 3000,
|
||||
});
|
||||
|
||||
meterProvider.addMetricReader(metricReader);
|
||||
}
|
||||
export const meter = meterProvider.getMeter('example-exporter-collector');
|
||||
export const RequestCounter = meter.createCounter('post_requests', {
|
||||
description: 'Example of a Counter',
|
||||
});
|
||||
|
||||
export const UseCaseCounter = meter.createCounter('use_case', {
|
||||
description: 'use case counter',
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
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.cloud-xip.com:85/zipkin-endpoint/api/v2/spans',
|
||||
serviceName: serviceName,
|
||||
getExportRequestHeaders: () => {
|
||||
return {
|
||||
'Authorization': 'Basic ' + btoa('tabteste@006:tabteste@006'),
|
||||
};
|
||||
}
|
||||
})));
|
||||
|
||||
provider.register();
|
||||
return provider;
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
export const OpentelemetryChatProvider = createProvider('FO-chat-service');
|
||||
export const OpentelemetryAgendaProvider = createProvider('FO-agenda-service');
|
||||
export const OpentelemetryNotificationProvider = createProvider('FO-notification');
|
||||
export const OpentelemetryInterceptorProvider = createProvider('FO-interceptor');
|
||||
export const OpentelemetryPublicationProvider = createProvider('FO-publication-service');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
|
||||
import { Tracer, Span } from '@opentelemetry/sdk-trace-base';
|
||||
import { OpentelemetryAgendaProvider, OpentelemetryInterceptorProvider, OpentelemetryNotificationProvider } from './opentelemetry';
|
||||
import { Device, DeviceInfo } from '@capacitor/device';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { UseCaseCounter } from './matrix';
|
||||
// import { context, propagation } from '@opentelemetry/api';
|
||||
|
||||
const tracerInstance = OpentelemetryAgendaProvider.getTracer('example-tracer-hole', '111', {})
|
||||
const tracerNotificationInstance = OpentelemetryNotificationProvider.getTracer('example-tracer-hole', '111', {})
|
||||
// const logger: ILoggerAdapter = new ColoredLoggerService()
|
||||
|
||||
let device: DeviceInfo;
|
||||
|
||||
Device.getInfo().then(e => {
|
||||
device = e
|
||||
});
|
||||
|
||||
|
||||
const createTracingInstance = ({bugPrint, name, module, autoFinish}): TracingType => {
|
||||
const requestId = uuidv4()
|
||||
|
||||
let _tracerInstance:Tracer
|
||||
|
||||
if(module == 'notification') {
|
||||
_tracerInstance = tracerNotificationInstance
|
||||
} else {
|
||||
_tracerInstance = tracerInstance
|
||||
}
|
||||
|
||||
const span = _tracerInstance.startSpan(name);
|
||||
let hasBug:Boolean
|
||||
|
||||
const data = {
|
||||
event: {},
|
||||
tags: {}
|
||||
}
|
||||
|
||||
return {
|
||||
span: span as any,
|
||||
tracer: tracerInstance,
|
||||
tracerId: requestId,
|
||||
attributes: SemanticAttributes,
|
||||
setStatus: (status: any) => {
|
||||
span.setStatus(status);
|
||||
},
|
||||
addEvent: (context: string, message?: any, obj?: any) => {
|
||||
|
||||
data.event[context] = message;
|
||||
|
||||
const value = [JSON.stringify(message)] as any
|
||||
span.addEvent(context, value);
|
||||
},
|
||||
LocalLogEvent:(context: string, message: any, obj: any) => {
|
||||
data.tags[context] = message;
|
||||
},
|
||||
setAttribute: (key: string, value: string) => {
|
||||
data.tags[key] = value;
|
||||
span.setAttribute(key, value);
|
||||
},
|
||||
getAttribute: (key: string) => {
|
||||
return data.tags[key]
|
||||
},
|
||||
finish: () => {
|
||||
if(environment.apiURL != 'https://gdqas-api.oapr.gov.ao/api/') {
|
||||
span.end();
|
||||
UseCaseCounter.add(1, {user: SessionStore?.user?.FullName, outcome:data.tags['outcome'], usecase: name})
|
||||
}
|
||||
|
||||
if(bugPrint && data.tags['outcome'] == 'failed') {
|
||||
console.error(name, data)
|
||||
}
|
||||
},
|
||||
bugFlag:() => {
|
||||
hasBug = true
|
||||
},
|
||||
createSpan: (name, parent?: any) => {
|
||||
return tracerInstance.startSpan(name, { root: false }, parent) as Span;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function XTracerAsync({ name, bugPrint, module = null, autoFinish = true, daley =0 }) {
|
||||
return (
|
||||
target: unknown,
|
||||
propertyKey: string,
|
||||
descriptor: PropertyDescriptor,
|
||||
) => {
|
||||
const originalMethod = descriptor.value;
|
||||
descriptor.value = async function (...args: unknown[]) {
|
||||
|
||||
|
||||
const tracing = createTracingInstance({bugPrint, name, module, autoFinish})
|
||||
|
||||
tracing.setAttribute('User', SessionStore?.user?.FullName);
|
||||
tracing.setAttribute('current.page', window.location.pathname);
|
||||
tracing.setAttribute('device.name', device?.name || device?.model)
|
||||
tracing.setAttribute('commit.date', environment.version.lastCommitTime)
|
||||
tracing.setAttribute('commit.branch', environment.version.branch)
|
||||
|
||||
|
||||
args.push(tracing)
|
||||
|
||||
try {
|
||||
const result = await originalMethod.apply(this, args);
|
||||
|
||||
if(autoFinish ) {
|
||||
setTimeout(tracing.finish , daley)
|
||||
}
|
||||
|
||||
return result
|
||||
} catch (e) {
|
||||
tracing.setAttribute('catch', 'true')
|
||||
|
||||
if(autoFinish) {
|
||||
setTimeout(tracing.finish , daley)
|
||||
}
|
||||
console.error(e);
|
||||
return false
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export function XTracer({ name, bugPrint, module, autoFinish = true, daley =0 }) {
|
||||
return (
|
||||
target: unknown,
|
||||
propertyKey: string,
|
||||
descriptor: PropertyDescriptor,
|
||||
) => {
|
||||
const originalMethod = descriptor.value;
|
||||
descriptor.value = function (...args: unknown[]) {
|
||||
|
||||
const tracing = createTracingInstance({bugPrint, name, module, autoFinish})
|
||||
|
||||
tracing.setAttribute('User', SessionStore?.user?.FullName);
|
||||
tracing.setAttribute('current.page', window.location.pathname);
|
||||
tracing.setAttribute('device.name', device?.name || device?.model)
|
||||
tracing.setAttribute('commit.date', environment.version.lastCommitTime)
|
||||
tracing.setAttribute('commit.branch', environment.version.branch)
|
||||
|
||||
args.push(tracing)
|
||||
|
||||
try {
|
||||
const result = originalMethod.apply(this, args);
|
||||
|
||||
|
||||
if(autoFinish) {
|
||||
setTimeout(tracing.finish , daley)
|
||||
}
|
||||
return result
|
||||
} catch (e) {
|
||||
|
||||
tracing.setAttribute('catch', 'true')
|
||||
|
||||
if(autoFinish) {
|
||||
setTimeout(tracing.finish , daley)
|
||||
}
|
||||
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;
|
||||
getAttribute: (key: string) => string;
|
||||
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