forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
67 lines (53 loc) · 1.73 KB
/
options.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
package tracing
import (
"errors"
"fmt"
"github.com/dop251/goja"
"go.k6.io/k6/js/common"
)
// options are the options that can be passed to the
// tracing.instrumentHTTP() method.
type options struct {
// Propagation is the propagation format to use for the tracer.
Propagator string `json:"propagator"`
// Sampling is the sampling rate to use for the
// tracer, expressed in percents within the
// bounds: 0.0 <= n <= 1.0.
Sampling float64 `json:"sampling"`
// Baggage is a map of baggage items to add to the tracer.
Baggage map[string]string `json:"baggage"`
}
// defaultSamplingRate is the default sampling rate applied to options.
const defaultSamplingRate float64 = 1.0
// newOptions returns a new options object from the given goja.Value.
//
// Note that if the sampling field value is absent, or nullish, we'll
// set it to the `defaultSamplingRate` value.
func newOptions(rt *goja.Runtime, from goja.Value) (options, error) {
var opts options
if err := rt.ExportTo(from, &opts); err != nil {
return opts, fmt.Errorf("unable to parse options object; reason: %w", err)
}
fromSamplingValue := from.ToObject(rt).Get("sampling")
if fromSamplingValue == nil || common.IsNullish(fromSamplingValue) {
opts.Sampling = defaultSamplingRate
}
return opts, nil
}
func (i *options) validate() error {
var (
isW3C = i.Propagator == W3CPropagatorName
isJaeger = i.Propagator == JaegerPropagatorName
)
if !isW3C && !isJaeger {
return fmt.Errorf("unknown propagator: %s", i.Propagator)
}
if i.Sampling < 0.0 || i.Sampling > 1.0 {
return errors.New("sampling rate must be between 0.0 and 1.0")
}
// TODO: implement baggage support
if i.Baggage != nil {
return errors.New("baggage is not yet supported")
}
return nil
}