-
Notifications
You must be signed in to change notification settings - Fork 954
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
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 { | ||||||
|
||||||
| ): LoggerProvider | undefined { | |
| ): LoggerProvider { |
Is there a scenario where setupLoggerProvider would be undefined? setupResource only returns a Resource not 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.
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 undefined
setupResource have a default value for resources, this is why it doesn't have the option to return undefined
maryliag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
🤔 If someone uses a config file but neglects to specify a valid exporter (otlp_http, otlp_grpc, otlp_file/development(soon), console)... should we default an exporter for them or should we consider it a noop because it's invalid? It's not clear to me.
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.
I believe I saw the default should be otlp_http in this case, so I kept at that.
Uh oh!
There was an error while loading. Please reload this page.