-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(opentelemetry-sdk-node): set resources and log provider for experimental start #6152
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
Changes from 2 commits
42dfbf4
1946cd6
3aee374
0b0f6f5
31ebe49
077e462
4643c5f
3e1103c
4e8006c
b065522
11b32c0
dd1a34c
b7d2b6c
5a8da6d
1466b03
651686f
c3071c1
435b72c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,16 +15,46 @@ | |||||
| */ | ||||||
| import { | ||||||
| ConfigFactory, | ||||||
| ConfigurationModel, | ||||||
| createConfigFactory, | ||||||
| LogRecordExporterModel, | ||||||
| } from '@opentelemetry/configuration'; | ||||||
| import { diag, DiagConsoleLogger } from '@opentelemetry/api'; | ||||||
| import { | ||||||
| getPropagatorFromConfiguration, | ||||||
| getResourceDetectorsFromConfiguration, | ||||||
| setupDefaultContextManager, | ||||||
| setupPropagator, | ||||||
| } from './utils'; | ||||||
| import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||||||
| import type { SDKOptions } from './types'; | ||||||
| import { | ||||||
| BatchLogRecordProcessor, | ||||||
| ConsoleLogRecordExporter, | ||||||
| LoggerProvider, | ||||||
| LogRecordExporter, | ||||||
| LogRecordProcessor, | ||||||
| SimpleLogRecordProcessor, | ||||||
| } from '@opentelemetry/sdk-logs'; | ||||||
| import { OTLPLogExporter as OTLPHttpLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; | ||||||
| import { OTLPLogExporter as OTLPGrpcLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc'; | ||||||
| import { OTLPLogExporter as OTLPProtoLogExporter } from '@opentelemetry/exporter-logs-otlp-proto'; | ||||||
| import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base'; | ||||||
| import { logs } from '@opentelemetry/api-logs'; | ||||||
| import { | ||||||
| defaultResource, | ||||||
| detectResources, | ||||||
| envDetector, | ||||||
| hostDetector, | ||||||
| osDetector, | ||||||
| processDetector, | ||||||
| Resource, | ||||||
| ResourceDetectionConfig, | ||||||
| ResourceDetector, | ||||||
| resourceFromAttributes, | ||||||
| serviceInstanceIdDetector, | ||||||
| } from '@opentelemetry/resources'; | ||||||
| import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||||||
|
|
||||||
| /** | ||||||
| * @experimental Function to start the OpenTelemetry Node SDK | ||||||
|
|
@@ -54,13 +84,134 @@ export function startNodeSDK(sdkOptions: SDKOptions): { | |||||
| : (sdkOptions?.textMapPropagator ?? | ||||||
| getPropagatorFromConfiguration(config)) | ||||||
| ); | ||||||
| const resource = setupResource(config, sdkOptions); | ||||||
| const loggerProvider = setupLoggerProvider(config, sdkOptions, resource); | ||||||
|
|
||||||
| const shutdownFn = async () => { | ||||||
| const promises: Promise<unknown>[] = []; | ||||||
| if (loggerProvider) { | ||||||
| promises.push(loggerProvider.shutdown()); | ||||||
| } | ||||||
| await Promise.all(promises); | ||||||
| }; | ||||||
| return { shutdown: shutdownFn }; | ||||||
| } | ||||||
| const NOOP_SDK = { | ||||||
| shutdown: async () => {}, | ||||||
| }; | ||||||
|
|
||||||
| function setupResource( | ||||||
| config: ConfigurationModel, | ||||||
| sdkOptions: SDKOptions | ||||||
| ): Resource { | ||||||
| let resource: Resource = sdkOptions.resource ?? defaultResource(); | ||||||
| const autoDetectResources = sdkOptions.autoDetectResources ?? true; | ||||||
| let resourceDetectors: ResourceDetector[]; | ||||||
|
|
||||||
| if (!autoDetectResources) { | ||||||
| resourceDetectors = []; | ||||||
| } else if (sdkOptions.resourceDetectors != null) { | ||||||
| resourceDetectors = sdkOptions.resourceDetectors; | ||||||
| } else if (config.node_resource_detectors) { | ||||||
| resourceDetectors = getResourceDetectorsFromConfiguration(config); | ||||||
| } else { | ||||||
| resourceDetectors = [ | ||||||
| envDetector, | ||||||
| processDetector, | ||||||
| hostDetector, | ||||||
| osDetector, | ||||||
| serviceInstanceIdDetector, | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| if (autoDetectResources) { | ||||||
| const internalConfig: ResourceDetectionConfig = { | ||||||
| detectors: resourceDetectors, | ||||||
| }; | ||||||
|
|
||||||
| resource = resource.merge(detectResources(internalConfig)); | ||||||
| } | ||||||
|
|
||||||
| resource = | ||||||
| sdkOptions.serviceName === undefined | ||||||
| ? resource | ||||||
| : resource.merge( | ||||||
| resourceFromAttributes({ | ||||||
| [ATTR_SERVICE_NAME]: sdkOptions.serviceName, | ||||||
| }) | ||||||
| ); | ||||||
|
|
||||||
| return resource; | ||||||
| } | ||||||
|
|
||||||
| function setupLoggerProvider( | ||||||
| config: ConfigurationModel, | ||||||
| sdkOptions: SDKOptions, | ||||||
| resource: Resource | undefined | ||||||
| ): LoggerProvider | undefined { | ||||||
|
Member
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
Is there a scenario where
Member
Author
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. if you don't have any log provider on env variables or config file, there is no logger provider to be setup, so it will return
|
||||||
| const logProcessors = | ||||||
| sdkOptions.logRecordProcessors ?? | ||||||
| getLogRecordProcessorsFromConfiguration(config); | ||||||
|
|
||||||
| if (logProcessors) { | ||||||
| const loggerProvider = new LoggerProvider({ | ||||||
| resource: resource, | ||||||
| processors: logProcessors, | ||||||
| }); | ||||||
|
|
||||||
| logs.setGlobalLoggerProvider(loggerProvider); | ||||||
| return loggerProvider; | ||||||
| } | ||||||
| return undefined; | ||||||
| } | ||||||
|
|
||||||
| function getExporterType(exporter: LogRecordExporterModel): LogRecordExporter { | ||||||
| if (exporter.otlp_http) { | ||||||
| if (exporter.otlp_http.encoding === 'json') { | ||||||
| return new OTLPHttpLogExporter({ | ||||||
| compression: | ||||||
| exporter.otlp_http.compression === 'gzip' | ||||||
| ? CompressionAlgorithm.GZIP | ||||||
| : CompressionAlgorithm.NONE, | ||||||
| }); | ||||||
| } | ||||||
| if (exporter.otlp_http.encoding === 'protobuf') { | ||||||
| return new OTLPProtoLogExporter(); | ||||||
| } | ||||||
| diag.warn(`Unsupported OTLP logs protocol. Using http/protobuf.`); | ||||||
|
maryliag marked this conversation as resolved.
Outdated
|
||||||
| return new OTLPProtoLogExporter(); | ||||||
| } else if (exporter.otlp_grpc) { | ||||||
| return new OTLPGrpcLogExporter(); | ||||||
| } else if (exporter.console) { | ||||||
| return new ConsoleLogRecordExporter(); | ||||||
| } | ||||||
| diag.warn(`Unsupported Exporter value. Using OTLP http/protobuf.`); | ||||||
|
Member
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. 🤔 If someone uses a config file but neglects to specify a valid exporter (
Member
Author
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. I believe I saw the default should be
Member
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. Sorry on the delay in coming back to this. In re-reviewing the spec for required and null properties, my understanding is that if a field is required it must be a valid entry to be used and otherwise it is invalid.
I'll take a look at the go and java implementations to see how they handle it |
||||||
| return new OTLPProtoLogExporter(); | ||||||
| } | ||||||
|
|
||||||
| function getLogRecordProcessorsFromConfiguration( | ||||||
|
maryliag marked this conversation as resolved.
Outdated
|
||||||
| config: ConfigurationModel | ||||||
| ): LogRecordProcessor[] | undefined { | ||||||
| const logRecordProcessors: LogRecordProcessor[] = []; | ||||||
| config.logger_provider?.processors?.forEach(processor => { | ||||||
| if (processor.batch) { | ||||||
| logRecordProcessors.push( | ||||||
| new BatchLogRecordProcessor(getExporterType(processor.batch.exporter), { | ||||||
| maxQueueSize: processor.batch.max_queue_size, | ||||||
| maxExportBatchSize: processor.batch.max_export_batch_size, | ||||||
| scheduledDelayMillis: processor.batch.schedule_delay, | ||||||
| exportTimeoutMillis: processor.batch.export_timeout, | ||||||
| }) | ||||||
| ); | ||||||
| } | ||||||
| if (processor.simple) { | ||||||
| logRecordProcessors.push( | ||||||
| new SimpleLogRecordProcessor(getExporterType(processor.simple.exporter)) | ||||||
| ); | ||||||
| } | ||||||
| }); | ||||||
| if (logRecordProcessors.length > 0) { | ||||||
| return logRecordProcessors; | ||||||
| } | ||||||
| return undefined; | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.