Skip to content

Commit 9b4911b

Browse files
authored
[service]: use configured logger whenever possible (#13081)
#### Description I was using the supervised collector today and ran into an issue where the agent (collector) was crashing on startup and I wasn't seeing the logs exported via the configured logger, but I knew it existed bc I was getting the `Setting up own telemetry...` log. Turns out in `service.New`, once we've created the logger, we aren't using it to log and following errors. Instead, they are being returned by `service.New` and handled by the fallbackLogger. I propose that, since we have a logger, we use it. As a followup it would be nice if any confmap errors could be reported using the instantiated logger, but that would be a bigger refactor. #### Testing Tested locally with the following config: ```yaml receivers: nop: exporters: nop: otlphttp: endpoint: "${MISSING_ENV_VAR}:4318" service: pipelines: traces: receivers: [nop] processors: [] exporters: [otlphttp] telemetry: logs: processors: - batch: exporter: otlp: endpoint: https://api.honeycomb.io:443 headers: - name: x-honeycomb-team value: `[READACTED]` protocol: http/protobuf ``` console output: ``` 2025-05-22T17:25:30.764-0600 info service/service.go:200 Setting up own telemetry... {"resource": {}} 2025-05-22T17:25:30.764-0600 error service/service.go:223 failed to initialize service graph {"resource": {}, "error": "failed to build pipelines: failed to create \"otlphttp\" exporter for data type \"traces\": endpoint must be a valid URL"} go.opentelemetry.io/collector/service.New /Users/tylerhelmuth/projects/opentelemetry-collector/service/service.go:223 go.opentelemetry.io/collector/otelcol.(*Collector).setupConfigurationComponents /Users/tylerhelmuth/projects/opentelemetry-collector/otelcol/collector.go:197 go.opentelemetry.io/collector/otelcol.(*Collector).Run /Users/tylerhelmuth/projects/opentelemetry-collector/otelcol/collector.go:312 go.opentelemetry.io/collector/otelcol.NewCommand.func1 /Users/tylerhelmuth/projects/opentelemetry-collector/otelcol/command.go:39 github.com/spf13/cobra.(*Command).execute /Users/tylerhelmuth/go/1.24.0/pkg/mod/github.com/spf13/[email protected]/command.go:1015 github.com/spf13/cobra.(*Command).ExecuteC /Users/tylerhelmuth/go/1.24.0/pkg/mod/github.com/spf13/[email protected]/command.go:1148 github.com/spf13/cobra.(*Command).Execute /Users/tylerhelmuth/go/1.24.0/pkg/mod/github.com/spf13/[email protected]/command.go:1071 main.runInteractive /Users/tylerhelmuth/projects/opentelemetry-collector/cmd/otelcorecol/main.go:57 main.run /Users/tylerhelmuth/projects/opentelemetry-collector/cmd/otelcorecol/main_others.go:10 main.main /Users/tylerhelmuth/projects/opentelemetry-collector/cmd/otelcorecol/main.go:50 runtime.main /Users/tylerhelmuth/go/1.24.0/pkg/mod/golang.org/[email protected]/src/runtime/proc.go:283 2025-05-22T17:25:30.758-0600 warn envprovider/provider.go:61 Configuration references unset environment variable {"name": "MISSING_ENV_VAR"} Error: failed to build pipelines: failed to create "otlphttp" exporter for data type "traces": endpoint must be a valid URL 2025/05/22 17:25:30 collector server run finished with error: failed to build pipelines: failed to create "otlphttp" exporter for data type "traces": endpoint must be a valid URL ``` Proof that the error log exported: ![image](https://github.com/user-attachments/assets/09ea7f06-bae1-41b8-ad8f-b286bf18e2b5)
1 parent 657abbe commit 9b4911b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: service
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Use configured loggers to log errors as soon as it is available
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13081]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

service/service.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,24 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
190190
}
191191
srv.loggerProvider = lp
192192

193+
// Use initialized logger to handle any subsequent errors
194+
// https://github.com/open-telemetry/opentelemetry-collector/pull/13081
195+
defer func() {
196+
if err != nil {
197+
logger.Error("error found during service initialization", zap.Error(err))
198+
_ = sdk.Shutdown(ctx)
199+
}
200+
}()
201+
193202
tracerProvider, err := telFactory.CreateTracerProvider(ctx, telset, &cfg.Telemetry)
194203
if err != nil {
195-
err = multierr.Append(err, sdk.Shutdown(ctx))
196204
return nil, fmt.Errorf("failed to create tracer provider: %w", err)
197205
}
198206

199207
logger.Info("Setting up own telemetry...")
200208

201209
mp, err := telFactory.CreateMeterProvider(ctx, telset, &cfg.Telemetry)
202210
if err != nil {
203-
err = multierr.Append(err, sdk.Shutdown(ctx))
204211
return nil, fmt.Errorf("failed to create meter provider: %w", err)
205212
}
206213
srv.telemetrySettings = component.TelemetrySettings{
@@ -218,13 +225,11 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
218225
})
219226

220227
if err = srv.initGraph(ctx, cfg); err != nil {
221-
err = multierr.Append(err, srv.shutdownTelemetry(ctx))
222228
return nil, err
223229
}
224230

225231
// process the configuration and initialize the pipeline
226232
if err = srv.initExtensions(ctx, cfg.Extensions); err != nil {
227-
err = multierr.Append(err, srv.shutdownTelemetry(ctx))
228233
return nil, err
229234
}
230235

0 commit comments

Comments
 (0)