Skip to content

Commit

Permalink
moved function to pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-urbanski-freiheit-com committed Dec 17, 2024
1 parent 54d2ec6 commit 08f811e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
38 changes: 38 additions & 0 deletions pkg/tracing/span.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright freiheit.com*/

package tracing

import (
"context"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

type OnErrFunc = func(err error) error

// StartSpanFromContext is the same as tracer.StartSpanFromContext, but also returns an onError function that tags the span as error
// You should call the onErrorFunc when the span should be marked as failed.
func StartSpanFromContext(ctx context.Context, name string) (tracer.Span, context.Context, OnErrFunc) {
mySpan, ctx := tracer.StartSpanFromContext(ctx, name)
onErr := func(err error) error {
if err == nil {
return nil
}
mySpan.Finish(tracer.WithError(err))
return err
}
return mySpan, ctx, onErr
}
22 changes: 3 additions & 19 deletions services/manifest-repo-export-service/pkg/migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,11 @@ import (
"github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/logger"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/freiheit-com/kuberpult/pkg/tracing"
)

type OnErrFunc = func(err error) error

// StartSpanFromContext is the same as tracer.StartSpanFromContext, but also returns an onError function that tags the span as error
// You should call the onErrorFunc when the span should be marked as failed.
func StartSpanFromContext(ctx context.Context, name string) (tracer.Span, context.Context, OnErrFunc) {
mySpan, ctx := tracer.StartSpanFromContext(ctx, name)
onErr := func(err error) error {
if err == nil {
return nil
}
mySpan.Finish(tracer.WithError(err))
return err
}
return mySpan, ctx, onErr
}

func DBReadCustomMigrationCutoff(h *db.DBHandler, ctx context.Context, transaction *sql.Tx, requestedVersion *api.KuberpultVersion) (*api.KuberpultVersion, error) {
span, ctx, onErr := StartSpanFromContext(ctx, "DBReadCustomMigrationCutoff")
span, ctx, onErr := tracing.StartSpanFromContext(ctx, "DBReadCustomMigrationCutoff")
defer span.Finish()

requestedVersionString := FormatKuberpultVersion(requestedVersion)
Expand Down Expand Up @@ -100,7 +84,7 @@ LIMIT 1;`)
}

func DBWriteCustomMigrationCutoff(h *db.DBHandler, ctx context.Context, tx *sql.Tx, kuberpultVersion *api.KuberpultVersion) error {
span, ctx, onErr := StartSpanFromContext(ctx, "DBWriteCustomMigrationCutoff")
span, ctx, onErr := tracing.StartSpanFromContext(ctx, "DBWriteCustomMigrationCutoff")
defer span.Finish()

timestamp, err := h.DBReadTransactionTimestamp(ctx, tx)
Expand Down

0 comments on commit 08f811e

Please sign in to comment.