-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[chore] service/telemetry: encapsulate SDK creation #13606
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
mx-psi
merged 1 commit into
open-telemetry:main
from
axw:otelconftelemetry-createtelemetry
Aug 27, 2025
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -13,9 +13,7 @@ import ( | |
|
|
||
| config "go.opentelemetry.io/contrib/otelconf/v0.3.0" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/metric" | ||
| noopmetric "go.opentelemetry.io/otel/metric/noop" | ||
| sdkresource "go.opentelemetry.io/otel/sdk/resource" | ||
| nooptrace "go.opentelemetry.io/otel/trace/noop" | ||
| "go.uber.org/multierr" | ||
| "go.uber.org/zap" | ||
|
|
@@ -37,7 +35,6 @@ import ( | |
| "go.opentelemetry.io/collector/service/internal/graph" | ||
| "go.opentelemetry.io/collector/service/internal/moduleinfo" | ||
| "go.opentelemetry.io/collector/service/internal/proctelemetry" | ||
| "go.opentelemetry.io/collector/service/internal/resource" | ||
| "go.opentelemetry.io/collector/service/internal/status" | ||
| "go.opentelemetry.io/collector/service/telemetry" | ||
| "go.opentelemetry.io/collector/service/telemetry/otelconftelemetry" | ||
|
|
@@ -101,11 +98,11 @@ type Settings struct { | |
|
|
||
| // Service represents the implementation of a component.Host. | ||
| type Service struct { | ||
| buildInfo component.BuildInfo | ||
| telemetrySettings component.TelemetrySettings | ||
| host *graph.Host | ||
| collectorConf *confmap.Conf | ||
| sdk *config.SDK | ||
| buildInfo component.BuildInfo | ||
| telemetrySettings component.TelemetrySettings | ||
| host *graph.Host | ||
| collectorConf *confmap.Conf | ||
| telemetryProviders telemetry.Providers | ||
| } | ||
|
|
||
| // New creates a new Service, its telemetry, and Components. | ||
|
|
@@ -126,39 +123,31 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { | |
| collectorConf: set.CollectorConf, | ||
| } | ||
|
|
||
| // Fetch data for internal telemetry like instance id and sdk version to provide for internal telemetry. | ||
| res := resource.New(set.BuildInfo, cfg.Telemetry.Resource) | ||
| pcommonRes := pdataFromSdk(res) | ||
|
|
||
| mpConfig := &cfg.Telemetry.Metrics.MeterProvider | ||
| if mpConfig.Views == nil { | ||
| mpConfig.Views = configureViews(cfg.Telemetry.Metrics.Level) | ||
| } | ||
|
|
||
| sdk, err := otelconftelemetry.NewSDK(ctx, &cfg.Telemetry, res) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create SDK: %w", err) | ||
| } | ||
| srv.sdk = sdk | ||
| defer func() { | ||
| if err != nil { | ||
| err = multierr.Append(err, sdk.Shutdown(ctx)) | ||
| } | ||
| }() | ||
|
|
||
| telFactory := otelconftelemetry.NewFactory(sdk, res) | ||
| telFactory := otelconftelemetry.NewFactory() | ||
| telset := telemetry.Settings{ | ||
| BuildInfo: set.BuildInfo, | ||
| ZapOptions: set.LoggingOptions, | ||
| } | ||
|
|
||
| logger, loggerProvider, err := telFactory.CreateLogger(ctx, telset, &cfg.Telemetry) | ||
| telProviders, err := telFactory.CreateProviders(ctx, telset, &cfg.Telemetry) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create logger: %w", err) | ||
| return nil, fmt.Errorf("failed to create telemetry providers: %w", err) | ||
| } | ||
| srv.telemetryProviders = telProviders | ||
| defer func() { | ||
| if err != nil { | ||
| err = multierr.Append(err, telProviders.Shutdown(ctx)) | ||
| } | ||
| }() | ||
|
|
||
| // Use initialized logger to handle any subsequent errors | ||
| // https://github.com/open-telemetry/opentelemetry-collector/pull/13081 | ||
| logger := telProviders.Logger() | ||
| defer func() { | ||
| if err != nil { | ||
| logger.Error("error found during service initialization", zap.Error(err)) | ||
|
|
@@ -168,6 +157,7 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { | |
| // Wrap the zap.Logger with componentattribute so scope attributes | ||
| // can be added and removed dynamically, and tee logs to the | ||
| // LoggerProvider. | ||
| loggerProvider := telProviders.LoggerProvider() | ||
| logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { | ||
| core = componentattribute.NewConsoleCoreWithAttributes(core, attribute.NewSet()) | ||
| core = componentattribute.NewOTelTeeCoreWithAttributes( | ||
|
|
@@ -179,23 +169,11 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { | |
| return core | ||
| })) | ||
|
|
||
| tracerProvider, err := telFactory.CreateTracerProvider(ctx, telset, &cfg.Telemetry) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create tracer provider: %w", err) | ||
| } | ||
|
|
||
| logger.Info("Setting up own telemetry...") | ||
|
|
||
| mp, err := telFactory.CreateMeterProvider(ctx, telset, &cfg.Telemetry) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create meter provider: %w", err) | ||
| } | ||
| srv.telemetrySettings = component.TelemetrySettings{ | ||
| Logger: logger, | ||
| MeterProvider: mp, | ||
| TracerProvider: tracerProvider, | ||
| // Construct telemetry attributes from build info and config's resource attributes. | ||
| Resource: pcommonRes, | ||
| MeterProvider: telProviders.MeterProvider(), | ||
| TracerProvider: telProviders.TracerProvider(), | ||
| Resource: telProviders.Resource(), | ||
| } | ||
| srv.host.Reporter = status.NewReporter(srv.host.NotifyComponentStatusChange, func(err error) { | ||
| if errors.Is(err, status.ErrStatusNotReady) { | ||
|
|
@@ -220,25 +198,9 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { | |
| return nil, fmt.Errorf("failed to register process metrics: %w", err) | ||
| } | ||
| } | ||
|
|
||
| logsAboutMeterProvider(logger, cfg.Telemetry.Metrics, mp) | ||
|
|
||
| return srv, nil | ||
| } | ||
|
|
||
| func logsAboutMeterProvider(logger *zap.Logger, cfg otelconftelemetry.MetricsConfig, mp metric.MeterProvider) { | ||
| if cfg.Level == configtelemetry.LevelNone || len(cfg.Readers) == 0 { | ||
| logger.Info("Skipped telemetry setup.") | ||
| return | ||
| } | ||
|
|
||
| if lmp, ok := mp.(interface { | ||
| LogAboutServers(logger *zap.Logger, cfg otelconftelemetry.MetricsConfig) | ||
| }); ok { | ||
| lmp.LogAboutServers(logger, cfg) | ||
| } | ||
|
Comment on lines
-235
to
-239
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. Dead code, nothing implements this interface. |
||
| } | ||
|
|
||
| // Start starts the extensions and pipelines. If Start fails Shutdown should be called to ensure a clean state. | ||
| // Start does the following steps in order: | ||
| // 1. Start all extensions. | ||
|
|
@@ -299,8 +261,8 @@ func (srv *Service) Shutdown(ctx context.Context) error { | |
|
|
||
| srv.telemetrySettings.Logger.Info("Shutdown complete.") | ||
|
|
||
| if err := srv.sdk.Shutdown(ctx); err != nil { | ||
| errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry: %w", err)) | ||
| if err := srv.telemetryProviders.Shutdown(ctx); err != nil { | ||
| errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry providers: %w", err)) | ||
| } | ||
|
|
||
| return errs | ||
|
|
@@ -344,17 +306,6 @@ func (srv *Service) Logger() *zap.Logger { | |
| return srv.telemetrySettings.Logger | ||
| } | ||
|
|
||
| func pdataFromSdk(res *sdkresource.Resource) pcommon.Resource { | ||
| // pcommon.NewResource is the best way to generate a new resource currently and is safe to use outside of tests. | ||
| // Because the resource is signal agnostic, and we need a net new resource, not an existing one, this is the only | ||
| // method of creating it without exposing internal packages. | ||
| pcommonRes := pcommon.NewResource() | ||
| for _, keyValue := range res.Attributes() { | ||
| pcommonRes.Attributes().PutStr(string(keyValue.Key), keyValue.Value.AsString()) | ||
| } | ||
| return pcommonRes | ||
| } | ||
|
|
||
| func dropViewOption(selector *config.ViewSelector) config.View { | ||
| return config.View{ | ||
| Selector: selector, | ||
|
|
||
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
Oops, something went wrong.
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.
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.
This logging is now done inside otelconftelemetry.