-
Notifications
You must be signed in to change notification settings - Fork 533
services/friendbot: Instrument friendbot to emit traces #5796
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
Open
satyamz
wants to merge
16
commits into
stellar:master
Choose a base branch
from
satyamz:friendbot-instrumentation-03
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
dd4613e
Add stellar tracer to utils package
satyamz d844cd6
Add otelchi middleware for the go-chi middle
satyamz 1710eb8
Update go-chi package version
satyamz c43ef69
Add instrumentation for friendbot handler function
satyamz 12d1a91
Add insecure flag for exporter
satyamz 0a6b3b4
Use handler with tracer
satyamz a1fbcc9
Update instrumentation for friendbot
satyamz e233146
Update utils/tracer/tracer.go
satyamz fa8162c
Update services/friendbot/main.go
satyamz 446c59c
Remove TODO from version
satyamz 139d73c
Add condition if tx is not success
satyamz 6b95c11
Update friendbot package with otel modifications
satyamz 5eefe39
Remove t.context until go 1.24
satyamz efefde8
Remove t.context from tests
satyamz 8eb2292
Update context handling in tests and remove mux params
satyamz 9194d20
Update tracer name in friendbot minion
satyamz 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,3 @@ num_minions = 1000 | |
base_fee = 100000 | ||
minion_batch_size = 50 | ||
submit_tx_retries_allowed = 5 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,103 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/stellar/go/protocols/horizon" | ||
"github.com/stellar/go/strkey" | ||
"github.com/stellar/go/support/render/hal" | ||
"github.com/stellar/go/support/render/problem" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const ( | ||
// Tracer name for friendbot service | ||
tracerName = "stellar-friendbot" | ||
) | ||
|
||
// FriendbotHandler causes an account at `Address` to be created. | ||
type FriendbotHandler struct { | ||
Friendbot *Bot | ||
tracer trace.Tracer | ||
} | ||
|
||
// NewFriendbotHandler returns friendbot handler based on the tracing enabled | ||
func NewFriendbotHandler(fb *Bot) *FriendbotHandler { | ||
tracer := otel.Tracer(tracerName) | ||
return &FriendbotHandler{ | ||
Friendbot: fb, | ||
tracer: tracer, | ||
} | ||
|
||
} | ||
|
||
// Handle is a method that implements http.HandlerFunc | ||
func (handler *FriendbotHandler) Handle(w http.ResponseWriter, r *http.Request) { | ||
result, err := handler.doHandle(r) | ||
ctx, span := handler.tracer.Start(r.Context(), "friendbot.init_http_request") | ||
defer span.End() | ||
|
||
// Add request attributes to span | ||
span.SetAttributes( | ||
attribute.String("http.method", r.Method), | ||
attribute.String("http.url", r.URL.String()), | ||
attribute.String("http.user_agent", r.UserAgent()), | ||
) | ||
|
||
result, err := handler.doHandle(ctx, r) | ||
if err != nil { | ||
problem.Render(r.Context(), w, err) | ||
span.SetStatus(codes.Error, err.Error()) | ||
return | ||
} | ||
|
||
span.SetStatus(codes.Ok, codes.Ok.String()) | ||
hal.Render(w, *result) | ||
} | ||
|
||
// doHandle is just a convenience method that returns the object to be rendered | ||
func (handler *FriendbotHandler) doHandle(r *http.Request) (*horizon.Transaction, error) { | ||
func (handler *FriendbotHandler) doHandle(ctx context.Context, r *http.Request) (*horizon.Transaction, error) { | ||
ctx, span := handler.tracer.Start(ctx, "friendbot.parse_http_request") | ||
defer span.End() | ||
err := r.ParseForm() | ||
if err != nil { | ||
p := problem.BadRequest | ||
p.Detail = "Request parameters are not escaped or incorrectly formatted." | ||
span.SetStatus(codes.Error, err.Error()) | ||
return nil, &p | ||
} | ||
|
||
address, err := handler.loadAddress(r) | ||
address, err := handler.loadAddress(ctx, r) | ||
if err != nil { | ||
span.SetStatus(codes.Error, err.Error()) | ||
return nil, problem.MakeInvalidFieldProblem("addr", err) | ||
} | ||
return handler.Friendbot.Pay(address) | ||
span.SetStatus(codes.Ok, codes.Ok.String()) | ||
return handler.Friendbot.Pay(ctx, address) | ||
} | ||
|
||
func (handler *FriendbotHandler) loadAddress(r *http.Request) (string, error) { | ||
func (handler *FriendbotHandler) loadAddress(ctx context.Context, r *http.Request) (string, error) { | ||
_, span := handler.tracer.Start(ctx, "minion.destination_address") | ||
defer span.End() | ||
|
||
address := r.Form.Get("addr") | ||
if address == "" { | ||
span.SetStatus(codes.Error, "missing destination account address") | ||
span.SetAttributes(attribute.String("error.type", "missing_parameter")) | ||
} | ||
|
||
unescaped, err := url.QueryUnescape(address) | ||
if err != nil { | ||
span.SetStatus(codes.Error, err.Error()) | ||
return unescaped, err | ||
} | ||
|
||
_, err = strkey.Decode(strkey.VersionByteAccountID, unescaped) | ||
span.SetAttributes(attribute.String("destination.account", address)) | ||
span.SetStatus(codes.Ok, codes.Ok.String()) | ||
return unescaped, err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.