Skip to content

Commit

Permalink
feat: add ci.github.workflow.job.head_branch.is_main boolean attrib…
Browse files Browse the repository at this point in the history
…ute (#178)

* Add ci.github.workflow.job.head_branch.is_main attribute

* Get default branch from payload
  • Loading branch information
dsotirakis authored Dec 11, 2024
1 parent 4c33125 commit 5cc0396
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 9 additions & 1 deletion receiver/githubactionsreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ func TestProcessSteps(t *testing.T) {
traceID, _ := generateTraceID(123, 1)
parentSpanID := createParentSpan(ss, tc.givenSteps, &github.WorkflowJob{}, traceID, logger)

processSteps(ss, tc.givenSteps, &github.WorkflowJob{}, traceID, parentSpanID, logger)
defaultBranch := "main"
processSteps(ss, tc.givenSteps, &github.WorkflowJob{}, &defaultBranch, traceID, parentSpanID, logger)

startIdx := 1 // Skip the parent span if it's the first one
if len(tc.expectedStatuses) == 0 {
Expand Down Expand Up @@ -263,6 +264,13 @@ func TestResourceAndSpanAttributesCreation(t *testing.T) {
continue
}

isMainValue, found := attrs.Get("ci.github.workflow.job.head_branch.is_main")
if !found || isMainValue.AsString() == "" { // Skip if the attribute is not found or name is empty
continue
}

require.True(t, isMainValue.Bool())

expectedStepName := expectedStep["ci.github.workflow.job.step.name"]

if stepName == expectedStepName {
Expand Down
15 changes: 11 additions & 4 deletions receiver/githubactionsreceiver/trace_event_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func eventToTraces(event interface{}, config *Config, logger *zap.Logger) (*ptra
return nil, fmt.Errorf("failed to generate trace ID: %w", err)
}

defaultBranch := e.GetRepo().DefaultBranch

parentSpanID := createParentSpan(scopeSpans, e.GetWorkflowJob().Steps, e.GetWorkflowJob(), traceID, logger)
processSteps(scopeSpans, e.GetWorkflowJob().Steps, e.GetWorkflowJob(), traceID, parentSpanID, logger)
processSteps(scopeSpans, e.GetWorkflowJob().Steps, e.GetWorkflowJob(), defaultBranch, traceID, parentSpanID, logger)

case *github.WorkflowRunEvent:
logger.Info("Processing WorkflowRunEvent", zap.Int64("workflow_id", e.GetWorkflowRun().GetID()), zap.String("workflow_name", e.GetWorkflowRun().GetName()), zap.String("repo", e.GetRepo().GetFullName()))
Expand Down Expand Up @@ -161,13 +163,18 @@ func createRootSpan(resourceSpans ptrace.ResourceSpans, event *github.WorkflowRu
return rootSpanID, nil
}

func createSpan(scopeSpans ptrace.ScopeSpans, step *github.TaskStep, job *github.WorkflowJob, traceID pcommon.TraceID, parentSpanID pcommon.SpanID, logger *zap.Logger) pcommon.SpanID {
func createSpan(scopeSpans ptrace.ScopeSpans, step *github.TaskStep, job *github.WorkflowJob, defaultBranch *string, traceID pcommon.TraceID, parentSpanID pcommon.SpanID, logger *zap.Logger) pcommon.SpanID {
logger.Debug("Processing span", zap.String("step_name", step.GetName()))
span := scopeSpans.Spans().AppendEmpty()
span.SetTraceID(traceID)
span.SetParentSpanID(parentSpanID)

var spanID pcommon.SpanID
if job.GetHeadBranch() == *defaultBranch {
span.Attributes().PutBool("ci.github.workflow.job.head_branch.is_main", true)
} else {
span.Attributes().PutBool("ci.github.workflow.job.head_branch.is_main", false)
}

span.Attributes().PutStr("ci.github.workflow.job.step.name", step.GetName())
span.Attributes().PutStr("ci.github.workflow.job.step.status", step.GetStatus())
Expand Down Expand Up @@ -267,9 +274,9 @@ func generateStepSpanID(runID int64, runAttempt int, jobName string, stepNumber
return spanID, nil
}

func processSteps(scopeSpans ptrace.ScopeSpans, steps []*github.TaskStep, job *github.WorkflowJob, traceID pcommon.TraceID, parentSpanID pcommon.SpanID, logger *zap.Logger) {
func processSteps(scopeSpans ptrace.ScopeSpans, steps []*github.TaskStep, job *github.WorkflowJob, defaultBranch *string, traceID pcommon.TraceID, parentSpanID pcommon.SpanID, logger *zap.Logger) {
for _, step := range steps {
createSpan(scopeSpans, step, job, traceID, parentSpanID, logger)
createSpan(scopeSpans, step, job, defaultBranch, traceID, parentSpanID, logger)
}
}

Expand Down

0 comments on commit 5cc0396

Please sign in to comment.