-
Notifications
You must be signed in to change notification settings - Fork 528
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
8
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
8 commits
Select commit
Hold shift + click to select a range
66d5939
Add stellar tracer to utils package
satyamz f4ed738
Add otelchi middleware for the go-chi middle
satyamz 5d64356
Update go-chi package version
satyamz 40e0e23
Add instrumentation for friendbot handler function
satyamz c1d6b06
Add insecure flag for exporter
satyamz dd0155b
Use handler with tracer
satyamz ea410df
Update instrumentation for friendbot
satyamz b6e4969
Merge branch 'master' into friendbot-instrumentation-03
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
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.
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 condition sets span status to error but doesn't return early, allowing execution to continue with an empty address which will likely cause issues downstream. The function should return an error when address is empty.
Copilot uses AI. Check for mistakes.