chore: add traces to peerfinder

This commit is contained in:
Jon Lundy 2022-10-25 20:37:59 -06:00
parent 9dd9443bc9
commit 5bf052580f
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
2 changed files with 41 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package peerfinder
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"net/netip" "net/netip"
"strconv" "strconv"
"time" "time"
@ -123,6 +124,9 @@ func (e *Result) MarshalBinary() (text []byte, err error) {
func (e *Result) UnmarshalBinary(b []byte) error { func (e *Result) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e) return json.Unmarshal(b, e)
} }
func (e *Result) String() string {
return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency)
}
type Info struct { type Info struct {
ScriptVersion string `json:"script_version"` ScriptVersion string `json:"script_version"`

View File

@ -4,13 +4,15 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"net"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
ulid "github.com/oklog/ulid/v2"
contentnegotiation "gitlab.com/jamietanna/content-negotiation-go" contentnegotiation "gitlab.com/jamietanna/content-negotiation-go"
"go.opentelemetry.io/otel/attribute"
"github.com/oklog/ulid"
"github.com/sour-is/ev/internal/lg" "github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es" "github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event" "github.com/sour-is/ev/pkg/es/event"
@ -88,11 +90,13 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (s *service) getPending(w http.ResponseWriter, r *http.Request, uuid string) { func (s *service) getPending(w http.ResponseWriter, r *http.Request, uuid string) {
ctx := r.Context() ctx, span := lg.Span(r.Context())
_, span := lg.Span(ctx)
defer span.End() defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
info, err := es.Upsert(ctx, s.es, "pf-info", func(ctx context.Context, agg *Info) error { info, err := es.Upsert(ctx, s.es, "pf-info", func(ctx context.Context, agg *Info) error {
return agg.OnCreate() // initialize if not exists return agg.OnCreate() // initialize if not exists
}) })
@ -155,7 +159,12 @@ func (s *service) getPending(w http.ResponseWriter, r *http.Request, uuid string
span.RecordError(err) span.RecordError(err)
} }
func (s *service) getResults(w http.ResponseWriter, r *http.Request, uuid string) { func (s *service) getResults(w http.ResponseWriter, r *http.Request, uuid string) {
ctx := r.Context() ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
responses, err := s.es.Read(ctx, queueResponses+uuid, -1, es.AllEvents) responses, err := s.es.Read(ctx, queueResponses+uuid, -1, es.AllEvents)
if err != nil { if err != nil {
@ -177,17 +186,28 @@ func (s *service) getResults(w http.ResponseWriter, r *http.Request, uuid string
} }
} }
func (s *service) postRequest(w http.ResponseWriter, r *http.Request) { func (s *service) postRequest(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx, span := lg.Span(r.Context())
defer span.End()
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) w.WriteHeader(http.StatusUnprocessableEntity)
return return
} }
req := &Request{ ip := net.ParseIP(r.Form.Get("req_ip"))
RequestIP: r.Form.Get("req_ip"), if ip == nil {
w.WriteHeader(http.StatusBadRequest)
return
} }
req := &Request{
RequestIP: ip.String(),
}
span.SetAttributes(
attribute.Stringer("req_ip", ip),
)
if hidden, err := strconv.ParseBool(r.Form.Get("req_hidden")); err != nil { if hidden, err := strconv.ParseBool(r.Form.Get("req_hidden")); err != nil {
req.Hidden = hidden req.Hidden = hidden
} }
@ -195,7 +215,12 @@ func (s *service) postRequest(w http.ResponseWriter, r *http.Request) {
s.es.Append(ctx, queueRequests, event.NewEvents(req)) s.es.Append(ctx, queueRequests, event.NewEvents(req))
} }
func (s *service) postResult(w http.ResponseWriter, r *http.Request, id string) { func (s *service) postResult(w http.ResponseWriter, r *http.Request, id string) {
ctx := r.Context() ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("id", id),
)
if _, err := ulid.ParseStrict(id); err != nil { if _, err := ulid.ParseStrict(id); err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@ -219,6 +244,9 @@ func (s *service) postResult(w http.ResponseWriter, r *http.Request, id string)
PeerVersion: r.Form.Get("peer_version"), PeerVersion: r.Form.Get("peer_version"),
Latency: latency, Latency: latency,
} }
span.SetAttributes(
attribute.Stringer("result", req),
)
s.es.Append(ctx, queueResponses+id, event.NewEvents(req)) s.es.Append(ctx, queueResponses+id, event.NewEvents(req))
} }