-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
192 lines (161 loc) · 4.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/google/go-github/v39/github"
"github.com/lightstep/otel-launcher-go/pipelines"
"go.opentelemetry.io/collector/translator/conventions"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/oauth2"
)
type actionConfig struct {
workflow string
githubRepository string
owner string
repo string
runID string
pipelineConfig pipelines.PipelineConfig
}
// TODO: add attributes using https://docs.github.com/en/actions/learn-github-actions/environment-variables
// TODO: add user-agent
// TODO: add support for auth
func getSteps(ctx context.Context, conf actionConfig) error {
tracer := otel.Tracer(conf.githubRepository)
client := github.NewClient(nil)
// login using the GITHUB_TOKEN coming from the jobs
// as per https://docs.github.com/en/actions/security-guides/automatic-token-authentication
githubToken, ok := os.LookupEnv("GITHUB_TOKEN")
if ok {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
}
id, err := strconv.ParseInt(conf.runID, 10, 64)
if err != nil {
return err
}
workflow, _, err := client.Actions.GetWorkflowRunByID(ctx, conf.owner, conf.repo, id)
if err != nil {
return err
}
ctx, workflowSpan := tracer.Start(ctx, *workflow.Name, trace.WithTimestamp(workflow.CreatedAt.Time))
defer workflowSpan.End(trace.WithTimestamp(workflow.UpdatedAt.Time))
jobs, _, err := client.Actions.ListWorkflowJobs(ctx, conf.owner, conf.repo, id, &github.ListWorkflowJobsOptions{})
if err != nil {
return err
}
for _, job := range jobs.Jobs {
ctx, jobSpan := tracer.Start(ctx, *job.Name, trace.WithTimestamp(job.GetStartedAt().Time))
if err != nil {
return err
}
for _, step := range job.Steps {
_, stepSpan := tracer.Start(ctx, *step.Name, trace.WithTimestamp(step.GetStartedAt().Time))
if step.CompletedAt != nil {
stepSpan.End(trace.WithTimestamp(step.CompletedAt.Time))
} else {
stepSpan.End()
}
}
if job.CompletedAt != nil {
jobSpan.End(trace.WithTimestamp(job.CompletedAt.Time))
} else {
jobSpan.End()
}
}
return nil
}
// Code inspired from the opentelemetry-go OTLP exporter
//
// https://github.com/open-telemetry/opentelemetry-go/blob/92551d3933c9c7ef5eaf4f93f876a5487d0024b9/exporters/otlp/otlpmetric/internal/otlpconfig/envconfig.go#L172
func stringToHeader(value string) map[string]string {
headersPairs := strings.Split(value, ",")
headers := make(map[string]string)
for _, header := range headersPairs {
nameValue := strings.SplitN(header, "=", 2)
if len(nameValue) < 2 {
continue
}
name, err := url.QueryUnescape(nameValue[0])
if err != nil {
continue
}
trimmedName := strings.TrimSpace(name)
trimmedValue := strings.TrimSpace(nameValue[1])
headers[trimmedName] = trimmedValue
}
return headers
}
func parseConfig() (actionConfig, error) {
endpoint, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
if !ok || len(endpoint) == 0 {
return actionConfig{}, errors.New("invalid endpoint")
}
headers := stringToHeader(os.Getenv("OTEL_EXPORTER_OTLP_HEADERS"))
githubRepository, ok := os.LookupEnv("GITHUB_REPOSITORY")
if !ok {
return actionConfig{}, errors.New("missing variable: GITHUB_REPOSITORY")
}
runID, ok := os.LookupEnv("GITHUB_RUN_ID")
if !ok {
return actionConfig{}, errors.New("missing variable: GITHUB_RUN_ID")
}
workflowName, ok := os.LookupEnv("GITHUB_WORKFLOW")
if !ok {
return actionConfig{}, errors.New("missing variable: GITHUB_WORKFLOW")
}
parts := strings.Split(githubRepository, "/")
if len(parts) < 2 {
return actionConfig{}, fmt.Errorf("invalid variable GITHUB_REPOSITORY: %s", githubRepository)
}
attributes := []attribute.KeyValue{
attribute.String(conventions.AttributeServiceName, githubRepository),
}
r, _ := resource.New(context.Background(),
resource.WithSchemaURL(semconv.SchemaURL),
resource.WithAttributes(attributes...),
)
insecure := false
conf := actionConfig{
workflow: workflowName,
githubRepository: githubRepository,
owner: parts[0],
repo: parts[1],
runID: runID,
pipelineConfig: pipelines.PipelineConfig{
Endpoint: endpoint,
Insecure: insecure, // TODO: provide config for this
Headers: headers,
Propagators: []string{"tracecontext"}, // TODO: provide config for this
Resource: r,
},
}
return conf, nil
}
func main() {
conf, err := parseConfig()
if err != nil {
log.Fatal(err)
}
pipelineShutdown, err := pipelines.NewTracePipeline(conf.pipelineConfig)
defer pipelineShutdown()
if err != nil {
log.Printf("%v", err)
}
err = getSteps(context.Background(), conf)
if err != nil {
log.Println(err)
}
}