-
Notifications
You must be signed in to change notification settings - Fork 953
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
42dfbf4
1946cd6
3aee374
0b0f6f5
31ebe49
077e462
4643c5f
3e1103c
4e8006c
b065522
11b32c0
dd1a34c
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
Contributor
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
Show resolved
Hide resolved
|
||||||
| 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.`); | ||||||
|
||||||
| return new OTLPProtoLogExporter(); | ||||||
| } | ||||||
|
|
||||||
| function getLogRecordProcessorsFromConfiguration( | ||||||
maryliag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| 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; | ||||||
| } | ||||||
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.
Note for reviewers: this is the only thing that I changed compared to the "original".
Before we had only env, process and host, but if the value was all, or there was no value on env variable it was setting to all, and that included os and service instance id. We do say on our docs that the default is to have all detectors on.
We didn't add os and service instance id because we wanted to have them baking for awhile before adding, which I think we can do now.
But let me know if you don't think is a good idea to turn any of them by default