Skip to content

Commit

Permalink
chore: otel fix and enhancement (#16754)
Browse files Browse the repository at this point in the history
* feat(otel): use routePath insteadof path

* feat(otel): add otel bucket boundary env

* chore: move otel to debug block with alphabetical sort
  • Loading branch information
incubator4 committed Sep 15, 2024
1 parent 0ae1f0c commit be3ba61
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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;
5 changes: 3 additions & 2 deletions lib/utils/otel/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
},
});

Expand Down

0 comments on commit be3ba61

Please sign in to comment.