/
tracing_model.go
58 lines (49 loc) · 1.51 KB
/
tracing_model.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
package features
import (
"errors"
"time"
"github.com/openzipkin/zipkin-go/model"
)
// Timers store timestamp and duration in JSON format from model.SpanModel
type Timers struct {
T int64 `json:"timestamp,omitempty"`
D int64 `json:"duration,omitempty"`
}
func getTimers(s model.SpanModel) (*Timers, error) {
var timestamp int64
if !s.Timestamp.IsZero() {
if s.Timestamp.Unix() < 1 {
// Zipkin does not allow Timestamps before Unix epoch
return nil, errors.New("ErrValidTimestampRequired")
}
timestamp = s.Timestamp.Round(time.Microsecond).UnixNano() / 1e3
}
if s.Duration < time.Microsecond {
if s.Duration < 0 {
// negative duration is not allowed and signals a timing logic error
return nil, errors.New("ErrValidDurationRequired")
} else if s.Duration > 0 {
// sub microsecond durations are reported as 1 microsecond
s.Duration = 1 * time.Microsecond
}
} else {
// Duration will be rounded to nearest microsecond representation.
//
// NOTE: Duration.Round() is not available in Go 1.8 which we still support.
// To handle microsecond resolution rounding we'll add 500 nanoseconds to
// the duration. When truncated to microseconds in the call to marshal, it
// will be naturally rounded. See TestSpanDurationRounding in span_test.go
s.Duration += 500 * time.Nanosecond
}
if s.LocalEndpoint.Empty() {
s.LocalEndpoint = nil
}
if s.RemoteEndpoint.Empty() {
s.RemoteEndpoint = nil
}
timers := Timers{
T: timestamp,
D: s.Duration.Nanoseconds() / 1e3,
}
return &timers, nil
}