Skip to content

Commit

Permalink
Update opencensus and related dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Jan 31, 2019
1 parent ce9efe2 commit a63a2b2
Show file tree
Hide file tree
Showing 3,348 changed files with 2,337,812 additions and 3,928 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
83 changes: 76 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ignored = ["go.opencensus.io/exporter/stackdriver"]

[[constraint]]
name = "go.uber.org/zap"
version = "~1.9.1"
Expand Down Expand Up @@ -70,9 +72,9 @@
name = "github.com/prometheus/client_golang"
revision = "bcbbc08eb2ddff3af83bbf11e7ec13b4fd730b6e"

[[constraint]]
[[override]]
name = "go.opencensus.io"
version = "~0.14.0"
version = "~0.19.0"

[[constraint]]
name = "github.com/stretchr/testify"
Expand All @@ -89,3 +91,7 @@
[[constraint]]
name = "gopkg.in/natefinch/lumberjack.v2"
version = "2.1.0"

[[constraint]]
name = "contrib.go.opencensus.io/exporter/stackdriver"
version = "0.9.1"
43 changes: 43 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"database/sql"
"encoding/base64"
"fmt"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -454,3 +457,43 @@ func extractClientAddress(logger *zap.Logger, ctx context.Context) (string, stri

return clientIP, clientPort
}

func traceApiBefore(ctx context.Context, logger *zap.Logger, fullMethodName string, fn func(clientIP, clientPort string) error) error {
name := fmt.Sprintf("%v-before", fullMethodName)
clientIP, clientPort := extractClientAddress(logger, ctx)
statsCtx, err := tag.New(ctx, tag.Upsert(MetricsFunction, name))
if err != nil {
// If there was an error processing the stats, just execute the function.
logger.Warn("Error tagging API before stats", zap.String("full_method_name", fullMethodName), zap.Error(err))
clientIP, clientPort := extractClientAddress(logger, ctx)
return fn(clientIP, clientPort)
}
startNanos := time.Now().UTC().UnixNano()
statsCtx, span := trace.StartSpan(statsCtx, name)

err = fn(clientIP, clientPort)

span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))

return err
}

func traceApiAfter(ctx context.Context, logger *zap.Logger, fullMethodName string, fn func(clientIP, clientPort string)) {
name := fmt.Sprintf("%v-after", logger)
clientIP, clientPort := extractClientAddress(logger, ctx)
statsCtx, err := tag.New(ctx, tag.Upsert(MetricsFunction, name))
if err != nil {
// If there was an error processing the stats, just execute the function.
logger.Warn("Error tagging API after stats", zap.String("full_method_name", fullMethodName), zap.Error(err))
fn(clientIP, clientPort)
return
}
startNanos := time.Now().UTC().UnixNano()
statsCtx, span := trace.StartSpan(statsCtx, name)

fn(clientIP, clientPort)

span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
109 changes: 41 additions & 68 deletions server/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,35 @@
package server

import (
"fmt"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/heroiclabs/nakama/api"
"github.com/lib/pq"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"time"
)

func (s *ApiServer) GetAccount(ctx context.Context, in *empty.Empty) (*api.Account, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)

// Before hook.
if fn := s.runtime.BeforeGetAccount(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})

// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort)
if err != nil {
return nil, status.Error(code, err.Error())
beforeFn := func(clientIP, clientPort string) error {
err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort)
if err != nil {
return status.Error(code, err.Error())
}
// Empty input never overridden.
return nil
}
// Empty input never overridden.

// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
// Execute the before function lambda wrapped in a trace for stats measurement.
err := traceApiBefore(ctx, s.logger, ctx.Value(ctxFullMethodKey{}).(string), beforeFn)
if err != nil {
return nil, err
}
}

user, err := GetAccount(ctx, s.logger, s.db, s.tracker, userID)
Expand All @@ -65,19 +56,12 @@ func (s *ApiServer) GetAccount(ctx context.Context, in *empty.Empty) (*api.Accou

// After hook.
if fn := s.runtime.AfterGetAccount(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})

// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, user)

// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
afterFn := func(clientIP, clientPort string) {
fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, user)
}

// Execute the after function lambda wrapped in a trace for stats measurement.
traceApiAfter(ctx, s.logger, ctx.Value(ctxFullMethodKey{}).(string), afterFn)
}

return user, nil
Expand All @@ -88,29 +72,25 @@ func (s *ApiServer) UpdateAccount(ctx context.Context, in *api.UpdateAccountRequ

// Before hook.
if fn := s.runtime.BeforeUpdateAccount(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})

// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
beforeFn := func(clientIP, clientPort string) error {
result, err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", ctx.Value(ctxFullMethodKey{}).(string)), zap.String("uid", userID.String()))
return status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
return nil
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", userID.String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result

// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
// Execute the before function lambda wrapped in a trace for stats measurement.
err := traceApiBefore(ctx, s.logger, ctx.Value(ctxFullMethodKey{}).(string), beforeFn)
if err != nil {
return nil, err
}
}

username := in.GetUsername().GetValue()
Expand All @@ -130,19 +110,12 @@ func (s *ApiServer) UpdateAccount(ctx context.Context, in *api.UpdateAccountRequ

// After hook.
if fn := s.runtime.AfterUpdateAccount(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})

// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)

// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
afterFn := func(clientIP, clientPort string) {
fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
}

// Execute the after function lambda wrapped in a trace for stats measurement.
traceApiAfter(ctx, s.logger, ctx.Value(ctxFullMethodKey{}).(string), afterFn)
}

return &empty.Empty{}, nil
Expand Down
Loading

0 comments on commit a63a2b2

Please sign in to comment.