|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/google/go-github/v39/github" |
| 13 | + "github.com/lightstep/otel-launcher-go/launcher" |
| 14 | + "go.opentelemetry.io/otel" |
| 15 | + "go.opentelemetry.io/otel/codes" |
| 16 | + "go.opentelemetry.io/otel/trace" |
| 17 | +) |
| 18 | + |
| 19 | +type actionConfig struct { |
| 20 | + workflow string |
| 21 | + githubRepository string |
| 22 | + owner string |
| 23 | + repo string |
| 24 | + runID string |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: add attributes using https://docs.github.com/en/actions/learn-github-actions/environment-variables |
| 28 | +// TODO: add user-agent |
| 29 | +// TODO: add support for auth |
| 30 | + |
| 31 | +func getSteps(ctx context.Context, conf actionConfig) error { |
| 32 | + tracer := otel.Tracer(conf.githubRepository) |
| 33 | + client := github.NewClient(nil) |
| 34 | + id, err := strconv.ParseInt(conf.runID, 10, 64) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + workflow, _, err := client.Actions.GetWorkflowRunByID(ctx, conf.owner, conf.repo, id) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + ctx, workflowSpan := tracer.Start(ctx, *workflow.Name, trace.WithTimestamp(workflow.CreatedAt.Time)) |
| 44 | + jobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), conf.owner, conf.repo, id, &github.ListWorkflowJobsOptions{}) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + for _, job := range jobs.Jobs { |
| 50 | + ctx, jobSpan := tracer.Start(ctx, *job.Name, trace.WithTimestamp(job.StartedAt.Time)) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + for _, step := range job.Steps { |
| 55 | + _, stepSpan := tracer.Start(ctx, *step.Name, trace.WithTimestamp(step.StartedAt.Time)) |
| 56 | + stepSpan.End(trace.WithTimestamp(step.CompletedAt.Time)) |
| 57 | + } |
| 58 | + jobSpan.End(trace.WithTimestamp(job.CompletedAt.Time)) |
| 59 | + } |
| 60 | + workflowSpan.End(trace.WithTimestamp(workflow.UpdatedAt.Time)) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func parseConfig() (actionConfig, error) { |
| 65 | + githubRepository, ok := os.LookupEnv("GITHUB_REPOSITORY") |
| 66 | + if !ok { |
| 67 | + return actionConfig{}, errors.New("missing variable: GITHUB_REPOSITORY") |
| 68 | + } |
| 69 | + |
| 70 | + runID, ok := os.LookupEnv("GITHUB_RUN_ID") |
| 71 | + if !ok { |
| 72 | + return actionConfig{}, errors.New("missing variable: GITHUB_RUN_ID") |
| 73 | + } |
| 74 | + |
| 75 | + workflowName, ok := os.LookupEnv("GITHUB_WORKFLOW") |
| 76 | + if !ok { |
| 77 | + return actionConfig{}, errors.New("missing variable: GITHUB_WORKFLOW") |
| 78 | + } |
| 79 | + |
| 80 | + parts := strings.Split(githubRepository, "/") |
| 81 | + if len(parts) < 2 { |
| 82 | + return actionConfig{}, fmt.Errorf("invalid variable GITHUB_REPOSITORY: %s", githubRepository) |
| 83 | + } |
| 84 | + conf := actionConfig{ |
| 85 | + workflow: workflowName, |
| 86 | + githubRepository: githubRepository, |
| 87 | + owner: parts[0], |
| 88 | + repo: parts[1], |
| 89 | + runID: runID, |
| 90 | + } |
| 91 | + |
| 92 | + return conf, nil |
| 93 | +} |
| 94 | + |
| 95 | +func main() { |
| 96 | + |
| 97 | + conf, err := parseConfig() |
| 98 | + if err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | + lsOtel := launcher.ConfigureOpentelemetry( |
| 102 | + launcher.WithServiceName(conf.githubRepository), |
| 103 | + ) |
| 104 | + defer lsOtel.Shutdown() |
| 105 | + tracer := otel.Tracer(conf.githubRepository) |
| 106 | + ctx, span := tracer.Start(context.Background(), conf.workflow) |
| 107 | + defer span.End() |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + log.Printf("%v", err) |
| 111 | + } |
| 112 | + |
| 113 | + err = getSteps(ctx, conf) |
| 114 | + if err != nil { |
| 115 | + span.SetStatus(codes.Error, err.Error()) |
| 116 | + log.Println(err) |
| 117 | + } |
| 118 | +} |
0 commit comments