Skip to content

Commit a54a8a2

Browse files
authored
Add Sampler API (#70)
- also add implementation of AlwaysSampleSampler and NeverSampleSampler
1 parent 6dab220 commit a54a8a2

File tree

5 files changed

+226
-0
lines changed

5 files changed

+226
-0
lines changed

api/trace/always_sampler.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import (
18+
"go.opentelemetry.io/api/core"
19+
)
20+
21+
const (
22+
alwaysSamplerDescription = "AlwaysSampleSampler"
23+
)
24+
25+
var alwaysSampleDecision = Decision{Sampled: true}
26+
27+
type alwaysSampleSampler struct{}
28+
29+
// ShouldSample implements Sampler interface.
30+
// It always returns a Decision with Sampled value set to true
31+
// and with Attributes set to an empty slice.
32+
func (as alwaysSampleSampler) ShouldSample(
33+
_ core.SpanContext,
34+
_ bool,
35+
_ core.TraceID,
36+
_ uint64,
37+
_ string,
38+
) Decision {
39+
return alwaysSampleDecision
40+
}
41+
42+
// Description implements Sampler interface.
43+
// It returns the description of this sampler.
44+
func (as alwaysSampleSampler) Description() string {
45+
return alwaysSamplerDescription
46+
}
47+
48+
var _ Sampler = alwaysSampleSampler{}
49+
50+
func AlwaysSampleSampler() alwaysSampleSampler {
51+
return alwaysSampleSampler{}
52+
}

api/trace/always_sampler_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
"go.opentelemetry.io/api/core"
22+
)
23+
24+
func TestShouldSample(t *testing.T) {
25+
gotD := AlwaysSampleSampler().ShouldSample(
26+
core.SpanContext{}, false, core.TraceID{}, 0, "span")
27+
wantD := Decision{Sampled: true}
28+
if diff := cmp.Diff(wantD, gotD); diff != "" {
29+
t.Errorf("Decision: +got, -want%v", diff)
30+
}
31+
}
32+
33+
func TestDescription(t *testing.T) {
34+
gotDesc := AlwaysSampleSampler().Description()
35+
wantDesc := alwaysSamplerDescription
36+
if diff := cmp.Diff(wantDesc, gotDesc); diff != "" {
37+
t.Errorf("Description: +got, -want%v", diff)
38+
}
39+
}

api/trace/never_sampler.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import (
18+
"go.opentelemetry.io/api/core"
19+
)
20+
21+
const (
22+
neverSamplerDescription = "NeverSampleSampler"
23+
)
24+
25+
var neverSampledecision = Decision{Sampled: false}
26+
27+
type neverSampleSampler struct{}
28+
29+
// ShouldSample implements Sampler interface.
30+
// It always returns a Decision with Sampled value set to false
31+
// and with Attributes set to an empty slice.
32+
func (ns neverSampleSampler) ShouldSample(
33+
_ core.SpanContext,
34+
_ bool,
35+
_ core.TraceID,
36+
_ uint64,
37+
_ string,
38+
) Decision {
39+
return neverSampledecision
40+
}
41+
42+
// Description implements Sampler interface.
43+
// It returns the description of this sampler.
44+
func (ns neverSampleSampler) Description() string {
45+
return neverSamplerDescription
46+
}
47+
48+
var _ Sampler = neverSampleSampler{}
49+
50+
func NeverSampleSampler() neverSampleSampler {
51+
return neverSampleSampler{}
52+
}

api/trace/never_sampler_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
"go.opentelemetry.io/api/core"
22+
)
23+
24+
func TestNeverSamperShouldSample(t *testing.T) {
25+
gotD := NeverSampleSampler().ShouldSample(
26+
core.SpanContext{}, false, core.TraceID{}, 0, "span")
27+
wantD := Decision{Sampled: false}
28+
if diff := cmp.Diff(wantD, gotD); diff != "" {
29+
t.Errorf("Decision: +got, -want%v", diff)
30+
}
31+
}
32+
33+
func TestNeverSamplerDescription(t *testing.T) {
34+
gotDesc := NeverSampleSampler().Description()
35+
wantDesc := neverSamplerDescription
36+
if diff := cmp.Diff(wantDesc, gotDesc); diff != "" {
37+
t.Errorf("Description: +got, -want%v", diff)
38+
}
39+
}

api/trace/sampler.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package trace
16+
17+
import "go.opentelemetry.io/api/core"
18+
19+
type Sampler interface {
20+
// ShouldSample returns a Decision that contains a decision whether to sample
21+
// or not sample the span to be created. Decision is based on a Sampler specific
22+
// algorithm that takes into account one or more input parameters.
23+
ShouldSample(
24+
sc core.SpanContext,
25+
remote bool,
26+
traceID core.TraceID,
27+
spanID uint64,
28+
spanName string,
29+
) Decision
30+
31+
// Description returns of the sampler. It contains its name or short description
32+
// and its configured properties.
33+
// For example 'ProbabilitySampler:{0.00001}'
34+
Description() string
35+
}
36+
37+
type Decision struct {
38+
// Sampled is set true if the span should be sampled.
39+
Sampled bool
40+
41+
// Attributes provides insight into Sample r's decision process.
42+
// It could be empty slice or nil if no attributes are recorded by the sampler.
43+
Attributes []core.KeyValue
44+
}

0 commit comments

Comments
 (0)