-
Notifications
You must be signed in to change notification settings - Fork 247
feat(tracing): part 10 da retriever tracing #2991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
200f4d6
feat: add sequencer tracing instrumentation
chatton 1ef693a
chore: using helper fn instead of having it duplicated
chatton ef202f4
chore: adding da retreiver syncing
chatton 4312e48
Merge branch 'main' into tracing-part-10-da-retriever-v2
chatton eeea8b6
chore: bump sonic version to work with 1.25
chatton 65fda65
Merge branch 'main' into tracing-part-10-da-retriever-v2
chatton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,57 @@ | ||
| package syncing | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/codes" | ||
| "go.opentelemetry.io/otel/trace" | ||
|
|
||
| "github.com/evstack/ev-node/block/internal/common" | ||
| ) | ||
|
|
||
| var _ DARetriever = (*tracedDARetriever)(nil) | ||
|
|
||
| // tracedDARetriever wraps a DARetriever with OpenTelemetry tracing. | ||
| type tracedDARetriever struct { | ||
| inner DARetriever | ||
| tracer trace.Tracer | ||
| } | ||
|
|
||
| // WithTracingDARetriever wraps a DARetriever with OpenTelemetry tracing. | ||
| func WithTracingDARetriever(inner DARetriever) DARetriever { | ||
| return &tracedDARetriever{ | ||
| inner: inner, | ||
| tracer: otel.Tracer("ev-node/da-retriever"), | ||
| } | ||
| } | ||
|
|
||
| func (t *tracedDARetriever) RetrieveFromDA(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) { | ||
| ctx, span := t.tracer.Start(ctx, "DARetriever.RetrieveFromDA", | ||
| trace.WithAttributes( | ||
| attribute.Int64("da.height", int64(daHeight)), | ||
| ), | ||
| ) | ||
| defer span.End() | ||
|
|
||
| events, err := t.inner.RetrieveFromDA(ctx, daHeight) | ||
| if err != nil { | ||
| span.RecordError(err) | ||
| span.SetStatus(codes.Error, err.Error()) | ||
| return events, err | ||
| } | ||
|
|
||
| span.SetAttributes(attribute.Int("event.count", len(events))) | ||
|
|
||
| // add block heights from events | ||
| if len(events) > 0 { | ||
| heights := make([]int64, len(events)) | ||
| for i, event := range events { | ||
| heights[i] = int64(event.Header.Height()) | ||
| } | ||
| span.SetAttributes(attribute.Int64Slice("block.heights", heights)) | ||
| } | ||
|
|
||
| return events, nil | ||
| } |
This file contains hidden or 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,123 @@ | ||
| package syncing | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/codes" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| "go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
|
|
||
| "github.com/evstack/ev-node/block/internal/common" | ||
| "github.com/evstack/ev-node/pkg/telemetry/testutil" | ||
| "github.com/evstack/ev-node/types" | ||
| ) | ||
|
|
||
| type mockDARetriever struct { | ||
| retrieveFromDAFn func(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) | ||
| } | ||
|
|
||
| func (m *mockDARetriever) RetrieveFromDA(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) { | ||
| if m.retrieveFromDAFn != nil { | ||
| return m.retrieveFromDAFn(ctx, daHeight) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func setupDARetrieverTrace(t *testing.T, inner DARetriever) (DARetriever, *tracetest.SpanRecorder) { | ||
| t.Helper() | ||
| sr := tracetest.NewSpanRecorder() | ||
| tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) | ||
| t.Cleanup(func() { _ = tp.Shutdown(context.Background()) }) | ||
| otel.SetTracerProvider(tp) | ||
| return WithTracingDARetriever(inner), sr | ||
| } | ||
|
|
||
| func TestTracedDARetriever_RetrieveFromDA_Success(t *testing.T) { | ||
| mock := &mockDARetriever{ | ||
| retrieveFromDAFn: func(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) { | ||
| return []common.DAHeightEvent{ | ||
| { | ||
| Header: &types.SignedHeader{ | ||
| Header: types.Header{ | ||
| BaseHeader: types.BaseHeader{Height: 100}, | ||
| }, | ||
| }, | ||
| DaHeight: daHeight, | ||
| Source: common.SourceDA, | ||
| }, | ||
| { | ||
| Header: &types.SignedHeader{ | ||
| Header: types.Header{ | ||
| BaseHeader: types.BaseHeader{Height: 101}, | ||
| }, | ||
| }, | ||
| DaHeight: daHeight, | ||
| Source: common.SourceDA, | ||
| }, | ||
| }, nil | ||
| }, | ||
| } | ||
| retriever, sr := setupDARetrieverTrace(t, mock) | ||
| ctx := context.Background() | ||
|
|
||
| events, err := retriever.RetrieveFromDA(ctx, 50) | ||
| require.NoError(t, err) | ||
| require.Len(t, events, 2) | ||
|
|
||
| spans := sr.Ended() | ||
| require.Len(t, spans, 1) | ||
| span := spans[0] | ||
| require.Equal(t, "DARetriever.RetrieveFromDA", span.Name()) | ||
| require.Equal(t, codes.Unset, span.Status().Code) | ||
|
|
||
| attrs := span.Attributes() | ||
| testutil.RequireAttribute(t, attrs, "da.height", int64(50)) | ||
| testutil.RequireAttribute(t, attrs, "event.count", 2) | ||
| } | ||
|
|
||
| func TestTracedDARetriever_RetrieveFromDA_NoEvents(t *testing.T) { | ||
| mock := &mockDARetriever{ | ||
| retrieveFromDAFn: func(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) { | ||
| return []common.DAHeightEvent{}, nil | ||
| }, | ||
| } | ||
| retriever, sr := setupDARetrieverTrace(t, mock) | ||
| ctx := context.Background() | ||
|
|
||
| events, err := retriever.RetrieveFromDA(ctx, 50) | ||
| require.NoError(t, err) | ||
| require.Empty(t, events) | ||
|
|
||
| spans := sr.Ended() | ||
| require.Len(t, spans, 1) | ||
| span := spans[0] | ||
| require.Equal(t, codes.Unset, span.Status().Code) | ||
|
|
||
| attrs := span.Attributes() | ||
| testutil.RequireAttribute(t, attrs, "event.count", 0) | ||
| } | ||
|
|
||
| func TestTracedDARetriever_RetrieveFromDA_Error(t *testing.T) { | ||
| expectedErr := errors.New("DA retrieval failed") | ||
| mock := &mockDARetriever{ | ||
| retrieveFromDAFn: func(ctx context.Context, daHeight uint64) ([]common.DAHeightEvent, error) { | ||
| return nil, expectedErr | ||
| }, | ||
| } | ||
| retriever, sr := setupDARetrieverTrace(t, mock) | ||
| ctx := context.Background() | ||
|
|
||
| _, err := retriever.RetrieveFromDA(ctx, 50) | ||
| require.Error(t, err) | ||
| require.Equal(t, expectedErr, err) | ||
|
|
||
| spans := sr.Ended() | ||
| require.Len(t, spans, 1) | ||
| span := spans[0] | ||
| require.Equal(t, codes.Error, span.Status().Code) | ||
| require.Equal(t, expectedErr.Error(), span.Status().Description) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is great for verifying the success case. To make it more comprehensive, it would be beneficial to also assert that the
block.heightsattribute is correctly set on the span when events are returned.The
testutil.RequireAttributehelper doesn't seem to support slice attributes, so you might need to perform this check manually. Here's a suggestion to add this verification: