-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore: squash commit * fix: tweak some mentioned issues --------- Co-authored-by: incubator4 <[email protected]>
- Loading branch information
1 parent
a761fd0
commit 68c7430
Showing
11 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { MiddlewareHandler } from 'hono'; | ||
import { getPath } from '@/utils/helpers'; | ||
import { config } from '@/config'; | ||
import { tracer } from '@/utils/otel'; | ||
|
||
const middleware: MiddlewareHandler = async (ctx, next) => { | ||
if (config.debugInfo) { | ||
// Only enable tracing in debug mode | ||
const { method, raw } = ctx.req; | ||
const path = getPath(raw); | ||
|
||
const span = tracer.startSpan(`${method} ${path}`, { | ||
kind: 1, // server | ||
attributes: {}, | ||
}); | ||
span.addEvent('invoking handleRequest'); | ||
await next(); | ||
span.end(); | ||
} else { | ||
// Skip | ||
await next(); | ||
} | ||
}; | ||
|
||
export default middleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Handler } from 'hono'; | ||
import { getContext } from '@/utils/otel'; | ||
|
||
const handler: Handler = (ctx) => | ||
getContext() | ||
.then((val) => ctx.text(val)) | ||
.catch((error) => { | ||
ctx.status(500); | ||
ctx.json({ error }); | ||
}); | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './metric'; | ||
export * from './trace'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { PrometheusExporter, PrometheusSerializer } from '@opentelemetry/exporter-prometheus'; | ||
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||
import { MeterProvider } from '@opentelemetry/sdk-metrics'; | ||
import { Attributes } from '@opentelemetry/api'; | ||
|
||
interface IMetricAttributes extends Attributes { | ||
method: string; | ||
path: string; | ||
status: number; | ||
} | ||
|
||
interface IHistogramAttributes extends IMetricAttributes { | ||
unit: string; | ||
} | ||
|
||
const METRIC_PREFIX = 'rsshub'; | ||
|
||
const exporter = new PrometheusExporter({}); | ||
|
||
const provider = new MeterProvider({ | ||
resource: new Resource({ | ||
[SEMRESATTRS_SERVICE_NAME]: 'rsshub', | ||
}), | ||
readers: [exporter], | ||
}); | ||
|
||
const serializer = new PrometheusSerializer(); | ||
|
||
const meter = provider.getMeter('rsshub'); | ||
|
||
const requestTotal = meter.createCounter<IMetricAttributes>(`${METRIC_PREFIX}_request_total`); | ||
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], | ||
}, | ||
}); | ||
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], | ||
}, | ||
}); | ||
|
||
export const requestMetric = { | ||
success: (value: number, attributes: IMetricAttributes) => { | ||
requestTotal.add(1, attributes); | ||
request_duration_milliseconds_bucket.record(value, { unit: 'millisecond', ...attributes }); | ||
requestDurationSecondsBucket.record(value / 1000, { unit: 'second', ...attributes }); | ||
}, | ||
error: (attributes: IMetricAttributes) => { | ||
requestErrorTotal.add(1, attributes); | ||
}, | ||
}; | ||
|
||
export const getContext = () => | ||
new Promise<string>((resolve, reject) => { | ||
exporter | ||
.collect() | ||
.then((value) => { | ||
resolve(serializer.serialize(value.resourceMetrics)); | ||
}) | ||
.finally(() => { | ||
reject(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { BasicTracerProvider, BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'; | ||
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
|
||
const provider = new BasicTracerProvider({ | ||
resource: new Resource({ | ||
[SEMRESATTRS_SERVICE_NAME]: 'rsshub', | ||
}), | ||
}); | ||
|
||
const exporter = new OTLPTraceExporter({ | ||
// optional OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4318 | ||
}); | ||
|
||
provider.addSpanProcessor( | ||
new BatchSpanProcessor(exporter, { | ||
// The maximum queue size. After the size is reached spans are dropped. | ||
maxQueueSize: 4096, | ||
// The interval between two consecutive exports | ||
scheduledDelayMillis: 30000, | ||
}) | ||
); | ||
|
||
provider.register(); | ||
|
||
export const tracer = provider.getTracer('rsshub'); | ||
export const mainSpan = tracer.startSpan('main'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.