Skip to content

chore: deprecate cloudzap package and zap-based logging API - #844

Merged
fredrikaverpil merged 3 commits into
masterfrom
deprecate-cloudzap
Apr 1, 2026
Merged

chore: deprecate cloudzap package and zap-based logging API#844
fredrikaverpil merged 3 commits into
masterfrom
deprecate-cloudzap

Conversation

@fredrikaverpil

@fredrikaverpil fredrikaverpil commented Apr 1, 2026

Copy link
Copy Markdown
Member

Why?

The cloudslog.Handler configured by cloudrunner.Run provides the same capabilities as the cloudzap package — Cloud Logging field formatting, error reporting, and trace correlation — using standard log/slog instead of go.uber.org/zap. The zap logger, its context middleware, and explicit trace field injection are all redundant now.

Rather than removing the zap path outright (as in #814), this deprecates it first to give consumers time to migrate to log/slog with cloudslog.

What?

  • Deprecate the cloudzap package and all its exported symbols
  • Deprecate cloudrunner.Logger and cloudrunner.WithLoggerFields
  • Deprecate cloudrequestlog.ErrorDetails (returns zap.Field)
  • Deprecate WithOtelTraceHook, TraceIDHook, IDHook
  • Deprecate ProjectID and TraceHook fields on both cloudotel.TraceMiddleware and cloudtrace.Middleware
  • Fix incorrect doc comment on WithOtelTraceHook (said WithTraceHook)

Migration guide

Logging

Replace cloudrunner.Logger(ctx) with standard log/slog context functions. Custom fields can be passed directly as arguments:

// Before
cloudrunner.Logger(ctx).Info("processing event", zap.String("event_id", id))

// After
slog.InfoContext(ctx, "processing event", slog.String("event_id", id))

Context enrichment

Both cloudrunner.WithLoggerFields and cloudslog.With enrich all subsequent logs within the returned context. The replacement is a direct swap:

// Before
ctx = cloudrunner.WithLoggerFields(ctx,
    zap.String("event_type", eventType),
    zap.String("event_id", id),
)

// After
ctx = cloudslog.With(ctx,
    slog.String("event_type", eventType),
    slog.String("event_id", id),
)

Trace correlation

No action needed. The cloudslog.Handler (configured by cloudrunner.Run) automatically extracts trace/span IDs from the OpenTelemetry span context on every log call. Remove any WithOtelTraceHook or WithTraceHook options from your cloudrunner.Run call — they are no longer needed.

Error helpers

Use cloudslog.Errors for logging multiple errors:

slog.WarnContext(ctx, "partial failure", cloudslog.Errors(errs))

Request logging (cloudrequestlog)

The cloudrequestlog package already uses slog internally and is not deprecated. However, cloudrequestlog.ErrorDetails returns a zap.Field and is now deprecated. AdditionalFields.Add still accepts both zap.Field and slog.Attr for backwards compatibility, but prefer slog.Attr when adding custom fields to request logs.

Notes

  • No behavioral changes — existing code continues to work
  • Follow-up: remove the deprecated code in a future major version (see feat!: remove cloudzap #814)

@fredrikaverpil fredrikaverpil self-assigned this Apr 1, 2026
@fredrikaverpil
fredrikaverpil marked this pull request as ready for review April 1, 2026 06:05
@fredrikaverpil
fredrikaverpil requested review from a team as code owners April 1, 2026 06:05
@fredrikaverpil
fredrikaverpil force-pushed the deprecate-cloudzap branch 2 times, most recently from 366030d to e0f7e66 Compare April 1, 2026 06:25
Comment thread cloudzap/context.go Outdated

// WithLogger adds a logger to the current context.
//
// Deprecated: Use slog.SetDefault with cloudslog.NewHandler instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloudrunner-go sets its own default with Google Cloud Logging support so using slog.SetDefault is probably not what one wants to do (generally)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to

// WithLogger adds a logger to the current context.
//
// Deprecated: cloudrunner.Run configures the default slog logger with cloudslog.Handler.
// Use slog.InfoContext, slog.WarnContext, etc. instead of a context-based zap logger.
func WithLogger(ctx context.Context, logger *zap.Logger) context.Context {
	return context.WithValue(ctx, loggerContextKey{}, logger)
}

Comment thread cloudotel/tracemiddleware.go Outdated
Comment on lines +30 to +33
// Deprecated: As per https://docs.cloud.google.com/trace/docs/trace-log-integration the
// preferred trace format is now just the trace ID, not projects/PROJECT_ID/traces/TRACE_ID.
// The cloudslog.Handler injects trace fields automatically from the OpenTelemetry span
// context without requiring a project ID.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not used and was mistakenly missed to be deleted in the last PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I guess we shouldn't remove it- or do you think we should?

I changed it now into

// ProjectID of the project the service is running in.
//
// Deprecated: No longer used. As per https://docs.cloud.google.com/trace/docs/trace-log-integration
// the preferred trace format is now just the trace ID, not projects/PROJECT_ID/traces/TRACE_ID.
ProjectID string

Comment thread cloudrequestlog/details.go Outdated
Comment on lines +15 to +17
//
// Deprecated: Use slog.Any with slog.Attr instead of zap.Field. The zapcore.Field support
// in AdditionalFields.Add will be removed in a future version.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually no equivalent function at the moment. I did a quick search internally and I don't believe anybody is using this so let's just make the message clearer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, changed it:

// ErrorDetails creates a zap.Field that logs the gRPC error details of the provided error.
//
// Deprecated: Returns a zap.Field which ties consumers to the deprecated zap dependency.
// There is no drop-in slog replacement yet. This function will be removed in a future version.
func ErrorDetails(err error) zap.Field {
  ...
}

Comment thread cloudtrace/middleware.go Outdated
Comment on lines +19 to +23
//
// Deprecated: As per https://docs.cloud.google.com/trace/docs/trace-log-integration the
// preferred trace format is now just the trace ID, not projects/PROJECT_ID/traces/TRACE_ID.
// The cloudslog.Handler injects trace fields automatically from the OpenTelemetry span
// context without requiring a project ID.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not used and was mistakenly missed to be removed from last PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it like in the previous instance:

// ProjectID of the project the service is running in.
//
// Deprecated: No longer used. As per https://docs.cloud.google.com/trace/docs/trace-log-integration
// the preferred trace format is now just the trace ID, not projects/PROJECT_ID/traces/TRACE_ID.
ProjectID string

Comment thread options.go Outdated
Comment on lines +54 to +57
// WithOtelTraceHook configures the run context with a trace hook.
//
// Deprecated: The cloudslog.Handler automatically injects trace fields from the
// OpenTelemetry span context, making custom trace hooks for log enrichment redundant.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithOtelTraceHook allows selecting the new opentelemetry trace features (instead of the old custom one). The reason it should be deprecated is that we will remove the old features completely and there will be no reason to select the one since it will be the only one. (I feel the comment was already correct in this one which was already marked as deprecated except for the function name which was wrong)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I reverted this change into the deprecation warning we already had.

@fredrikaverpil
fredrikaverpil marked this pull request as draft April 1, 2026 09:40
@fredrikaverpil
fredrikaverpil changed the base branch from master to chore/bump-go-sage-golangcilint April 1, 2026 09:44
@fredrikaverpil
fredrikaverpil force-pushed the deprecate-cloudzap branch 3 times, most recently from 33232ef to 61faadb Compare April 1, 2026 10:06
@fredrikaverpil
fredrikaverpil marked this pull request as ready for review April 1, 2026 10:08
@fredrikaverpil
fredrikaverpil marked this pull request as draft April 1, 2026 10:08
@fredrikaverpil
fredrikaverpil force-pushed the chore/bump-go-sage-golangcilint branch 4 times, most recently from 0d6a1e3 to 40abb3d Compare April 1, 2026 11:04
Base automatically changed from chore/bump-go-sage-golangcilint to master April 1, 2026 11:05
The cloudslog.Handler configured by cloudrunner.Run provides the same
capabilities as the cloudzap package — Cloud Logging field formatting,
error reporting, and trace correlation — using standard log/slog. This
makes the zap logger, its context middleware, and explicit trace field
injection redundant.

Deprecate ahead of removal in a future version:

- cloudzap package and all exported symbols
- cloudrunner.Logger and cloudrunner.WithLoggerFields
- cloudrequestlog.ErrorDetails (returns zap.Field)
- WithOtelTraceHook, TraceIDHook, IDHook
- TraceHook fields on trace middlewares

Also remove unused ProjectID fields from cloudotel.TraceMiddleware and
cloudtrace.Middleware (dead code after the trace format change in #840),
and fix incorrect doc comment on WithOtelTraceHook (said WithTraceHook).
@fredrikaverpil
fredrikaverpil marked this pull request as ready for review April 1, 2026 12:07
@fredrikaverpil
fredrikaverpil merged commit 870c2ef into master Apr 1, 2026
1 check passed
@fredrikaverpil
fredrikaverpil deleted the deprecate-cloudzap branch April 1, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants