-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity.go
More file actions
39 lines (33 loc) · 769 Bytes
/
Copy pathactivity.go
File metadata and controls
39 lines (33 loc) · 769 Bytes
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
package main
import (
"context"
"time"
)
type activityEvent struct {
Stage string
Model string
SourceFormat string
ToFormat string
RequestID string
Count int
Stream bool
RuleCounts map[string]int
Elapsed time.Duration
}
type activityLogger func(activityEvent)
type activityLoggerContextKey struct{}
func withActivityLogger(ctx context.Context, logger activityLogger) context.Context {
if logger == nil {
return ctx
}
return context.WithValue(ctx, activityLoggerContextKey{}, logger)
}
func emitActivity(ctx context.Context, event activityEvent) {
if event.Count <= 0 || ctx == nil {
return
}
logger, _ := ctx.Value(activityLoggerContextKey{}).(activityLogger)
if logger != nil {
logger(event)
}
}