Skip to content

Commit

Permalink
* Renamed {retry,table}.WithID option to {retry,table}.WithLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Oct 24, 2023
1 parent 43bf888 commit b7964b9
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 236 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Renamed `{retry,table}.WithID` option to `{retry,table}.WithLabel`
* Added `ydb.WithTraceRetry` option
* Moved `internal/allocator.Buffers` to package `internal/xstring`
* Bumped `golang.org/x/sync` to `v0.3.0`
Expand Down
10 changes: 1 addition & 9 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ require (
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.10.2
github.com/prometheus/client_golang v1.13.0
github.com/ydb-platform/gorm-driver v0.0.5
github.com/ydb-platform/ydb-go-sdk-auth-environ v0.1.2
github.com/ydb-platform/ydb-go-sdk-prometheus v0.11.10
github.com/ydb-platform/ydb-go-sdk/v3 v3.47.3
github.com/ydb-platform/ydb-go-yc v0.10.1
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54
Expand All @@ -23,8 +21,6 @@ require (

require (
cloud.google.com/go/firestore v1.9.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
Expand All @@ -40,17 +36,12 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/yandex-cloud/go-genproto v0.0.0-20220815090733-4c139c0154e2 // indirect
github.com/ydb-platform/ydb-go-genproto v0.0.0-20231012155159-f85a672542fd // indirect
github.com/ydb-platform/ydb-go-sdk-metrics v0.16.3 // indirect
github.com/ydb-platform/ydb-go-yc-metadata v0.5.4 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.9.0 // indirect
Expand All @@ -63,6 +54,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.36.3 // indirect
modernc.org/ccgo/v3 v3.16.9 // indirect
Expand Down
47 changes: 0 additions & 47 deletions examples/go.sum

Large diffs are not rendered by default.

137 changes: 10 additions & 127 deletions examples/serverless/url_shortener/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ import (
"strings"
"sync"
"text/template"
"time"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
ydbMetrics "github.com/ydb-platform/ydb-go-sdk-prometheus"

ydb "github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

//go:embed static/index.html
Expand Down Expand Up @@ -71,76 +65,16 @@ type templateConfig struct {
}

type service struct {
db *ydb.Driver
registry *prometheus.Registry
router *mux.Router

calls *prometheus.GaugeVec
callsLatency *prometheus.HistogramVec
callsErrors *prometheus.GaugeVec
db *ydb.Driver
router *mux.Router
}

var once sync.Once

func getService(ctx context.Context, dsn string, opts ...ydb.Option) (s *service, err error) {
once.Do(func() {
var (
registry = prometheus.NewRegistry()
calls = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app",
Name: "calls",
}, []string{
"method",
"success",
})
callsLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "app",
Name: "latency",
Buckets: []float64{
(1 * time.Millisecond).Seconds(),
(5 * time.Millisecond).Seconds(),
(10 * time.Millisecond).Seconds(),
(50 * time.Millisecond).Seconds(),
(100 * time.Millisecond).Seconds(),
(500 * time.Millisecond).Seconds(),
(1000 * time.Millisecond).Seconds(),
(5000 * time.Millisecond).Seconds(),
(10000 * time.Millisecond).Seconds(),
},
}, []string{
"success",
"method",
})
callsErrors = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app",
Name: "errors",
}, []string{
"method",
})
)

registry.MustRegister(calls)
registry.MustRegister(callsLatency)
registry.MustRegister(callsErrors)

opts = append(
opts,
ydbMetrics.WithTraces(
registry,
ydbMetrics.WithSeparator("_"),
ydbMetrics.WithDetails(
trace.DetailsAll,
),
),
)

s = &service{
registry: registry,
router: mux.NewRouter(),

calls: calls,
callsLatency: callsLatency,
callsErrors: callsErrors,
router: mux.NewRouter(),
}

s.db, err = ydb.Open(ctx, dsn, opts...)
Expand All @@ -149,9 +83,6 @@ func getService(ctx context.Context, dsn string, opts ...ydb.Option) (s *service
return
}

s.router.Handle("/metrics", promhttp.InstrumentMetricHandler(
registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
))
s.router.HandleFunc("/", s.handleIndex).Methods(http.MethodGet)
s.router.HandleFunc("/shorten", s.handleShorten).Methods(http.MethodPost)
s.router.HandleFunc("/{[0-9a-fA-F]{8}}", s.handleLonger).Methods(http.MethodGet)
Expand Down Expand Up @@ -308,25 +239,9 @@ func successToString(b bool) string {

func (s *service) handleIndex(w http.ResponseWriter, r *http.Request) {
var (
err error
tpl *template.Template
start = time.Now()
err error
tpl *template.Template
)
defer func() {
if err != nil {
s.callsErrors.With(prometheus.Labels{
"method": "index",
}).Add(1)
}
s.callsLatency.With(prometheus.Labels{
"method": "index",
"success": successToString(err == nil),
}).Observe(time.Since(start).Seconds())
s.calls.With(prometheus.Labels{
"method": "index",
"success": successToString(err == nil),
}).Add(1)
}()
tpl, err = template.ParseFS(static, "static/index.html")
if err != nil {
writeResponse(w, http.StatusInternalServerError, err.Error())
Expand All @@ -345,26 +260,10 @@ func (s *service) handleIndex(w http.ResponseWriter, r *http.Request) {

func (s *service) handleShorten(w http.ResponseWriter, r *http.Request) {
var (
err error
url []byte
hash string
start = time.Now()
err error
url []byte
hash string
)
defer func() {
if err != nil {
s.callsErrors.With(prometheus.Labels{
"method": "shorten",
}).Add(1)
}
s.callsLatency.With(prometheus.Labels{
"method": "shorten",
"success": successToString(err == nil),
}).Observe(time.Since(start).Seconds())
s.calls.With(prometheus.Labels{
"method": "index",
"success": successToString(err == nil),
}).Add(1)
}()
url, err = io.ReadAll(r.Body)
if err != nil {
writeResponse(w, http.StatusInternalServerError, err.Error())
Expand All @@ -386,25 +285,9 @@ func (s *service) handleShorten(w http.ResponseWriter, r *http.Request) {

func (s *service) handleLonger(w http.ResponseWriter, r *http.Request) {
var (
err error
url string
start = time.Now()
err error
url string
)
defer func() {
if err != nil {
s.callsErrors.With(prometheus.Labels{
"method": "longer",
}).Add(1)
}
s.callsLatency.With(prometheus.Labels{
"method": "longer",
"success": successToString(err == nil),
}).Observe(time.Since(start).Seconds())
s.calls.With(prometheus.Labels{
"method": "index",
"success": successToString(err == nil),
}).Add(1)
}()
path := strings.Split(r.URL.Path, "/")
if !isShortCorrect(path[len(path)-1]) {
err = fmt.Errorf("'%s' is not a valid short path", path[len(path)-1])
Expand Down
4 changes: 2 additions & 2 deletions internal/table/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func doTx(
attempts, onIntermediate := 0, trace.TableOnDoTx(
opts.Trace,
&ctx,
opts.ID,
opts.Label,
opts.Idempotent,
isRetryCalledAbove(ctx),
)
Expand Down Expand Up @@ -119,7 +119,7 @@ func do(
if opts.Trace == nil {
opts.Trace = &trace.Table{}
}
attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, opts.ID, opts.Idempotent, isRetryCalledAbove(ctx))
attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, opts.Label, opts.Idempotent, isRetryCalledAbove(ctx))
defer func() {
onIntermediate(err)(attempts, err)
}()
Expand Down
12 changes: 6 additions & 6 deletions log/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) {
return nil
}
ctx := with(*info.Context, TRACE, "ydb", "retry")
id := info.ID
label := info.Label
idempotent := info.Idempotent
l.Log(ctx, "start",
String("id", id),
String("label", label),
Bool("idempotent", idempotent),
)
start := time.Now()
return func(info trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) {
if info.Error == nil {
l.Log(ctx, "attempt done",
String("id", id),
String("label", label),
latencyField(start),
)
} else {
Expand All @@ -46,7 +46,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) {
m := retry.Check(info.Error)
l.Log(WithLevel(ctx, lvl), "attempt failed",
Error(info.Error),
String("id", id),
String("label", label),
latencyField(start),
Bool("retryable", m.MustRetry(idempotent)),
Int64("code", m.StatusCode()),
Expand All @@ -57,7 +57,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) {
return func(info trace.RetryLoopDoneInfo) {
if info.Error == nil {
l.Log(ctx, "done",
String("id", id),
String("label", label),
latencyField(start),
Int("attempts", info.Attempts),
)
Expand All @@ -69,7 +69,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) {
m := retry.Check(info.Error)
l.Log(WithLevel(ctx, lvl), "failed",
Error(info.Error),
String("id", id),
String("label", label),
latencyField(start),
Int("attempts", info.Attempts),
Bool("retryable", m.MustRetry(idempotent)),
Expand Down
Loading

0 comments on commit b7964b9

Please sign in to comment.