chore: otel fix and enhancement (#16754)
* feat(otel): use routePath insteadof path * feat(otel): add otel bucket boundary env * chore: move otel to debug block with alphabetical sort
This commit is contained in:
@@ -47,6 +47,10 @@ export type Config = {
|
||||
debugInfo: string;
|
||||
loggerLevel: string;
|
||||
noLogfiles?: boolean;
|
||||
otel: {
|
||||
seconds_bucket?: string;
|
||||
milliseconds_bucket?: string;
|
||||
};
|
||||
showLoggerTimestamp?: boolean;
|
||||
sentry: {
|
||||
dsn?: string;
|
||||
@@ -414,6 +418,10 @@ const calculateValue = () => {
|
||||
debugInfo: envs.DEBUG_INFO || 'true',
|
||||
loggerLevel: envs.LOGGER_LEVEL || 'info',
|
||||
noLogfiles: toBoolean(envs.NO_LOGFILES, false),
|
||||
otel: {
|
||||
seconds_bucket: envs.OTEL_SECONDS_BUCKET || '0.01,0.1,1,2,5,15,30,60',
|
||||
milliseconds_bucket: envs.OTEL_MILLISECONDS_BUCKET || '10,20,50,100,250,500,1000,5000,15000',
|
||||
},
|
||||
showLoggerTimestamp: toBoolean(envs.SHOW_LOGGER_TIMESTAMP, false),
|
||||
sentry: {
|
||||
dsn: envs.SENTRY,
|
||||
|
||||
@@ -67,7 +67,7 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
|
||||
const message = `${error.name}: ${errorMessage}`;
|
||||
|
||||
logger.error(`Error in ${requestPath}: ${message}`);
|
||||
requestMetric.error({ path: requestPath, method: ctx.req.method, status: ctx.res.status });
|
||||
requestMetric.error({ path: matchedRoute, method: ctx.req.method, status: ctx.res.status });
|
||||
|
||||
return config.isPackage || ctx.req.query('format') === 'json'
|
||||
? ctx.json({
|
||||
|
||||
@@ -26,7 +26,7 @@ const colorStatus = (status: number) => {
|
||||
};
|
||||
|
||||
const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
const { method, raw } = ctx.req;
|
||||
const { method, raw, routePath } = ctx.req;
|
||||
const path = getPath(raw);
|
||||
|
||||
logger.info(`${LogPrefix.Incoming} ${method} ${path}`);
|
||||
@@ -38,7 +38,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
|
||||
const status = ctx.res.status;
|
||||
|
||||
logger.info(`${LogPrefix.Outgoing} ${method} ${path} ${colorStatus(status)} ${time(start)}`);
|
||||
requestMetric.success(Date.now() - start, { path, method, status });
|
||||
requestMetric.success(Date.now() - start, { path: routePath, method, status });
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
|
||||
@@ -3,6 +3,7 @@ import { PrometheusExporter, PrometheusSerializer } from '@opentelemetry/exporte
|
||||
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
|
||||
import { MeterProvider } from '@opentelemetry/sdk-metrics';
|
||||
import { Attributes } from '@opentelemetry/api';
|
||||
import { config } from '@/config';
|
||||
|
||||
interface IMetricAttributes extends Attributes {
|
||||
method: string;
|
||||
@@ -33,12 +34,12 @@ const requestTotal = meter.createCounter<IMetricAttributes>(`${METRIC_PREFIX}_re
|
||||
const requestErrorTotal = meter.createCounter<IMetricAttributes>(`${METRIC_PREFIX}_request_error_total`);
|
||||
const requestDurationSecondsBucket = meter.createHistogram<IHistogramAttributes>(`${METRIC_PREFIX}_request_duration_seconds_bucket`, {
|
||||
advice: {
|
||||
explicitBucketBoundaries: [0.01, 0.1, 1, 2, 5, 15, 30, 60],
|
||||
explicitBucketBoundaries: config.otel.seconds_bucket?.split(',').map(Number),
|
||||
},
|
||||
});
|
||||
const request_duration_milliseconds_bucket = meter.createHistogram<IHistogramAttributes>(`${METRIC_PREFIX}_request_duration_milliseconds_bucket`, {
|
||||
advice: {
|
||||
explicitBucketBoundaries: [10, 20, 50, 100, 250, 500, 1000, 5000, 15000],
|
||||
explicitBucketBoundaries: config.otel.milliseconds_bucket?.split(',').map(Number),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user