Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Enrich Zipkin db tracing
Browse files Browse the repository at this point in the history
Add info regarding

- database type and address
- approximation for queries results size
  • Loading branch information
embs committed Nov 27, 2017
1 parent a7ea691 commit c8496f7
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 111 deletions.
28 changes: 10 additions & 18 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MakeLoginEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(loginRequest)
u, err := s.Login(req.Username, req.Password)
u, err := s.Login(ctx, req.Username, req.Password)
return userResponse{User: u}, err
}
}
Expand All @@ -66,7 +66,7 @@ func MakeRegisterEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(registerRequest)
id, err := s.Register(req.Username, req.Password, req.Email, req.FirstName, req.LastName)
id, err := s.Register(ctx, req.Username, req.Password, req.Email, req.FirstName, req.LastName)
return postResponse{ID: id}, err
}
}
Expand All @@ -81,9 +81,7 @@ func MakeUserGetEndpoint(s Service) endpoint.Endpoint {

req := request.(GetRequest)

userspan := stdopentracing.StartSpan("users from db", stdopentracing.ChildOf(span.Context()))
usrs, err := s.GetUsers(req.ID)
userspan.Finish()
usrs, err := s.GetUsers(ctx, req.ID)
if req.ID == "" {
return EmbedStruct{usersResponse{Users: usrs}}, err
}
Expand All @@ -97,9 +95,7 @@ func MakeUserGetEndpoint(s Service) endpoint.Endpoint {
return users.User{}, err
}
user := usrs[0]
attrspan := stdopentracing.StartSpan("attributes from db", stdopentracing.ChildOf(span.Context()))
db.GetUserAttributes(&user)
attrspan.Finish()
db.GetUserAttributes(ctx, &user)
if req.Attr == "addresses" {
return EmbedStruct{addressesResponse{Addresses: user.Addresses}}, err
}
Expand All @@ -118,7 +114,7 @@ func MakeUserPostEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(users.User)
id, err := s.PostUser(req)
id, err := s.PostUser(ctx, req)
return postResponse{ID: id}, err
}
}
Expand All @@ -131,9 +127,7 @@ func MakeAddressGetEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(GetRequest)
addrspan := stdopentracing.StartSpan("addresses from db", stdopentracing.ChildOf(span.Context()))
adds, err := s.GetAddresses(req.ID)
addrspan.Finish()
adds, err := s.GetAddresses(ctx, req.ID)
if req.ID == "" {
return EmbedStruct{addressesResponse{Addresses: adds}}, err
}
Expand All @@ -152,7 +146,7 @@ func MakeAddressPostEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(addressPostRequest)
id, err := s.PostAddress(req.Address, req.UserID)
id, err := s.PostAddress(ctx, req.Address, req.UserID)
return postResponse{ID: id}, err
}
}
Expand All @@ -165,9 +159,7 @@ func MakeCardGetEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(GetRequest)
cardspan := stdopentracing.StartSpan("addresses from db", stdopentracing.ChildOf(span.Context()))
cards, err := s.GetCards(req.ID)
cardspan.Finish()
cards, err := s.GetCards(ctx, req.ID)
if req.ID == "" {
return EmbedStruct{cardsResponse{Cards: cards}}, err
}
Expand All @@ -186,7 +178,7 @@ func MakeCardPostEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(cardPostRequest)
id, err := s.PostCard(req.Card, req.UserID)
id, err := s.PostCard(ctx, req.Card, req.UserID)
return postResponse{ID: id}, err
}
}
Expand All @@ -199,7 +191,7 @@ func MakeDeleteEndpoint(s Service) endpoint.Endpoint {
span.SetTag("service", "user")
defer span.Finish()
req := request.(deleteRequest)
err = s.Delete(req.Entity, req.ID)
err = s.Delete(ctx, req.Entity, req.ID)
if err == nil {
return statusResponse{Status: true}, err
}
Expand Down
73 changes: 37 additions & 36 deletions api/middlewares.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"time"

"github.com/go-kit/kit/log"
Expand All @@ -26,17 +27,17 @@ type loggingMiddleware struct {
logger log.Logger
}

func (mw loggingMiddleware) Login(username, password string) (user users.User, err error) {
func (mw loggingMiddleware) Login(ctx context.Context, username, password string) (user users.User, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Login",
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Login(username, password)
return mw.next.Login(ctx, username, password)
}

func (mw loggingMiddleware) Register(username, password, email, first, last string) (string, error) {
func (mw loggingMiddleware) Register(ctx context.Context, username, password, email, first, last string) (string, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Register",
Expand All @@ -45,10 +46,10 @@ func (mw loggingMiddleware) Register(username, password, email, first, last stri
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Register(username, password, email, first, last)
return mw.next.Register(ctx, username, password, email, first, last)
}

func (mw loggingMiddleware) PostUser(user users.User) (id string, err error) {
func (mw loggingMiddleware) PostUser(ctx context.Context, user users.User) (id string, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "PostUser",
Expand All @@ -58,10 +59,10 @@ func (mw loggingMiddleware) PostUser(user users.User) (id string, err error) {
"took", time.Since(begin),
)
}(time.Now())
return mw.next.PostUser(user)
return mw.next.PostUser(ctx, user)
}

func (mw loggingMiddleware) GetUsers(id string) (u []users.User, err error) {
func (mw loggingMiddleware) GetUsers(ctx context.Context, id string) (u []users.User, err error) {
defer func(begin time.Time) {
who := id
if who == "" {
Expand All @@ -74,10 +75,10 @@ func (mw loggingMiddleware) GetUsers(id string) (u []users.User, err error) {
"took", time.Since(begin),
)
}(time.Now())
return mw.next.GetUsers(id)
return mw.next.GetUsers(ctx, id)
}

func (mw loggingMiddleware) PostAddress(add users.Address, id string) (string, error) {
func (mw loggingMiddleware) PostAddress(ctx context.Context, add users.Address, id string) (string, error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "PostAddress",
Expand All @@ -87,10 +88,10 @@ func (mw loggingMiddleware) PostAddress(add users.Address, id string) (string, e
"took", time.Since(begin),
)
}(time.Now())
return mw.next.PostAddress(add, id)
return mw.next.PostAddress(ctx, add, id)
}

func (mw loggingMiddleware) GetAddresses(id string) (a []users.Address, err error) {
func (mw loggingMiddleware) GetAddresses(ctx context.Context, id string) (a []users.Address, err error) {
defer func(begin time.Time) {
who := id
if who == "" {
Expand All @@ -103,10 +104,10 @@ func (mw loggingMiddleware) GetAddresses(id string) (a []users.Address, err erro
"took", time.Since(begin),
)
}(time.Now())
return mw.next.GetAddresses(id)
return mw.next.GetAddresses(ctx, id)
}

func (mw loggingMiddleware) PostCard(card users.Card, id string) (string, error) {
func (mw loggingMiddleware) PostCard(ctx context.Context, card users.Card, id string) (string, error) {
defer func(begin time.Time) {
cc := card
cc.MaskCC()
Expand All @@ -117,10 +118,10 @@ func (mw loggingMiddleware) PostCard(card users.Card, id string) (string, error)
"took", time.Since(begin),
)
}(time.Now())
return mw.next.PostCard(card, id)
return mw.next.PostCard(ctx, card, id)
}

func (mw loggingMiddleware) GetCards(id string) (a []users.Card, err error) {
func (mw loggingMiddleware) GetCards(ctx context.Context, id string) (a []users.Card, err error) {
defer func(begin time.Time) {
who := id
if who == "" {
Expand All @@ -133,10 +134,10 @@ func (mw loggingMiddleware) GetCards(id string) (a []users.Card, err error) {
"took", time.Since(begin),
)
}(time.Now())
return mw.next.GetCards(id)
return mw.next.GetCards(ctx, id)
}

func (mw loggingMiddleware) Delete(entity, id string) (err error) {
func (mw loggingMiddleware) Delete(ctx context.Context, entity, id string) (err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Delete",
Expand All @@ -145,7 +146,7 @@ func (mw loggingMiddleware) Delete(entity, id string) (err error) {
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Delete(entity, id)
return mw.next.Delete(ctx, entity, id)
}

func (mw loggingMiddleware) Health() (health []Health) {
Expand Down Expand Up @@ -174,85 +175,85 @@ func NewInstrumentingService(requestCount metrics.Counter, requestLatency metric
}
}

func (s *instrumentingService) Login(username, password string) (users.User, error) {
func (s *instrumentingService) Login(ctx context.Context, username, password string) (users.User, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "login").Add(1)
s.requestLatency.With("method", "login").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Login(username, password)
return s.Service.Login(ctx, username, password)
}

func (s *instrumentingService) Register(username, password, email, first, last string) (string, error) {
func (s *instrumentingService) Register(ctx context.Context, username, password, email, first, last string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "register").Add(1)
s.requestLatency.With("method", "register").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Register(username, password, email, first, last)
return s.Service.Register(ctx, username, password, email, first, last)
}

func (s *instrumentingService) PostUser(user users.User) (string, error) {
func (s *instrumentingService) PostUser(ctx context.Context, user users.User) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postUser").Add(1)
s.requestLatency.With("method", "postUser").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostUser(user)
return s.Service.PostUser(ctx, user)
}

func (s *instrumentingService) GetUsers(id string) (u []users.User, err error) {
func (s *instrumentingService) GetUsers(ctx context.Context, id string) (u []users.User, err error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getUsers").Add(1)
s.requestLatency.With("method", "getUsers").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetUsers(id)
return s.Service.GetUsers(ctx, id)
}

func (s *instrumentingService) PostAddress(add users.Address, id string) (string, error) {
func (s *instrumentingService) PostAddress(ctx context.Context, add users.Address, id string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postAddress").Add(1)
s.requestLatency.With("method", "postAddress").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostAddress(add, id)
return s.Service.PostAddress(ctx, add, id)
}

func (s *instrumentingService) GetAddresses(id string) ([]users.Address, error) {
func (s *instrumentingService) GetAddresses(ctx context.Context, id string) ([]users.Address, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getAddresses").Add(1)
s.requestLatency.With("method", "getAddresses").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetAddresses(id)
return s.Service.GetAddresses(ctx, id)
}

func (s *instrumentingService) PostCard(card users.Card, id string) (string, error) {
func (s *instrumentingService) PostCard(ctx context.Context, card users.Card, id string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postCard").Add(1)
s.requestLatency.With("method", "postCard").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostCard(card, id)
return s.Service.PostCard(ctx, card, id)
}

func (s *instrumentingService) GetCards(id string) ([]users.Card, error) {
func (s *instrumentingService) GetCards(ctx context.Context, id string) ([]users.Card, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getCards").Add(1)
s.requestLatency.With("method", "getCards").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetCards(id)
return s.Service.GetCards(ctx, id)
}

func (s *instrumentingService) Delete(entity, id string) error {
func (s *instrumentingService) Delete(ctx context.Context, entity, id string) error {
defer func(begin time.Time) {
s.requestCount.With("method", "delete").Add(1)
s.requestLatency.With("method", "delete").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Delete(entity, id)
return s.Service.Delete(ctx, entity, id)
}

func (s *instrumentingService) Health() []Health {
Expand Down
Loading

0 comments on commit c8496f7

Please sign in to comment.