-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tailsamplingprocessor: add latency policy
- Loading branch information
Koenraad Verheyden
committed
Jun 10, 2021
1 parent
18cec46
commit 54603c7
Showing
7 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sampling | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type latency struct { | ||
logger *zap.Logger | ||
thresholdMs int64 | ||
} | ||
|
||
var _ PolicyEvaluator = (*latency)(nil) | ||
|
||
// NewLatency creates a policy evaluator the samples all traces. | ||
func NewLatency(logger *zap.Logger, thresholdMs int64) PolicyEvaluator { | ||
return &latency{ | ||
logger: logger, | ||
thresholdMs: thresholdMs, | ||
} | ||
} | ||
|
||
// OnLateArrivingSpans notifies the evaluator that the given list of spans arrived | ||
// after the sampling decision was already taken for the trace. | ||
// This gives the evaluator a chance to log any message/metrics and/or update any | ||
// related internal state. | ||
func (l *latency) OnLateArrivingSpans(Decision, []*pdata.Span) error { | ||
l.logger.Debug("Triggering action for late arriving spans in latency filter") | ||
return nil | ||
} | ||
|
||
// Evaluate looks at the trace data and returns a corresponding SamplingDecision. | ||
func (l *latency) Evaluate(traceID pdata.TraceID, traceData *TraceData) (Decision, error) { | ||
l.logger.Debug("Evaluating spans in latency filter") | ||
|
||
traceData.Lock() | ||
batches := traceData.ReceivedBatches | ||
traceData.Unlock() | ||
|
||
for _, batch := range batches { | ||
rspans := batch.ResourceSpans() | ||
|
||
for i := 0; i < rspans.Len(); i++ { | ||
rs := rspans.At(i) | ||
ilss := rs.InstrumentationLibrarySpans() | ||
|
||
for i := 0; i < ilss.Len(); i++ { | ||
ils := ilss.At(i) | ||
|
||
for j := 0; j < ils.Spans().Len(); j++ { | ||
span := ils.Spans().At(j) | ||
|
||
startTime := span.StartTimestamp().AsTime() | ||
endTime := span.EndTimestamp().AsTime() | ||
|
||
duration := endTime.Sub(startTime) | ||
if duration.Milliseconds() >= l.thresholdMs { | ||
return Sampled, nil | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return NotSampled, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sampling | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestEvaluate_Latency(t *testing.T) { | ||
filter := NewLatency(zap.NewNop(), 5000) | ||
|
||
traceID := pdata.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) | ||
|
||
decision, err := filter.Evaluate(traceID, newTraceWithDuration(4500*time.Millisecond)) | ||
assert.Nil(t, err) | ||
assert.Equal(t, decision, NotSampled) | ||
|
||
decision, err = filter.Evaluate(traceID, newTraceWithDuration(5500*time.Millisecond)) | ||
assert.Nil(t, err) | ||
assert.Equal(t, decision, Sampled) | ||
} | ||
|
||
func TestOnLateArrivingSpans_Latency(t *testing.T) { | ||
filter := NewLatency(zap.NewNop(), 5000) | ||
err := filter.OnLateArrivingSpans(NotSampled, nil) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func newTraceWithDuration(duration time.Duration) *TraceData { | ||
now := time.Now() | ||
|
||
var traceBatches []pdata.Traces | ||
traces := pdata.NewTraces() | ||
rs := traces.ResourceSpans().AppendEmpty() | ||
ils := rs.InstrumentationLibrarySpans().AppendEmpty() | ||
span := ils.Spans().AppendEmpty() | ||
span.SetTraceID(pdata.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})) | ||
span.SetSpanID(pdata.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) | ||
span.SetStartTimestamp(pdata.TimestampFromTime(now)) | ||
span.SetEndTimestamp(pdata.TimestampFromTime(now.Add(duration))) | ||
traceBatches = append(traceBatches, traces) | ||
return &TraceData{ | ||
ReceivedBatches: traceBatches, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters