From 5cc039618f4f0166d56ae29758e757875c5f102b Mon Sep 17 00:00:00 2001 From: Dimitris Sotirakis Date: Wed, 11 Dec 2024 11:53:31 +0200 Subject: [PATCH] feat: add `ci.github.workflow.job.head_branch.is_main` boolean attribute (#178) * Add ci.github.workflow.job.head_branch.is_main attribute * Get default branch from payload --- receiver/githubactionsreceiver/receiver_test.go | 10 +++++++++- .../githubactionsreceiver/trace_event_handling.go | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/receiver/githubactionsreceiver/receiver_test.go b/receiver/githubactionsreceiver/receiver_test.go index 25e78c5..04df253 100644 --- a/receiver/githubactionsreceiver/receiver_test.go +++ b/receiver/githubactionsreceiver/receiver_test.go @@ -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 { @@ -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 { diff --git a/receiver/githubactionsreceiver/trace_event_handling.go b/receiver/githubactionsreceiver/trace_event_handling.go index d0c8157..a144847 100644 --- a/receiver/githubactionsreceiver/trace_event_handling.go +++ b/receiver/githubactionsreceiver/trace_event_handling.go @@ -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())) @@ -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()) @@ -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) } }