Add tracing
This commit is contained in:
168
ntpdb/otel.go
Normal file
168
ntpdb/otel.go
Normal file
@@ -0,0 +1,168 @@
|
||||
// Code generated by gowrap. DO NOT EDIT.
|
||||
// template: https://raw.githubusercontent.com/hexdigest/gowrap/6c8f05695fec23df85903a8da0af66ac414e2a63/templates/opentelemetry
|
||||
// gowrap: http://github.com/hexdigest/gowrap
|
||||
|
||||
package ntpdb
|
||||
|
||||
//go:generate gowrap gen -p go.ntppool.org/data-api/ntpdb -i QuerierTx -t https://raw.githubusercontent.com/hexdigest/gowrap/6c8f05695fec23df85903a8da0af66ac414e2a63/templates/opentelemetry -o otel.go -l ""
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// QuerierTxWithTracing implements QuerierTx interface instrumented with opentracing spans
|
||||
type QuerierTxWithTracing struct {
|
||||
QuerierTx
|
||||
_instance string
|
||||
_spanDecorator func(span trace.Span, params, results map[string]interface{})
|
||||
}
|
||||
|
||||
// NewQuerierTxWithTracing returns QuerierTxWithTracing
|
||||
func NewQuerierTxWithTracing(base QuerierTx, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) QuerierTxWithTracing {
|
||||
d := QuerierTxWithTracing{
|
||||
QuerierTx: base,
|
||||
_instance: instance,
|
||||
}
|
||||
|
||||
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
|
||||
d._spanDecorator = spanDecorator[0]
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
// Begin implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) Begin(ctx context.Context) (q1 QuerierTx, err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.Begin")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx}, map[string]interface{}{
|
||||
"q1": q1,
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.Begin(ctx)
|
||||
}
|
||||
|
||||
// Commit implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) Commit(ctx context.Context) (err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.Commit")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx}, map[string]interface{}{
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.Commit(ctx)
|
||||
}
|
||||
|
||||
// GetServerNetspeed implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (u1 uint32, err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerNetspeed")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx,
|
||||
"ip": ip}, map[string]interface{}{
|
||||
"u1": u1,
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.GetServerNetspeed(ctx, ip)
|
||||
}
|
||||
|
||||
// GetZoneStatsData implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) GetZoneStatsData(ctx context.Context) (ga1 []GetZoneStatsDataRow, err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneStatsData")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx}, map[string]interface{}{
|
||||
"ga1": ga1,
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.GetZoneStatsData(ctx)
|
||||
}
|
||||
|
||||
// GetZoneStatsV2 implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) GetZoneStatsV2(ctx context.Context, ip string) (ga1 []GetZoneStatsV2Row, err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneStatsV2")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx,
|
||||
"ip": ip}, map[string]interface{}{
|
||||
"ga1": ga1,
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.GetZoneStatsV2(ctx, ip)
|
||||
}
|
||||
|
||||
// Rollback implements QuerierTx
|
||||
func (_d QuerierTxWithTracing) Rollback(ctx context.Context) (err error) {
|
||||
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.Rollback")
|
||||
defer func() {
|
||||
if _d._spanDecorator != nil {
|
||||
_d._spanDecorator(_span, map[string]interface{}{
|
||||
"ctx": ctx}, map[string]interface{}{
|
||||
"err": err})
|
||||
} else if err != nil {
|
||||
_span.RecordError(err)
|
||||
_span.SetAttributes(
|
||||
attribute.String("event", "error"),
|
||||
attribute.String("message", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
_span.End()
|
||||
}()
|
||||
return _d.QuerierTx.Rollback(ctx)
|
||||
}
|
||||
17
ntpdb/querier.go
Normal file
17
ntpdb/querier.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.22.0
|
||||
|
||||
package ntpdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
GetServerNetspeed(ctx context.Context, ip string) (uint32, error)
|
||||
GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error)
|
||||
GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
64
ntpdb/tx.go
Normal file
64
ntpdb/tx.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package ntpdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type QuerierTx interface {
|
||||
Querier
|
||||
|
||||
Begin(ctx context.Context) (QuerierTx, error)
|
||||
Commit(ctx context.Context) error
|
||||
Rollback(ctx context.Context) error
|
||||
}
|
||||
|
||||
type Beginner interface {
|
||||
Begin(context.Context) (sql.Tx, error)
|
||||
}
|
||||
|
||||
type Tx interface {
|
||||
Begin(context.Context) (sql.Tx, error)
|
||||
Commit(ctx context.Context) error
|
||||
Rollback(ctx context.Context) error
|
||||
}
|
||||
|
||||
func (q *Queries) Begin(ctx context.Context) (QuerierTx, error) {
|
||||
tx, err := q.db.(Beginner).Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Queries{db: &tx}, nil
|
||||
}
|
||||
|
||||
func (q *Queries) Commit(ctx context.Context) error {
|
||||
tx, ok := q.db.(Tx)
|
||||
if !ok {
|
||||
return sql.ErrTxDone
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (q *Queries) Rollback(ctx context.Context) error {
|
||||
tx, ok := q.db.(Tx)
|
||||
if !ok {
|
||||
return sql.ErrTxDone
|
||||
}
|
||||
return tx.Rollback(ctx)
|
||||
}
|
||||
|
||||
type WrappedQuerier struct {
|
||||
QuerierTxWithTracing
|
||||
}
|
||||
|
||||
func NewWrappedQuerier(q QuerierTx) QuerierTx {
|
||||
return &WrappedQuerier{NewQuerierTxWithTracing(q, "")}
|
||||
}
|
||||
|
||||
func (wq *WrappedQuerier) Begin(ctx context.Context) (QuerierTx, error) {
|
||||
q, err := wq.QuerierTxWithTracing.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewWrappedQuerier(q), nil
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
|
||||
"log/slog"
|
||||
|
||||
"go.ntppool.org/common/tracing"
|
||||
)
|
||||
|
||||
type ZoneStats []ZoneStat
|
||||
@@ -14,7 +16,9 @@ type ZoneStat struct {
|
||||
V6 float64
|
||||
}
|
||||
|
||||
func (q *Queries) GetZoneStats(ctx context.Context) (*ZoneStats, error) {
|
||||
func GetZoneStats(ctx context.Context, q Querier) (*ZoneStats, error) {
|
||||
ctx, span := tracing.NewTracer("zone-stats").Start(ctx, "GetZoneStats")
|
||||
defer span.End()
|
||||
|
||||
zoneStatsRows, err := q.GetZoneStatsData(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user