chore(unified-storage): align how we do tracing (#114998)
This commit is contained in:
@@ -14,9 +14,9 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/lib/pq"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/noop"
|
||||
"go.uber.org/atomic"
|
||||
"google.golang.org/protobuf/proto"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -35,7 +35,8 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util/debouncer"
|
||||
)
|
||||
|
||||
const tracePrefix = "sql.resource."
|
||||
var tracer = otel.Tracer("github.com/grafana/grafana/pkg/storage/unified/sql")
|
||||
|
||||
const defaultPollingInterval = 100 * time.Millisecond
|
||||
const defaultWatchBufferSize = 100 // number of events to buffer in the watch stream
|
||||
const defaultPrunerHistoryLimit = 20
|
||||
@@ -56,7 +57,6 @@ type Backend interface {
|
||||
|
||||
type BackendOptions struct {
|
||||
DBProvider db.DBProvider
|
||||
Tracer trace.Tracer
|
||||
Reg prometheus.Registerer
|
||||
PollingInterval time.Duration
|
||||
WatchBufferSize int
|
||||
@@ -74,9 +74,6 @@ func NewBackend(opts BackendOptions) (Backend, error) {
|
||||
if opts.DBProvider == nil {
|
||||
return nil, errors.New("no db provider")
|
||||
}
|
||||
if opts.Tracer == nil {
|
||||
opts.Tracer = noop.NewTracerProvider().Tracer("sql-backend")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
if opts.PollingInterval == 0 {
|
||||
@@ -90,7 +87,6 @@ func NewBackend(opts BackendOptions) (Backend, error) {
|
||||
done: ctx.Done(),
|
||||
cancel: cancel,
|
||||
log: logging.DefaultLogger.With("logger", "sql-resource-server"),
|
||||
tracer: opts.Tracer,
|
||||
reg: opts.Reg,
|
||||
dbProvider: opts.DBProvider,
|
||||
pollingInterval: opts.PollingInterval,
|
||||
@@ -114,7 +110,6 @@ type backend struct {
|
||||
|
||||
// o11y
|
||||
log logging.Logger
|
||||
tracer trace.Tracer
|
||||
reg prometheus.Registerer
|
||||
storageMetrics *resource.StorageMetrics
|
||||
|
||||
@@ -171,7 +166,6 @@ func (b *backend) initLocked(ctx context.Context) error {
|
||||
rvManager, err := NewResourceVersionManager(ResourceManagerOptions{
|
||||
Dialect: b.dialect,
|
||||
DB: b.db,
|
||||
Tracer: b.tracer,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create resource version manager: %w", err)
|
||||
@@ -264,7 +258,7 @@ func (b *backend) Stop(_ context.Context) error {
|
||||
|
||||
// GetResourceStats implements Backend.
|
||||
func (b *backend) GetResourceStats(ctx context.Context, nsr resource.NamespacedResource, minCount int) ([]resource.ResourceStats, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"GetResourceStats", trace.WithAttributes(
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.GetResourceStats", trace.WithAttributes(
|
||||
attribute.String("namespace", nsr.Namespace),
|
||||
attribute.String("group", nsr.Group),
|
||||
attribute.String("resource", nsr.Resource),
|
||||
@@ -304,7 +298,7 @@ func (b *backend) GetResourceStats(ctx context.Context, nsr resource.NamespacedR
|
||||
}
|
||||
|
||||
func (b *backend) WriteEvent(ctx context.Context, event resource.WriteEvent) (int64, error) {
|
||||
_, span := b.tracer.Start(ctx, tracePrefix+"WriteEvent")
|
||||
_, span := tracer.Start(ctx, "sql.backend.WriteEvent")
|
||||
defer span.End()
|
||||
// TODO: validate key ?
|
||||
switch event.Type {
|
||||
@@ -320,7 +314,7 @@ func (b *backend) WriteEvent(ctx context.Context, event resource.WriteEvent) (in
|
||||
}
|
||||
|
||||
func (b *backend) create(ctx context.Context, event resource.WriteEvent) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"Create")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.create")
|
||||
defer span.End()
|
||||
|
||||
folder := ""
|
||||
@@ -408,7 +402,7 @@ func IsRowAlreadyExistsError(err error) bool {
|
||||
}
|
||||
|
||||
func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"Update")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.update")
|
||||
defer span.End()
|
||||
|
||||
folder := ""
|
||||
@@ -468,7 +462,7 @@ func (b *backend) update(ctx context.Context, event resource.WriteEvent) (int64,
|
||||
}
|
||||
|
||||
func (b *backend) delete(ctx context.Context, event resource.WriteEvent) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"Delete")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.delete")
|
||||
defer span.End()
|
||||
|
||||
folder := ""
|
||||
@@ -547,7 +541,7 @@ func (b *backend) checkConflict(res db.Result, key *resourcepb.ResourceKey, rv i
|
||||
}
|
||||
|
||||
func (b *backend) ReadResource(ctx context.Context, req *resourcepb.ReadRequest) *resource.BackendReadResponse {
|
||||
_, span := b.tracer.Start(ctx, tracePrefix+".Read")
|
||||
_, span := tracer.Start(ctx, "sql.backend.ReadResource")
|
||||
defer span.End()
|
||||
|
||||
// TODO: validate key ?
|
||||
@@ -580,7 +574,7 @@ func (b *backend) ReadResource(ctx context.Context, req *resourcepb.ReadRequest)
|
||||
}
|
||||
|
||||
func (b *backend) ListIterator(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"List")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.ListIterator")
|
||||
defer span.End()
|
||||
|
||||
if err := resource.MigrateListRequestVersionMatch(req, b.log); err != nil {
|
||||
@@ -602,7 +596,7 @@ func (b *backend) ListIterator(ctx context.Context, req *resourcepb.ListRequest,
|
||||
}
|
||||
|
||||
func (b *backend) ListHistory(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"ListHistory")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.ListHistory")
|
||||
defer span.End()
|
||||
|
||||
return b.getHistory(ctx, req, cb)
|
||||
@@ -610,7 +604,7 @@ func (b *backend) ListHistory(ctx context.Context, req *resourcepb.ListRequest,
|
||||
|
||||
// listLatest fetches the resources from the resource table.
|
||||
func (b *backend) listLatest(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"listLatest")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.listLatest")
|
||||
defer span.End()
|
||||
|
||||
if req.NextPageToken != "" {
|
||||
@@ -724,7 +718,7 @@ func (b *backend) ListModifiedSince(ctx context.Context, key resource.Namespaced
|
||||
|
||||
// listAtRevision fetches the resources from the resource_history table at a specific revision.
|
||||
func (b *backend) listAtRevision(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"listAtRevision")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.listAtRevision")
|
||||
defer span.End()
|
||||
|
||||
// Get the RV
|
||||
@@ -784,7 +778,7 @@ func (b *backend) listAtRevision(ctx context.Context, req *resourcepb.ListReques
|
||||
|
||||
// readHistory fetches the resource history from the resource_history table.
|
||||
func (b *backend) readHistory(ctx context.Context, key *resourcepb.ResourceKey, rv int64) *resource.BackendReadResponse {
|
||||
_, span := b.tracer.Start(ctx, tracePrefix+".ReadHistory")
|
||||
_, span := tracer.Start(ctx, "sql.backend.readHistory")
|
||||
defer span.End()
|
||||
|
||||
readReq := &sqlResourceHistoryReadRequest{
|
||||
@@ -815,7 +809,7 @@ func (b *backend) readHistory(ctx context.Context, key *resourcepb.ResourceKey,
|
||||
|
||||
// getHistory fetches the resource history from the resource_history table.
|
||||
func (b *backend) getHistory(ctx context.Context, req *resourcepb.ListRequest, cb func(resource.ListIterator) error) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"getHistory")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.getHistory")
|
||||
defer span.End()
|
||||
listReq := sqlGetHistoryRequest{
|
||||
SQLTemplate: sqltemplate.New(b.dialect),
|
||||
@@ -903,7 +897,7 @@ func (b *backend) WatchWriteEvents(ctx context.Context) (<-chan *resource.Writte
|
||||
|
||||
// listLatestRVs returns the latest resource version for each (Group, Resource) pair.
|
||||
func (b *backend) listLatestRVs(ctx context.Context) (groupResourceRV, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"listLatestRVs")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.listLatestRVs")
|
||||
defer span.End()
|
||||
var grvs []*groupResourceVersion
|
||||
err := b.db.WithTx(ctx, ReadCommittedRO, func(ctx context.Context, tx db.Tx) error {
|
||||
@@ -932,7 +926,7 @@ func (b *backend) listLatestRVs(ctx context.Context) (groupResourceRV, error) {
|
||||
|
||||
// fetchLatestRV returns the current maximum RV in the resource table
|
||||
func (b *backend) fetchLatestRV(ctx context.Context, x db.ContextExecer, d sqltemplate.Dialect, group, resource string) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"fetchLatestRV")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.fetchLatestRV")
|
||||
defer span.End()
|
||||
res, err := dbutil.QueryRow(ctx, x, sqlResourceVersionGet, sqlResourceVersionGetRequest{
|
||||
SQLTemplate: sqltemplate.New(d),
|
||||
@@ -951,7 +945,7 @@ func (b *backend) fetchLatestRV(ctx context.Context, x db.ContextExecer, d sqlte
|
||||
|
||||
// fetchLatestHistoryRV returns the current maximum RV in the resource_history table
|
||||
func (b *backend) fetchLatestHistoryRV(ctx context.Context, x db.ContextExecer, d sqltemplate.Dialect, key *resourcepb.ResourceKey, eventType resourcepb.WatchEvent_Type) (int64, error) {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"fetchLatestHistoryRV")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.fetchLatestHistoryRV")
|
||||
defer span.End()
|
||||
res, err := dbutil.QueryRow(ctx, x, sqlResourceHistoryReadLatestRV, sqlResourceHistoryReadLatestRVRequest{
|
||||
SQLTemplate: sqltemplate.New(d),
|
||||
@@ -973,7 +967,7 @@ func (b *backend) fetchLatestHistoryRV(ctx context.Context, x db.ContextExecer,
|
||||
const limitLastImportTimesDeletion = 1 * time.Hour
|
||||
|
||||
func (b *backend) GetResourceLastImportTimes(ctx context.Context) iter.Seq2[resource.ResourceLastImportTime, error] {
|
||||
ctx, span := b.tracer.Start(ctx, tracePrefix+"GetLastImportTimes")
|
||||
ctx, span := tracer.Start(ctx, "sql.backend.GetResourceLastImportTimes")
|
||||
defer span.End()
|
||||
|
||||
// Delete old entries, if configured, and if enough time has passed since last deletion.
|
||||
|
||||
Reference in New Issue
Block a user