/
priority.go
60 lines (49 loc) · 1.36 KB
/
priority.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"bytes"
"fmt"
"strings"
)
// priority allows for a priority sampling of events. When an event
// is created it is given a priority. Whenever an event pool is
// full and events need to be dropped, the events with the lowest priority
// are dropped.
type priority float32
// According to spec, Agents SHOULD truncate the value to at most 6
// digits past the decimal point.
const (
priorityFormat = "%.6f"
)
func newPriorityFromRandom(rnd func() float32) priority {
for {
if r := rnd(); 0.0 != r {
return priority(r)
}
}
}
// newPriority returns a new priority.
func newPriority() priority {
return newPriorityFromRandom(randFloat32)
}
// Float32 returns the priority as a float32.
func (p priority) Float32() float32 {
return float32(p)
}
func (p priority) isLowerPriority(y priority) bool {
return p < y
}
// MarshalJSON limits the number of decimals.
func (p priority) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(priorityFormat, p)), nil
}
// WriteJSON limits the number of decimals.
func (p priority) WriteJSON(buf *bytes.Buffer) {
fmt.Fprintf(buf, priorityFormat, p)
}
func (p priority) traceStateFormat() string {
s := fmt.Sprintf(priorityFormat, p)
s = strings.TrimRight(s, "0")
return strings.TrimRight(s, ".")
}