-
Notifications
You must be signed in to change notification settings - Fork 45
/
operation.go
72 lines (59 loc) · 2.22 KB
/
operation.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package zapdriver
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const operationKey = "logging.googleapis.com/operation"
// Operation adds the correct Stackdriver "operation" field.
//
// Additional information about a potentially long-running operation with which
// a log entry is associated.
//
// see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntryOperation
func Operation(id, producer string, first, last bool) zap.Field {
op := &operation{
ID: id,
Producer: producer,
First: first,
Last: last,
}
return zap.Object(operationKey, op)
}
// OperationStart is a convenience function for `Operation`. It should be called
// for the first operation log.
func OperationStart(id, producer string) zap.Field {
return Operation(id, producer, true, false)
}
// OperationCont is a convenience function for `Operation`. It should be called
// for any non-start/end operation log.
func OperationCont(id, producer string) zap.Field {
return Operation(id, producer, false, false)
}
// OperationEnd is a convenience function for `Operation`. It should be called
// for the last operation log.
func OperationEnd(id, producer string) zap.Field {
return Operation(id, producer, false, true)
}
// operation is the complete payload that can be interpreted by Stackdriver as
// an operation.
type operation struct {
// Optional. An arbitrary operation identifier. Log entries with the same
// identifier are assumed to be part of the same operation.
ID string `json:"id"`
// Optional. An arbitrary producer identifier. The combination of id and
// producer must be globally unique. Examples for producer:
// "MyDivision.MyBigCompany.com", "github.com/MyProject/MyApplication".
Producer string `json:"producer"`
// Optional. Set this to True if this is the first log entry in the operation.
First bool `json:"first"`
// Optional. Set this to True if this is the last log entry in the operation.
Last bool `json:"last"`
}
// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (op operation) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("id", op.ID)
enc.AddString("producer", op.Producer)
enc.AddBool("first", op.First)
enc.AddBool("last", op.Last)
return nil
}