-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add OpenTelemetry Prometheus metrics #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f10f81
feat: add telemetry config schema
rsbh 4c35194
feat: add OTel telemetry module with Prometheus exporter
rsbh 1a406d2
feat: add /api/metrics Prometheus endpoint
rsbh 69399e2
feat: add Nitro telemetry plugin for HTTP metrics
rsbh 26b8734
feat: instrument SSR render duration
rsbh 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,23 @@ | ||
| import type { IncomingMessage, ServerResponse } from 'node:http' | ||
| import { defineHandler } from 'nitro' | ||
| import { getExporter } from '../telemetry' | ||
|
|
||
| export default defineHandler(async () => { | ||
| const exporter = getExporter() | ||
| if (!exporter) { | ||
| return new Response('Telemetry not enabled', { status: 404 }) | ||
| } | ||
|
|
||
| const metricsString = await new Promise<string>((resolve) => { | ||
| const mockRes = { | ||
| setHeader: () => mockRes, | ||
| end: (data: string) => resolve(data), | ||
| } as unknown as ServerResponse | ||
|
|
||
| exporter.getMetricsRequestHandler({} as unknown as IncomingMessage, mockRes) | ||
| }) | ||
|
|
||
| return new Response(metricsString, { | ||
| headers: { 'Content-Type': 'text/plain; charset=utf-8' }, | ||
| }) | ||
| }) | ||
This file contains hidden or 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 hidden or 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,21 @@ | ||
| import { definePlugin } from 'nitro' | ||
| import { loadConfig } from '@/lib/config' | ||
| import { initTelemetry, recordRequest } from '../telemetry' | ||
|
|
||
| export default definePlugin((nitroApp) => { | ||
| const config = loadConfig() | ||
| if (!config.telemetry?.enabled) return | ||
|
|
||
| initTelemetry(config) | ||
|
|
||
| nitroApp.hooks.hook('request', (event) => { | ||
| if (event.path === '/api/metrics') return | ||
| event.context._requestStart = performance.now() | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('response', (res, event) => { | ||
| if (!event.context._requestStart) return | ||
| const duration = performance.now() - event.context._requestStart | ||
| recordRequest(event.method, event.path, res.status, duration) | ||
| }) | ||
| }) |
This file contains hidden or 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,49 @@ | ||
| import type { Counter, Histogram } from '@opentelemetry/api' | ||
| import sdkMetrics from '@opentelemetry/sdk-metrics' | ||
| import prometheusExporter from '@opentelemetry/exporter-prometheus' | ||
| import resources from '@opentelemetry/resources' | ||
| import semconv from '@opentelemetry/semantic-conventions' | ||
| import type { ChronicleConfig } from '@/types/config' | ||
|
|
||
| const { MeterProvider } = sdkMetrics | ||
| const { PrometheusExporter } = prometheusExporter | ||
| const { resourceFromAttributes } = resources | ||
| const { ATTR_SERVICE_NAME } = semconv | ||
|
|
||
| let exporter: PrometheusExporter | ||
| let requestCounter: Counter | ||
| let requestDuration: Histogram | ||
| let ssrRenderDuration: Histogram | ||
|
|
||
| export function initTelemetry(config: ChronicleConfig) { | ||
| const resource = resourceFromAttributes({ | ||
| [ATTR_SERVICE_NAME]: config.telemetry?.serviceName ?? 'chronicle', | ||
| }) | ||
|
|
||
| exporter = new PrometheusExporter({ preventServerStart: true }) | ||
| const provider = new MeterProvider({ resource, readers: [exporter] }) | ||
| const meter = provider.getMeter('chronicle') | ||
|
|
||
| requestCounter = meter.createCounter('http_server_request_total', { | ||
| description: 'Total HTTP requests', | ||
| }) | ||
| requestDuration = meter.createHistogram('http_server_request_duration_ms', { | ||
| description: 'HTTP request duration in ms', | ||
| }) | ||
| ssrRenderDuration = meter.createHistogram('http_server_ssr_render_duration_ms', { | ||
| description: 'SSR render duration in ms', | ||
| }) | ||
| } | ||
|
|
||
| export function getExporter() { | ||
| return exporter | ||
| } | ||
|
|
||
| export function recordRequest(method: string, route: string, status: number, durationMs: number) { | ||
| requestCounter?.add(1, { method, route, status }) | ||
| requestDuration?.record(durationMs, { method, route, status }) | ||
| } | ||
|
|
||
| export function recordSSRRender(route: string, status: number, durationMs: number) { | ||
| ssrRenderDuration?.record(durationMs, { route, status }) | ||
| } |
This file contains hidden or 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
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.
Promise may hang indefinitely if the handler fails.
The promise has no rejection path or timeout. If
getMetricsRequestHandlerthrows an error or fails to callend(), this request will hang forever. Consider adding error handling and a timeout.🛡️ Proposed fix with timeout and error handling
📝 Committable suggestion
🤖 Prompt for AI Agents