-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_context.go
48 lines (43 loc) · 1.03 KB
/
job_context.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
39
40
41
42
43
44
45
46
47
48
package golibcron
import (
"context"
"github.com/golibs-starter/golib/log/field"
)
const ContextValueJobName = "job_attributes_name"
const ContextValueJobRunId = "job_attributes_run_id"
const ContextLogJobMeta = "job_meta"
type JobAttributes struct {
Name string `json:"name,omitempty"`
RunId string `json:"run_id,omitempty"`
}
func (c JobAttributes) MarshalLogObject(encoder field.ObjectEncoder) error {
if c.Name != "" {
encoder.AddString("name", c.Name)
}
if c.RunId != "" {
encoder.AddString("run_id", c.RunId)
}
return nil
}
func ContextExtractor(ctx context.Context) []field.Field {
jobNameAny := ctx.Value(ContextValueJobName)
if jobNameAny == nil {
return nil
}
attrs := JobAttributes{}
if jobName, ok := jobNameAny.(string); ok {
attrs.Name = jobName
}
if attrs.Name == "" {
return nil
}
jobRunIdAny := ctx.Value(ContextValueJobRunId)
if jobRunIdAny != nil {
if jobRunId, ok := jobRunIdAny.(string); ok {
attrs.RunId = jobRunId
}
}
return []field.Field{
field.Object(ContextLogJobMeta, &attrs),
}
}