-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: opentelementry support #14668
Closed
Closed
feat: opentelementry support #14668
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,18 @@ | ||
import { MiddlewareHandler } from 'hono'; | ||
import { getPath } from '@/utils/helpers'; | ||
import { tracer } from '@/utils/otel'; | ||
|
||
const middleware: MiddlewareHandler = async (ctx, next) => { | ||
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(); | ||
}; | ||
|
||
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
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 request_total = meter.createCounter<IMetricAttributes>(`${metric_prefix}_request_total`); | ||
const request_error_total = meter.createCounter<IMetricAttributes>(`${metric_prefix}_request_error_total`); | ||
const request_duration_seconds_bucket = 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) => { | ||
request_total.add(1, attributes); | ||
request_duration_milliseconds_bucket.record(value, { unit: 'millisecond', ...attributes }); | ||
request_duration_seconds_bucket.record(value / 1000, { unit: 'second', ...attributes }); | ||
}, | ||
error: (attributes: IMetricAttributes) => { | ||
request_error_total.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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -51,6 +51,13 @@ | |||||||||||||||||||||||||||||
"dependencies": { | ||||||||||||||||||||||||||||||
"@hono/node-server": "1.8.2", | ||||||||||||||||||||||||||||||
"@notionhq/client": "2.2.14", | ||||||||||||||||||||||||||||||
"@opentelemetry/api": "^1.8.0", | ||||||||||||||||||||||||||||||
"@opentelemetry/exporter-prometheus": "^0.49.1", | ||||||||||||||||||||||||||||||
"@opentelemetry/exporter-trace-otlp-http": "^0.49.1", | ||||||||||||||||||||||||||||||
"@opentelemetry/resources": "^1.22.0", | ||||||||||||||||||||||||||||||
"@opentelemetry/sdk-metrics": "^1.22.0", | ||||||||||||||||||||||||||||||
"@opentelemetry/sdk-trace-base": "^1.22.0", | ||||||||||||||||||||||||||||||
"@opentelemetry/semantic-conventions": "^1.22.0", | ||||||||||||||||||||||||||||||
Comment on lines
+54
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please pin the dependency version. |
||||||||||||||||||||||||||||||
"@postlight/parser": "2.2.3", | ||||||||||||||||||||||||||||||
"@sentry/node": "7.105.0", | ||||||||||||||||||||||||||||||
"@tonyrl/rand-user-agent": "2.0.53", | ||||||||||||||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
DEBUG_INFO
is set tofalse
or any strings other thantrue
, the debug info from RSSHub will be hidden and only available if the same string is provided as query stringdebug
./metrics
does not respect this behaviour currently.The same also apply to
ACCESS_KEY
.RSSHub/lib/middleware/access-control.ts
Line 15 in 15a51bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basicly, metrics should be export with a external local port. In that case rsshub may handle another port with
127.0.0.1:9090
for example.I'm not sure if an external port should be created, but it might be a better solution than modify if else for access-control.
And also should be with env to control local server enable or not.