Skip to content

volume/service: change some logs to use structured logs#48675

Merged
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:volumes_more_structured_logs
Oct 18, 2024
Merged

volume/service: change some logs to use structured logs#48675
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:volumes_more_structured_logs

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added status/2-code-review area/volumes Volumes kind/refactor PR's that refactor, or clean-up code labels Oct 16, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Oct 16, 2024
@thaJeztah thaJeztah self-assigned this Oct 16, 2024
Comment thread volume/service/store.go
driverName := v.DriverName()
if _, err := s.drivers.ReleaseDriver(driverName); err != nil {
log.G(ctx).WithError(err).WithField("driver", driverName).Error("Error releasing reference to volume driver")
log.G(ctx).WithFields(log.Fields{"error": err, "driver": driverName, "volume-name": name}).Error("Error releasing reference to volume driver")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What was the problem with WithError?

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.

Mostly because WithError, WithField, WithFields do a lot under the hood; they have to merge new values with old values, and performing reflection; doing a single WithFields can avoid doing that multiple times;

// Add an error as single field (using the key defined in ErrorKey) to the Entry.
func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err)
}
// Add a context to the Entry.
func (entry *Entry) WithContext(ctx context.Context) *Entry {
dataCopy := make(Fields, len(entry.Data))
for k, v := range entry.Data {
dataCopy[k] = v
}
return &Entry{Logger: entry.Logger, Data: dataCopy, Time: entry.Time, err: entry.err, Context: ctx}
}
// Add a single field to the Entry.
func (entry *Entry) WithField(key string, value interface{}) *Entry {
return entry.WithFields(Fields{key: value})
}
// Add a map of fields to the Entry.
func (entry *Entry) WithFields(fields Fields) *Entry {
data := make(Fields, len(entry.Data)+len(fields))
for k, v := range entry.Data {
data[k] = v
}
fieldErr := entry.err
for k, v := range fields {
isErrField := false
if t := reflect.TypeOf(v); t != nil {
switch {
case t.Kind() == reflect.Func, t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func:
isErrField = true
}
}
if isErrField {
tmp := fmt.Sprintf("can not add field %q", k)
if fieldErr != "" {
fieldErr = entry.err + ", " + tmp
} else {
fieldErr = tmp
}
} else {
data[k] = v
}
}
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}
}

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.

@AkihiroSuda ☝️ does that answer your question? Do the changes LGTY?

Comment thread volume/service/store.go
driverName := v.DriverName()
if _, err := s.drivers.ReleaseDriver(driverName); err != nil {
log.G(ctx).WithError(err).WithField("driver", driverName).Error("Error releasing reference to volume driver")
log.G(ctx).WithFields(log.Fields{"error": err, "driver": driverName, "volume-name": name}).Error("Error releasing reference to volume driver")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
log.G(ctx).WithFields(log.Fields{"error": err, "driver": driverName, "volume-name": name}).Error("Error releasing reference to volume driver")
log.G(ctx).WithFields(log.Fields{logrus.ErrorKey: err, "driver": driverName, "volume-name": name}).Error("Error releasing reference to volume driver")

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.

Ah, yeah, that's on purpose; trying to avoid direct imports of logrus (we changed all imports to use containerd's logs package, which provides aliases for the essential parts)

@thaJeztah thaJeztah merged commit b0632b2 into moby:master Oct 18, 2024
@thaJeztah thaJeztah deleted the volumes_more_structured_logs branch October 18, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/volumes Volumes kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants