Skip to content

Commit d69dad5

Browse files
authored
[v2][query] Add interface for adjuster to operate on OTLP data format (#6346)
## Which problem is this PR solving? - Towards #6344 ## Description of the changes - This PR adds an interface for `Adjuster` to operate on the OTLP model format so that it can be used by the v2 query service. The v1 interface/implementation can be found in `model/adjuster`. - In the following PRs, we'll implement all the standard adjusters in `model/adjuster` to implement the new interface. ## How was this change tested? - Unit tests ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `yarn lint` and `yarn test` --------- Signed-off-by: Mahad Zaryab <[email protected]>
1 parent 8696541 commit d69dad5

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2024 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package adjuster
5+
6+
import (
7+
"errors"
8+
9+
"go.opentelemetry.io/collector/pdata/ptrace"
10+
)
11+
12+
// Adjuster defines an interface for modifying a trace object.
13+
// It returns the adjusted trace object, which is also updated in place.
14+
// If the adjuster encounters an issue that prevents it from applying
15+
// modifications, it should return the original trace object along with an error.
16+
type Adjuster interface {
17+
Adjust(ptrace.Traces) (ptrace.Traces, error)
18+
}
19+
20+
// Func is a type alias that wraps a function and makes an Adjuster from it.
21+
type Func func(trace ptrace.Traces) (ptrace.Traces, error)
22+
23+
// Adjust implements Adjuster interface for the Func alias.
24+
func (f Func) Adjust(trace ptrace.Traces) (ptrace.Traces, error) {
25+
return f(trace)
26+
}
27+
28+
// Sequence creates an adjuster that combines a series of adjusters
29+
// applied in order. Errors from each step are accumulated and returned
30+
// in the end as a single wrapper error. Errors do not interrupt the
31+
// sequence of adapters.
32+
func Sequence(adjusters ...Adjuster) Adjuster {
33+
return sequence{adjusters: adjusters}
34+
}
35+
36+
// FailFastSequence is similar to Sequence() but returns immediately
37+
// if any adjuster returns an error.
38+
func FailFastSequence(adjusters ...Adjuster) Adjuster {
39+
return sequence{adjusters: adjusters, failFast: true}
40+
}
41+
42+
type sequence struct {
43+
adjusters []Adjuster
44+
failFast bool
45+
}
46+
47+
func (c sequence) Adjust(trace ptrace.Traces) (ptrace.Traces, error) {
48+
var errs []error
49+
for _, adjuster := range c.adjusters {
50+
var err error
51+
trace, err = adjuster.Adjust(trace)
52+
if err != nil {
53+
if c.failFast {
54+
return trace, err
55+
}
56+
errs = append(errs, err)
57+
}
58+
}
59+
return trace, errors.Join(errs...)
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2024 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package adjuster_test
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"go.opentelemetry.io/collector/pdata/pcommon"
14+
"go.opentelemetry.io/collector/pdata/ptrace"
15+
16+
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc/adjuster"
17+
)
18+
19+
func TestSequences(t *testing.T) {
20+
// mock adjuster that increments last byte of span ID
21+
adj := adjuster.Func(func(trace ptrace.Traces) (ptrace.Traces, error) {
22+
span := trace.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
23+
spanId := span.SpanID()
24+
spanId[7]++
25+
span.SetSpanID(spanId)
26+
return trace, nil
27+
})
28+
29+
adjErr := errors.New("mock adjuster error")
30+
failingAdj := adjuster.Func(func(trace ptrace.Traces) (ptrace.Traces, error) {
31+
return trace, adjErr
32+
})
33+
34+
tests := []struct {
35+
name string
36+
adjuster adjuster.Adjuster
37+
err string
38+
lastSpanID pcommon.SpanID
39+
}{
40+
{
41+
name: "normal sequence",
42+
adjuster: adjuster.Sequence(adj, failingAdj, adj, failingAdj),
43+
err: fmt.Sprintf("%s\n%s", adjErr, adjErr),
44+
lastSpanID: [8]byte{0, 0, 0, 0, 0, 0, 0, 2},
45+
},
46+
{
47+
name: "fail fast sequence",
48+
adjuster: adjuster.FailFastSequence(adj, failingAdj, adj, failingAdj),
49+
err: adjErr.Error(),
50+
lastSpanID: [8]byte{0, 0, 0, 0, 0, 0, 0, 1},
51+
},
52+
}
53+
54+
for _, test := range tests {
55+
t.Run(test.name, func(t *testing.T) {
56+
trace := ptrace.NewTraces()
57+
span := trace.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
58+
span.SetSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 0})
59+
60+
adjTrace, err := test.adjuster.Adjust(trace)
61+
adjTraceSpan := adjTrace.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
62+
63+
assert.Equal(t, span, adjTraceSpan)
64+
assert.EqualValues(t, test.lastSpanID, span.SpanID())
65+
require.EqualError(t, err, test.err)
66+
})
67+
}
68+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2024 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package adjuster
5+
6+
import (
7+
"testing"
8+
9+
"github.com/jaegertracing/jaeger/pkg/testutils"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
testutils.VerifyGoLeaks(m)
14+
}

0 commit comments

Comments
 (0)