Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin committed Jul 31, 2020
1 parent 2511dd9 commit 37ef49f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 4 additions & 1 deletion engine/api/project/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"database/sql"
"reflect"
"runtime"
"strings"
"time"

"github.com/go-gorp/gorp"

"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/keys"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
Expand Down Expand Up @@ -310,6 +311,8 @@ func unwrap(ctx context.Context, db gorp.SqlExecutor, p *dbProject, opts []LoadO
continue
}
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
nameSplitted := strings.Split(name, "/")
name = nameSplitted[len(nameSplitted)-1]
_, end = telemetry.Span(ctx, name)
if err := f(db, &proj); err != nil && sdk.Cause(err) != sql.ErrNoRows {
end()
Expand Down
6 changes: 6 additions & 0 deletions engine/api/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,12 @@ func (api *API) postWorkflowRunHandler() service.Handler {
}

func (api *API) initWorkflowRun(ctx context.Context, projKey string, wf *sdk.Workflow, wfRun *sdk.WorkflowRun, opts sdk.WorkflowRunPostHandlerOption) {
ctx, end := telemetry.Span(ctx, "api.initWorkflowRun",
telemetry.Tag(telemetry.TagProjectKey, projKey),
telemetry.Tag(telemetry.TagWorkflow, wf.Name),
)
defer end()

var asCodeInfosMsg []sdk.Message
var report = new(workflow.ProcessorReport)
var u = opts.AuthConsumer
Expand Down
18 changes: 15 additions & 3 deletions engine/api/workflow_run_craft.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ovh/cds/sdk/log"
"github.com/ovh/cds/sdk/telemetry"
"github.com/pkg/errors"
"go.opencensus.io/trace"
)

func (api *API) WorkflowRunCraft(ctx context.Context, tick time.Duration) error {
Expand All @@ -32,8 +33,8 @@ func (api *API) WorkflowRunCraft(ctx context.Context, tick time.Duration) error
ctx,
"workflowRunCraft-"+strconv.FormatInt(id, 10),
func(ctx context.Context) {
ctx, end := telemetry.SpanFromMain(ctx, "api.WorkflowRunCraft")
defer end()
ctx, span := telemetry.New(ctx, api, "api.workflowRunCraft", nil, trace.SpanKindUnspecified)
defer span.End()
if err := api.workflowRunCraft(ctx, id); err != nil {
log.Error(ctx, "WorkflowRunCraft> error on workflow run %d: %v", id, err)
}
Expand All @@ -47,26 +48,34 @@ func (api *API) WorkflowRunCraft(ctx context.Context, tick time.Duration) error
}

func (api *API) workflowRunCraft(ctx context.Context, id int64) error {
_, next := telemetry.Span(ctx, "api.workflowRunCraft.lock")
lockKey := cache.Key("api:workflowRunCraft", strconv.FormatInt(id, 10))
b, err := api.Cache.Lock(lockKey, 5*time.Minute, 0, 1)
if err != nil {
next()
return err
}
if !b {
log.Debug("api.workflowRunCraft> run %d is locked in cache", id)
next()
return nil
}
next()
defer func() {
_ = api.Cache.Unlock(lockKey)
}()

_, next = telemetry.Span(ctx, "api.workflowRunCraft.LoadRunByID")
run, err := workflow.LoadRunByID(api.mustDB(), id, workflow.LoadRunOptions{})
if sdk.ErrorIs(err, sdk.ErrNotFound) {
next()
return nil
}
if err != nil {
return sdk.WrapError(err, "unable to load project %d", run.ProjectID)
next()
return sdk.WrapError(err, "unable to load workflow run %d", id)
}
next()

if !run.ToCraft {
return nil
Expand All @@ -76,12 +85,15 @@ func (api *API) workflowRunCraft(ctx context.Context, id int64) error {
return errors.New("unable to craft workflow run without options...")
}

_, next = telemetry.Span(ctx, "api.workflowRunCraft.LoadProjectByID")
proj, err := project.LoadByID(api.mustDB(), run.ProjectID,
project.LoadOptions.WithVariables,
project.LoadOptions.WithIntegrations)
if err != nil {
next()
return sdk.WrapError(err, "unable to load project %d", run.ProjectID)
}
next()

wf, err := workflow.LoadByID(ctx, api.mustDB(), api.Cache, *proj, run.WorkflowID, workflow.LoadOptions{
DeepPipeline: true,
Expand Down
8 changes: 7 additions & 1 deletion sdk/telemetry/tracing_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ func New(ctx context.Context, s Service, name string, sampler trace.Sampler, spa
if exp == nil {
return ctx, nil
}
return trace.StartSpan(ctx, name,
ctx, span := trace.StartSpan(ctx, name,
trace.WithSampler(sampler),
trace.WithSpanKind(spanKind))
ctx = SpanContextToContext(ctx, span.SpanContext())
ctx = ContextWithTag(ctx,
TagServiceType, s.Type(),
TagServiceName, s.Name(),
)
return ctx, span
}

// Start may start a tracing span
Expand Down

0 comments on commit 37ef49f

Please sign in to comment.