-
Notifications
You must be signed in to change notification settings - Fork 5
Replace custom Loki HTTP push with native OpenTelemetry OTLP exporter #1876
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
7 commits
Select commit
Hold shift + click to select a range
4526cc9
Initial plan
Copilot 887210e
feat: replace custom Loki push with OTel logging integration
Copilot ac93694
refactor: remove LOKI environment variables
Copilot 75e729f
fix: align logger service name with instrumentation
Copilot 88ea369
refactor: replace @vercel/otel with native OpenTelemetry SDK
Copilot 99a5b2b
refactor: extract shared resource configuration to reduce duplication
Copilot 0e23f57
feat(OTel): ...
simonknittel 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 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 |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ | |
| "Odoo", | ||
| "openai", | ||
| "OPENAI", | ||
| "otlp", | ||
| "polaris", | ||
| "postgres", | ||
| "postgresql", | ||
|
|
||
Large diffs are not rendered by default.
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
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,45 @@ | ||
| import { logs } from "@opentelemetry/api-logs"; | ||
| import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node"; | ||
| import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http"; | ||
| import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; | ||
| import { resourceFromAttributes } from "@opentelemetry/resources"; | ||
| import { | ||
| BatchLogRecordProcessor, | ||
| LoggerProvider, | ||
| } from "@opentelemetry/sdk-logs"; | ||
| import { NodeSDK } from "@opentelemetry/sdk-node"; | ||
| import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; | ||
| import { PrismaInstrumentation } from "@prisma/instrumentation"; | ||
| import { env } from "./env"; | ||
|
|
||
| const resource = resourceFromAttributes({ | ||
| [ATTR_SERVICE_NAME]: "sam", | ||
| }); | ||
|
|
||
| const traceExporter = new OTLPTraceExporter({ | ||
| url: `${env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces`, | ||
| }); | ||
|
|
||
| const sdk = new NodeSDK({ | ||
| resource, | ||
| traceExporter, | ||
| instrumentations: [ | ||
| getNodeAutoInstrumentations(), | ||
| new PrismaInstrumentation(), | ||
| ], | ||
| }); | ||
|
|
||
| sdk.start(); | ||
|
|
||
| const loggerProvider = new LoggerProvider({ | ||
| resource, | ||
| processors: [ | ||
| new BatchLogRecordProcessor( | ||
| new OTLPLogExporter({ | ||
| url: `${env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/logs`, | ||
| }), | ||
| ), | ||
| ], | ||
| }); | ||
|
|
||
| logs.setGlobalLoggerProvider(loggerProvider); | ||
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 |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| import { PrismaInstrumentation } from "@prisma/instrumentation"; | ||
| import { registerOTel } from "@vercel/otel"; | ||
| import { env } from "./env"; | ||
|
|
||
| export const register = () => { | ||
| if (env.ENABLE_INSTRUMENTATION !== "true") return; | ||
| if (!env.OTEL_EXPORTER_OTLP_PROTOCOL || !env.OTEL_EXPORTER_OTLP_ENDPOINT) | ||
| export const register = async () => { | ||
| if ( | ||
| env.ENABLE_INSTRUMENTATION !== "true" || | ||
| !env.OTEL_EXPORTER_OTLP_PROTOCOL || | ||
| !env.OTEL_EXPORTER_OTLP_ENDPOINT | ||
| ) | ||
| return; | ||
| registerOTel({ | ||
| serviceName: "sam", | ||
| instrumentations: [new PrismaInstrumentation()], | ||
| }); | ||
|
|
||
| if (process.env.NEXT_RUNTIME === "nodejs") { | ||
| await import("./instrumentation.node"); | ||
| } | ||
| }; |
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 was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { env } from "@/env"; | ||
| import { logs, SeverityNumber } from "@opentelemetry/api-logs"; | ||
| import { type LogEntry, type LogOutput } from "./types"; | ||
|
|
||
| const getSeverityNumber = (level: LogEntry["level"]): SeverityNumber => { | ||
| switch (level) { | ||
| case "info": | ||
| return SeverityNumber.INFO; | ||
| case "warn": | ||
| return SeverityNumber.WARN; | ||
| case "error": | ||
| return SeverityNumber.ERROR; | ||
| } | ||
| }; | ||
|
|
||
| export const logToOTel: LogOutput = (logEntry) => { | ||
| if ( | ||
| env.ENABLE_INSTRUMENTATION !== "true" || | ||
| !env.OTEL_EXPORTER_OTLP_PROTOCOL || | ||
| !env.OTEL_EXPORTER_OTLP_ENDPOINT | ||
| ) | ||
| return; | ||
|
|
||
| try { | ||
| const loggerProvider = logs.getLoggerProvider(); | ||
| const logger = loggerProvider.getLogger("sam"); | ||
|
|
||
| const { timestamp, level, message, host, commitSha, stack, ...attributes } = | ||
| logEntry; | ||
|
|
||
| logger.emit({ | ||
| severityNumber: getSeverityNumber(level), | ||
| severityText: level, | ||
| body: message, | ||
| timestamp: new Date(timestamp).getTime(), | ||
| attributes: { | ||
| host, | ||
| ...(commitSha && { commitSha }), | ||
| ...(stack && { stack }), | ||
| ...attributes, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.error("Failed to emit log to OTel:", error); | ||
| } | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.