-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: load opentelemetry modules on demand (#619)
- Loading branch information
1 parent
beeeaa6
commit ab0145c
Showing
16 changed files
with
130 additions
and
109 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { setupOpentelemetry } from '@u22n/otel'; | ||
import { opentelemetryEnabled } from '@u22n/otel'; | ||
import { name, version } from './package.json'; | ||
|
||
setupOpentelemetry({ name, version }); | ||
if (opentelemetryEnabled) { | ||
const { setupOpentelemetry } = await import('@u22n/otel/setup'); | ||
setupOpentelemetry({ name, version }); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { setupOpentelemetry } from '@u22n/otel'; | ||
import { opentelemetryEnabled } from '@u22n/otel'; | ||
import { name, version } from './package.json'; | ||
|
||
setupOpentelemetry({ name, version }); | ||
if (opentelemetryEnabled) { | ||
const { setupOpentelemetry } = await import('@u22n/otel/setup'); | ||
setupOpentelemetry({ name, version }); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { setupOpentelemetry } from '@u22n/otel'; | ||
import { opentelemetryEnabled } from '@u22n/otel'; | ||
import { name, version } from './package.json'; | ||
|
||
setupOpentelemetry({ name, version }); | ||
if (opentelemetryEnabled) { | ||
const { setupOpentelemetry } = await import('@u22n/otel/setup'); | ||
setupOpentelemetry({ name, version }); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
export async function register() { | ||
if (process.env.NEXT_RUNTIME === 'nodejs') { | ||
const { name, version } = await import('../package.json'); | ||
const { setupOpentelemetry } = await import('@u22n/otel'); | ||
setupOpentelemetry({ name, version }); | ||
const { opentelemetryEnabled } = await import('@u22n/otel'); | ||
|
||
if (opentelemetryEnabled) { | ||
const { name, version } = await import('../package.json'); | ||
const { setupOpentelemetry } = await import('@u22n/otel/setup'); | ||
setupOpentelemetry({ name, version }); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { setupOpentelemetry } from '@u22n/otel'; | ||
import { opentelemetryEnabled } from '@u22n/otel'; | ||
import { name, version } from './package.json'; | ||
|
||
setupOpentelemetry({ name, version }); | ||
if (opentelemetryEnabled) { | ||
const { setupOpentelemetry } = await import('@u22n/otel/setup'); | ||
setupOpentelemetry({ name, version }); | ||
} |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export { trace } from '@opentelemetry/api'; | ||
export { flatten, unflatten } from 'flat'; |
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 |
---|---|---|
@@ -1,60 +1,2 @@ | ||
import { | ||
NodeTracerProvider, | ||
BatchSpanProcessor | ||
} from '@opentelemetry/sdk-trace-node'; | ||
import { | ||
BatchLogRecordProcessor, | ||
LoggerProvider | ||
} from '@opentelemetry/sdk-logs'; | ||
import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston'; | ||
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { logs } from '@opentelemetry/api-logs'; | ||
import { env } from './env'; | ||
|
||
export const setupOpentelemetry = ({ | ||
name, | ||
version | ||
}: { | ||
name: string; | ||
version: string; | ||
}) => { | ||
if (!env.OTEL_ENABLED) return; | ||
|
||
const resource = new Resource({ | ||
'service.name': name, | ||
'service.version': version | ||
}); | ||
|
||
const traceProvider = new NodeTracerProvider({ resource }); | ||
traceProvider.addSpanProcessor( | ||
new BatchSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: env.OTEL_EXPORTER_TRACES_ENDPOINT | ||
}) | ||
) | ||
); | ||
traceProvider.register(); | ||
|
||
const loggerProvider = new LoggerProvider({ resource }); | ||
loggerProvider.addLogRecordProcessor( | ||
new BatchLogRecordProcessor( | ||
new OTLPLogExporter({ | ||
url: env.OTEL_EXPORTER_LOGS_ENDPOINT | ||
}) | ||
) | ||
); | ||
logs.setGlobalLoggerProvider(loggerProvider); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new UndiciInstrumentation(), | ||
new HttpInstrumentation(), | ||
new WinstonInstrumentation() | ||
] | ||
}); | ||
}; | ||
export const opentelemetryEnabled = env.OTEL_ENABLED; |
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,58 @@ | ||
import { | ||
BatchSpanProcessor, | ||
NodeTracerProvider | ||
} from '@opentelemetry/sdk-trace-node'; | ||
import { | ||
BatchLogRecordProcessor, | ||
LoggerProvider | ||
} from '@opentelemetry/sdk-logs'; | ||
import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston'; | ||
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici'; | ||
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; | ||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { logs } from '@opentelemetry/api-logs'; | ||
import { env } from './env'; | ||
|
||
export const setupOpentelemetry = ({ | ||
name, | ||
version | ||
}: { | ||
name: string; | ||
version: string; | ||
}) => { | ||
const resource = new Resource({ | ||
'service.name': name, | ||
'service.version': version | ||
}); | ||
|
||
const traceProvider = new NodeTracerProvider({ resource }); | ||
traceProvider.addSpanProcessor( | ||
new BatchSpanProcessor( | ||
new OTLPTraceExporter({ | ||
url: env.OTEL_EXPORTER_TRACES_ENDPOINT | ||
}) | ||
) | ||
); | ||
traceProvider.register(); | ||
|
||
const loggerProvider = new LoggerProvider({ resource }); | ||
loggerProvider.addLogRecordProcessor( | ||
new BatchLogRecordProcessor( | ||
new OTLPLogExporter({ | ||
url: env.OTEL_EXPORTER_LOGS_ENDPOINT | ||
}) | ||
) | ||
); | ||
logs.setGlobalLoggerProvider(loggerProvider); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new UndiciInstrumentation(), | ||
new HttpInstrumentation(), | ||
new WinstonInstrumentation() | ||
] | ||
}); | ||
}; |