forked from asahasrabuddhe/zapdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
38 lines (32 loc) · 1.14 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package zapdriver
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const serviceContextKey = "serviceContext"
// ServiceContext adds the correct service information adding the log line
// It is a required field if an error needs to be reported.
//
// see: https://cloud.google.com/error-reporting/reference/rest/v1beta1/ServiceContext
// see: https://cloud.google.com/error-reporting/docs/formatting-error-messages
func ServiceContext(name, version string) zap.Field {
return zap.Object(serviceContextKey, newServiceContext(name, version))
}
// serviceContext describes a running service that sends errors.
// Currently it only describes a service name.
type serviceContext struct {
Name string `json:"service"`
Version string `json:"version"`
}
// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (service_context serviceContext) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("service", service_context.Name)
enc.AddString("version", service_context.Version)
return nil
}
func newServiceContext(name, version string) *serviceContext {
return &serviceContext{
Name: name,
Version: version,
}
}