-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompositeTracer.go
117 lines (103 loc) · 3.85 KB
/
CompositeTracer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package trace
import (
"context"
cref "github.com/pip-services3-gox/pip-services3-commons-gox/refer"
)
// CompositeTracer aggregates all tracers from component references under a single component.
// It allows to record traces and conveniently send them to multiple destinations.
// References:
// - *:tracer:*:*:1.0 (optional) ITracer components to pass operation traces
// See ITracer
// Example:
// type MyComponent struct {
// tracer CompositeTracer
// }
// func NewMyComponent() *MyComponent{
// return &MyComponent{
// tracer: NewCompositeTracer(nil);
// }
// }
// func (c* MyComponent) SetReferences(ctx context.Context, references IReferences) {
// c.tracer.SetReferences(references)
// ...
// }
// public MyMethod(ctx context.Context, correlatonId string) {
// timing := c.tracer.BeginTrace(ctx, correlationId, "mycomponent", "mymethod");
// ...
// timing.EndTrace(ctx);
// if err != nil {
// timing.EndFailure(ctx, err);
// }
// }
type CompositeTracer struct {
Tracers []ITracer
}
// NewCompositeTracer creates a new instance of the tracer.
// Parameters:
// - references to locate the component dependencies.
func NewCompositeTracer() *CompositeTracer {
return &CompositeTracer{}
}
// NewCompositeTracerFromReferences creates a new instance of the tracer.
// Parameters:
// - ctx context.Context
// - refer.IReferences references to locate the component dependencies.
// Returns: CompositeLogger
func NewCompositeTracerFromReferences(ctx context.Context, references cref.IReferences) *CompositeTracer {
c := NewCompositeTracer()
c.SetReferences(ctx, references)
return c
}
// SetReferences sets references to dependent components.
// Parameters:
// - ctx context.Context
// - references to locate the component dependencies.
func (c *CompositeTracer) SetReferences(ctx context.Context, references cref.IReferences) {
if c.Tracers == nil {
c.Tracers = []ITracer{}
}
tracers := references.GetOptional(cref.NewDescriptor("*", "tracer", "*", "*", "*"))
for _, l := range tracers {
if l == c {
continue
}
if tracer, ok := l.(ITracer); ok {
c.Tracers = append(c.Tracers, tracer)
}
}
}
// Trace records an operation trace with its name and duration
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// - duration execution duration in milliseconds.
func (c *CompositeTracer) Trace(ctx context.Context, correlationId string, component string, operation string, duration int64) {
for _, tracer := range c.Tracers {
tracer.Trace(ctx, correlationId, component, operation, duration)
}
}
// Failure records an operation failure with its name, duration and error
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// - error an error object associated with this trace.
// - duration execution duration in milliseconds.
func (c *CompositeTracer) Failure(ctx context.Context, correlationId string, component string, operation string, err error, duration int64) {
for _, tracer := range c.Tracers {
tracer.Failure(ctx, correlationId, component, operation, err, duration)
}
}
// BeginTrace begins recording an operation trace
// Parameters:
// - ctx context.Context
// - correlationId (optional) transaction id to trace execution through call chain.
// - component a name of called component
// - operation a name of the executed operation.
// Returns: a trace timing object.
func (c *CompositeTracer) BeginTrace(ctx context.Context, correlationId string, component string, operation string) *TraceTiming {
return NewTraceTiming(correlationId, component, operation, c)
}