Skip to content

Commit

Permalink
fix: increase sample rate and add capture error and added it to most …
Browse files Browse the repository at this point in the history
…error bacllbacks
  • Loading branch information
bubbajoe committed Jun 22, 2024
1 parent 0ab9f6e commit 0a54dae
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 53 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/clarkmcc/go-typescript v0.7.0
github.com/dgate-io/chi-router v0.0.0-20231217131951-d154152d5115
github.com/dgate-io/chi-router v0.0.0-20240610012144-c4aae03990a6
github.com/dgraph-io/badger/v4 v4.2.0
github.com/dop251/goja v0.0.0-20240220182346-e401ed450204
github.com/dop251/goja_nodejs v0.0.0-20231122114759-e84d9a924c5c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgate-io/chi-router v0.0.0-20231217131951-d154152d5115 h1:AVEnGd1UBqJU7MnbyAtPfp47mlI5GvMS4fFNZVMS0KA=
github.com/dgate-io/chi-router v0.0.0-20231217131951-d154152d5115/go.mod h1:MyLj6L03q1t8GW/541pHuP6co58QfLppSYPS0PvLtC8=
github.com/dgate-io/chi-router v0.0.0-20240610012144-c4aae03990a6 h1:8zUtyQlZm7t1lmoaK8/s6GItIAF1mORkaHGXSU/r4cI=
github.com/dgate-io/chi-router v0.0.0-20240610012144-c4aae03990a6/go.mod h1:MyLj6L03q1t8GW/541pHuP6co58QfLppSYPS0PvLtC8=
github.com/dgraph-io/badger/v4 v4.2.0 h1:kJrlajbXXL9DFTNuhhu9yCx7JJa4qpYWxtE8BzuWsEs=
github.com/dgraph-io/badger/v4 v4.2.0/go.mod h1:qfCqhPoWDFJRx1gp5QwwyGo8xk1lbHUxvK9nK0OGAak=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
Expand Down
17 changes: 9 additions & 8 deletions internal/admin/routes/collection_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,16 @@ func ConfigureCollectionAPI(server chi.Router, logger *zap.Logger, cs changestat
return
}
if collection.Type == spec.CollectionTypeDocument && collection.Schema != nil {
err := collection.Schema.Validate(payloadData)

if err != nil {
verrs := err.(*jsonschema.ValidationError)
validationErrs := make([]string, len(verrs.Causes))
for i, ve := range verrs.Causes {
validationErrs[i] = ve.Error()
if err := collection.Schema.Validate(payloadData); err != nil {
if verrs, ok := err.(*jsonschema.ValidationError); ok {
errs := make([]error, len(verrs.Causes))
for i, ve := range verrs.Causes {
errs[i] = ve
}
util.JsonErrors(w, http.StatusBadRequest, errs)
} else {
util.JsonError(w, http.StatusBadRequest, err.Error())
}
util.JsonErrors(w, http.StatusBadRequest, validationErrs)
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/extensions/telemetry/no_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import "github.com/dgate-io/dgate/internal/config"
func SetupTelemetry(conf *config.DGateConfig) func() {
return func() {}
}

func CaptureError(err error) {}
15 changes: 10 additions & 5 deletions internal/extensions/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ func SetupTelemetry(name string, version string) func() {
"version": version,
}
if err := sentry.Init(sentry.ClientOptions{
Dsn: dsn,
Debug: false,
EnableTracing: true,
Release: fmt.Sprint(name, "@", version),
Tags: finalTags,
Dsn: dsn,
Debug: false,
EnableTracing: true,
TracesSampleRate: 1.0,
Release: fmt.Sprint(name, "@", version),
Tags: finalTags,
}); err != nil {
zap.L().Error("sentry.Init failed", zap.Error(err))
return func() {}
Expand All @@ -46,3 +47,7 @@ func SetupTelemetry(name string, version string) func() {
sentry.Flush(2 * time.Second)
}
}

func CaptureError(err error) {
sentry.CaptureException(err)
}
74 changes: 38 additions & 36 deletions internal/proxy/proxy_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"time"

"github.com/dgate-io/dgate/internal/config"
"github.com/dgate-io/dgate/internal/extensions/telemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
api "go.opentelemetry.io/otel/metric"
)

type ProxyMetrics struct {
type Telemetry struct {
resolveNamespaceDurInstrument api.Float64Histogram
resolveCertDurInstrument api.Float64Histogram
proxyDurInstrument api.Float64Histogram
Expand All @@ -21,11 +22,11 @@ type ProxyMetrics struct {
errorCountInstrument api.Int64Counter
}

func NewProxyMetrics() *ProxyMetrics {
return &ProxyMetrics{}
func NewProxyMetrics() *Telemetry {
return &Telemetry{}
}

func (pm *ProxyMetrics) Setup(config *config.DGateConfig) {
func (tm *Telemetry) Setup(config *config.DGateConfig) {
if config.DisableMetrics {
return
}
Expand All @@ -41,28 +42,28 @@ func (pm *ProxyMetrics) Setup(config *config.DGateConfig) {
},
))

pm.resolveNamespaceDurInstrument, _ = meter.Float64Histogram(
tm.resolveNamespaceDurInstrument, _ = meter.Float64Histogram(
"resolve_namespace_duration", api.WithUnit("us"))
pm.resolveCertDurInstrument, _ = meter.Float64Histogram(
tm.resolveCertDurInstrument, _ = meter.Float64Histogram(
"resolve_cert_duration", api.WithUnit("ms"))
pm.proxyDurInstrument, _ = meter.Float64Histogram(
tm.proxyDurInstrument, _ = meter.Float64Histogram(
"request_duration", api.WithUnit("ms"))
pm.moduleDurInstrument, _ = meter.Float64Histogram(
tm.moduleDurInstrument, _ = meter.Float64Histogram(
"module_duration", api.WithUnit("ms"))
pm.upstreamDurInstrument, _ = meter.Float64Histogram(
tm.upstreamDurInstrument, _ = meter.Float64Histogram(
"upstream_duration", api.WithUnit("ms"))
pm.proxyCountInstrument, _ = meter.Int64Counter(
tm.proxyCountInstrument, _ = meter.Int64Counter(
"request_count")
pm.moduleRunCountInstrument, _ = meter.Int64Counter(
tm.moduleRunCountInstrument, _ = meter.Int64Counter(
"module_executions")
pm.errorCountInstrument, _ = meter.Int64Counter(
tm.errorCountInstrument, _ = meter.Int64Counter(
"error_count")
}

func (pm *ProxyMetrics) MeasureProxyRequest(
func (tm *Telemetry) MeasureProxyRequest(
reqCtx *RequestContext, start time.Time,
) {
if pm.proxyDurInstrument == nil || pm.proxyCountInstrument == nil {
if tm.proxyDurInstrument == nil || tm.proxyCountInstrument == nil {
return
}
serviceAttr := attribute.NewSet()
Expand Down Expand Up @@ -91,19 +92,19 @@ func (pm *ProxyMetrics) MeasureProxyRequest(
attribute.Int("status_code", reqCtx.rw.Status()),
)

pm.proxyDurInstrument.Record(reqCtx.ctx,
tm.proxyDurInstrument.Record(reqCtx.ctx,
float64(elasped)/float64(time.Millisecond),
api.WithAttributeSet(attrSet), api.WithAttributeSet(serviceAttr))

pm.proxyCountInstrument.Add(reqCtx.ctx, 1,
tm.proxyCountInstrument.Add(reqCtx.ctx, 1,
api.WithAttributeSet(attrSet), api.WithAttributeSet(serviceAttr))
}

func (pm *ProxyMetrics) MeasureModuleDuration(
func (tm *Telemetry) MeasureModuleDuration(
reqCtx *RequestContext, moduleFunc string,
start time.Time, err error,
) {
if pm.moduleDurInstrument == nil || pm.moduleRunCountInstrument == nil {
if tm.moduleDurInstrument == nil || tm.moduleRunCountInstrument == nil {
return
}
elasped := time.Since(start)
Expand All @@ -117,21 +118,21 @@ func (pm *ProxyMetrics) MeasureModuleDuration(
attribute.String("pattern", reqCtx.pattern),
attribute.String("host", reqCtx.req.Host),
)
pm.addError(moduleFunc, err, attrSet)
tm.addError(moduleFunc, err, attrSet)

pm.moduleDurInstrument.Record(reqCtx.ctx,
tm.moduleDurInstrument.Record(reqCtx.ctx,
float64(elasped)/float64(time.Millisecond),
api.WithAttributeSet(attrSet))

pm.moduleRunCountInstrument.Add(reqCtx.ctx, 1,
tm.moduleRunCountInstrument.Add(reqCtx.ctx, 1,
api.WithAttributeSet(attrSet))
}

func (pm *ProxyMetrics) MeasureUpstreamDuration(
func (tm *Telemetry) MeasureUpstreamDuration(
reqCtx *RequestContext, start time.Time,
upstreamHost string, err error,
) {
if pm.upstreamDurInstrument == nil {
if tm.upstreamDurInstrument == nil {
return
}
elasped := time.Since(start)
Expand All @@ -146,35 +147,35 @@ func (pm *ProxyMetrics) MeasureUpstreamDuration(
attribute.String("service", reqCtx.route.Service.Name),
attribute.String("upstream_host", upstreamHost),
)
pm.addError("upstream_request", err, attrSet)
tm.addError("upstream_request", err, attrSet)

pm.upstreamDurInstrument.Record(reqCtx.ctx,
tm.upstreamDurInstrument.Record(reqCtx.ctx,
float64(elasped)/float64(time.Millisecond),
api.WithAttributeSet(attrSet))
}

func (pm *ProxyMetrics) MeasureNamespaceResolutionDuration(
func (tm *Telemetry) MeasureNamespaceResolutionDuration(
start time.Time, host, namespace string, err error,
) {
if pm.resolveNamespaceDurInstrument == nil {
if tm.resolveNamespaceDurInstrument == nil {
return
}
elasped := time.Since(start)
attrSet := attribute.NewSet(
attribute.String("host", host),
attribute.String("namespace", namespace),
)
pm.addError("namespace_resolution", err, attrSet)
tm.addError("namespace_resolution", err, attrSet)

pm.resolveNamespaceDurInstrument.Record(context.TODO(),
tm.resolveNamespaceDurInstrument.Record(context.TODO(),
float64(elasped)/float64(time.Microsecond),
api.WithAttributeSet(attrSet))
}

func (pm *ProxyMetrics) MeasureCertResolutionDuration(
func (tm *Telemetry) MeasureCertResolutionDuration(
start time.Time, host string, cache bool, err error,
) {
if pm.resolveCertDurInstrument == nil {
if tm.resolveCertDurInstrument == nil {
return
}

Expand All @@ -184,20 +185,21 @@ func (pm *ProxyMetrics) MeasureCertResolutionDuration(
attribute.String("host", host),
attribute.Bool("cache", cache),
)
pm.addError("cert_resolution", err, attrSet)
tm.addError("cert_resolution", err, attrSet)

pm.resolveCertDurInstrument.Record(context.TODO(),
tm.resolveCertDurInstrument.Record(context.TODO(),
float64(elasped)/float64(time.Millisecond),
api.WithAttributeSet(attrSet))
}

func (pm *ProxyMetrics) addError(
func (tm *Telemetry) addError(
namespace string, err error,
attrs ...attribute.Set,
) {
if pm.errorCountInstrument == nil || err == nil {
if tm.errorCountInstrument == nil || err == nil {
return
}
telemetry.CaptureError(err)
attrSet := attribute.NewSet(
attribute.String("error_value", err.Error()),
attribute.String("namespace", namespace),
Expand All @@ -210,5 +212,5 @@ func (pm *ProxyMetrics) addError(
attrSets = append(attrSets, api.WithAttributeSet(attr))
}

pm.errorCountInstrument.Add(context.TODO(), 1, attrSets...)
tm.errorCountInstrument.Add(context.TODO(), 1, attrSets...)
}
2 changes: 1 addition & 1 deletion internal/proxy/proxy_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ProxyState struct {
proxyLock *sync.RWMutex
ready *atomic.Bool
pendingChanges bool
metrics *ProxyMetrics
metrics *Telemetry

rm *resources.ResourceManager
skdr scheduler.Scheduler
Expand Down
8 changes: 6 additions & 2 deletions pkg/util/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package util

import (
"encoding/json"
"errors"
"net/http"
"reflect"

"github.com/dgate-io/dgate/internal/extensions/telemetry"
)

func isSlice(v interface{}) bool {
Expand Down Expand Up @@ -45,11 +48,12 @@ func JsonError[T any](w http.ResponseWriter, statusCode int, message T) {
})
}

func JsonErrors[T any](w http.ResponseWriter, statusCode int, message []T) {
func JsonErrors(w http.ResponseWriter, statusCode int, errs []error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
telemetry.CaptureError(errors.Join(errs...))
json.NewEncoder(w).Encode(map[string]any{
"errors": message,
"errors": errs,
"status": statusCode,
})
}

0 comments on commit 0a54dae

Please sign in to comment.