Skip to content

Commit

Permalink
feat: allow controlling of StartCall info logging from a service runt…
Browse files Browse the repository at this point in the history
…ime (#422)
  • Loading branch information
PeSme authored Oct 8, 2024
1 parent 0a77019 commit bac5b27
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var httpCallLatency = promauto.NewHistogramVec( // nolint:gochecknoglobals // Wh
Help: "The latency of the HTTP request, in seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"app", "call", "statuscode", "statuscategory", "kind"}, // Labels
[]string{"target", "call", "statuscode", "statuscategory", "kind"}, // Labels
)

// ReportHTTPLatency reports the http_request_handled metric for a request.
Expand Down Expand Up @@ -90,7 +90,7 @@ var grpcCallLatency = promauto.NewHistogramVec( // nolint:gochecknoglobals // Wh
Help: "The latency of the gRPC request, in seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"app", "call", "statuscode", "statuscategory", "kind"}, // Labels
[]string{"target", "call", "statuscode", "statuscategory", "kind"}, // Labels
)

// ReportGRPCLatency reports the grpc_request_handled metric for a request.
Expand Down Expand Up @@ -121,7 +121,7 @@ var outboundCallLatency = promauto.NewHistogramVec( // nolint:gochecknoglobals /
Help: "The latency of the outbound request, in seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"app", "call", "statuscode", "statuscategory", "kind"}, // Labels
[]string{"target", "call", "statuscode", "statuscategory", "kind"}, // Labels
)

// ReportOutboundLatency reports the outbound_call_seconds metric for a request.
Expand Down
14 changes: 13 additions & 1 deletion pkg/trace/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,19 @@ func SetCustomCallKind(ctx context.Context, ck metrics.CallKind) {
// GetCallName returns name of the current call
func GetCallName(ctx context.Context) string {
info := callTracker.Info(ctx)
return info.Name
if info != nil {
return info.Name
}
return ""
}

// IsInfoLoggingEnabled returns state of info logging
func IsInfoLoggingEnabled(ctx context.Context) bool {
info := callTracker.Info(ctx)
if info != nil {
return info.Opts.EnableInfoLogging
}
return false
}

// EndCall calculates the duration of the call, writes to metrics,
Expand Down
2 changes: 1 addition & 1 deletion pkg/trace/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ func TestReportLatencyMetrics(t *testing.T) {
}, protocmp.Transform()))

assert.Check(t, cmp.Equal(metric.GetLabel(), []*dto.LabelPair{
{Name: proto.String("app"), Value: proto.String("gobox")},
{Name: proto.String("call"), Value: proto.String("test")},
{Name: proto.String("kind"), Value: proto.String("internal")},
{Name: proto.String("statuscategory"), Value: proto.String("CategoryOK")},
{Name: proto.String("statuscode"), Value: proto.String("OK")},
{Name: proto.String("target"), Value: proto.String("gobox")},
}, protocmp.Transform()))
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/trace/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Outreach Corporation. All Rights Reserved.

// Description: This file contains constants and tools for controlling trace.Start/EndCall logging

package trace

import (
"context"

"github.com/getoutreach/gobox/pkg/log"
)

type InfoLoggingResolved int32

const (
InfoLoggingDefault InfoLoggingResolved = 0
InfoLoggingEnabled InfoLoggingResolved = 1
InfoLoggingDisabled InfoLoggingResolved = 2
)

type InfoLoggingResolver = func(ctx context.Context, operation string) InfoLoggingResolved

// ResolvedLogging returns signals trace.EndCall whether to enable/disable info logging
func ResolvedLogging(logging InfoLoggingResolved) log.Marshaler {
if logging == InfoLoggingDefault {
return nil
}
if logging == InfoLoggingEnabled {
return WithInfoLoggingEnabled()
}
return WithInfoLoggingDisabled()
}

func ReevaluateLogging(ctx context.Context, resolver InfoLoggingResolver) {
logging := resolver(ctx, GetCallName(ctx))
if logging == InfoLoggingDefault {
return
}
callInfo := callTracker.Info(ctx)
callInfo.Opts.EnableInfoLogging = logging == InfoLoggingEnabled
}

0 comments on commit bac5b27

Please sign in to comment.