Skip to content

Commit 38725d5

Browse files
committed
refactor to use OTLP env variables
1 parent 1bdd6cf commit 38725d5

File tree

6 files changed

+93
-25
lines changed

6 files changed

+93
-25
lines changed

.github/workflows/test.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ jobs:
1313
uses: codeboten/github-action-to-otlp@v1
1414
with:
1515
endpoint: "ingest.lightstep.com:443"
16-
env:
17-
LS_ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
16+
headers: "lightstep-access-token:${{ secrets.ACCESS_TOKEN }}"

action.yaml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
name: "Github Action to OTLP"
22
description: "Record job information as trace and transmit via OTLP"
33
inputs:
4-
endpoint: # endpoint of OTLP backend
4+
endpoint:
55
description: "Endpoint to send OTLP to"
66
required: true
7-
# headers: # endpoint of OTLP backend
8-
# description: "Headers to configure OTLP"
9-
# required: true
7+
headers:
8+
description: "Headers to configure gRPC metadata"
9+
required: false
1010
runs:
1111
using: "docker"
1212
image: "Dockerfile"
1313
env:
14-
OTEL_ENDPOINT: ${{ inputs.endpoint }}
14+
OTEL_EXPORTER_OTLP_ENDPOINT: ${{ inputs.endpoint }}
15+
OTEL_EXPORTER_OTLP_HEADERS: ${{ inputs.headers }}

go.mod

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ go 1.15
44

55
require (
66
github.com/google/go-github/v39 v39.0.0
7-
github.com/lightstep/otel-launcher-go v1.0.0-RC3
7+
github.com/lightstep/otel-launcher-go/pipelines v0.0.0-20210924222414-e413bcf575d8
88
github.com/stretchr/testify v1.7.0
9-
go.opentelemetry.io/otel v1.0.0-RC3
10-
go.opentelemetry.io/otel/trace v1.0.0-RC3
9+
go.opentelemetry.io/collector v0.28.0
10+
go.opentelemetry.io/otel v1.0.0
11+
go.opentelemetry.io/otel/sdk v1.0.0
12+
go.opentelemetry.io/otel/trace v1.0.0
1113
)

go.sum

+12-8
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
583583
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
584584
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
585585
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
586-
github.com/lightstep/otel-launcher-go v1.0.0-RC3 h1:u08r9nEjgxHdkWri7U0FQt/mK6bGzq2hjwIFE5rrh1A=
587-
github.com/lightstep/otel-launcher-go v1.0.0-RC3/go.mod h1:WxH2vxzKgHhDnrDgH17twtIyC1xvTyoZwFxspkW5lCc=
586+
github.com/lightstep/otel-launcher-go/pipelines v0.0.0-20210924222414-e413bcf575d8 h1:BDrgANrdSwsAKhQZapwbG9TIsFyaVqW0p0lA+84Iy/E=
587+
github.com/lightstep/otel-launcher-go/pipelines v0.0.0-20210924222414-e413bcf575d8/go.mod h1:k0DpkjWBF5gzRRfoTYMNTD4WmsKzBr7dzSxT4qcYHNA=
588588
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
589589
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
590590
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
@@ -785,8 +785,6 @@ github.com/securego/gosec v0.0.0-20200203094520-d13bb6d2420c/go.mod h1:gp0gaHj0W
785785
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
786786
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
787787
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
788-
github.com/sethvargo/go-envconfig v0.3.2 h1:277Lb2iTpUZjUZu1qLoLa/aetwvtZbKh8wNWXmc6dSk=
789-
github.com/sethvargo/go-envconfig v0.3.2/go.mod h1:XZ2JRR7vhlBEO5zMmOpLgUhgYltqYqq4d4tKagtPUv0=
790788
github.com/shirou/gopsutil v2.20.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
791789
github.com/shirou/gopsutil v3.21.5+incompatible h1:OloQyEerMi7JUrXiNzy8wQ5XN+baemxSl12QgIzt0jc=
792790
github.com/shirou/gopsutil v3.21.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
@@ -912,26 +910,32 @@ go.opentelemetry.io/contrib/propagators/ot v0.23.0 h1:QD69MmaJwLOaLj8uNmxpJa52v/
912910
go.opentelemetry.io/contrib/propagators/ot v0.23.0/go.mod h1:uhAU1z0UtdOTHWPfmpAOlikY/vK79cGwrteYDkcxvGU=
913911
go.opentelemetry.io/otel v1.0.0-RC3 h1:kvwiyEkiUT/JaadXzVLI/R1wDO934A7r3Bs2wEe6wqA=
914912
go.opentelemetry.io/otel v1.0.0-RC3/go.mod h1:Ka5j3ua8tZs4Rkq4Ex3hwgBgOchyPVq5S6P2lz//nKQ=
913+
go.opentelemetry.io/otel v1.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=
914+
go.opentelemetry.io/otel v1.0.0/go.mod h1:AjRVh9A5/5DE7S+mZtTR6t8vpKKryam+0lREnfmS4cg=
915915
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.23.0 h1:vKIEsT6IJU0NYd+iZccjgCmk80zsa7dTiC2Bu7U1jz0=
916916
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.23.0/go.mod h1:pe9oOWRaZyapdajWCn64fnl76v3cmTEmNBgh7MkKvwE=
917917
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.23.0 h1:JSsJID+KU3G8wxynfHIlWaefOvYngDjnrmtHOGb1sb0=
918918
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.23.0/go.mod h1:aSP5oMNaAfOYq+sRydHANZ0vBYLyZR/3lR9pru9aPLk=
919-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0-RC3 h1:5gOhYk62x9f5NAGSQl9WBc6J5nCI0tuofzMZ/dAR01Q=
920-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0-RC3/go.mod h1:1tvDhRy/GCexiD9dQZzqwqGnI7/fnZOsi31DyvK3zyQ=
921-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0-RC3 h1:F3cdr1An+QUPcj4SQrS92fEWV4A31jWFHCLpbiDUdCo=
922-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0-RC3/go.mod h1:9JCUOSptzVKaFbxGVO+Wa1P8fDpA5QGVTuIzL/PKSrk=
919+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0 h1:Vv4wbLEjheCTPV07jEav7fyUpJkyftQK7Ss2G7qgdSo=
920+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0/go.mod h1:3VqVbIbjAycfL1C7sIu/Uh/kACIUPWHztt8ODYwR3oM=
921+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0 h1:B9VtEB1u41Ohnl8U6rMCh1jjedu8HwFh4D0QeB+1N+0=
922+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0/go.mod h1:zhEt6O5GGJ3NCAICr4hlCPoDb2GQuh4Obb4gZBgkoQQ=
923923
go.opentelemetry.io/otel/internal/metric v0.23.0 h1:mPfzm9Iqhw7G2nDBmUAjFTfPqLZPbOW2k7QI57ITbaI=
924924
go.opentelemetry.io/otel/internal/metric v0.23.0/go.mod h1:z+RPiDJe30YnCrOhFGivwBS+DU1JU/PiLKkk4re2DNY=
925925
go.opentelemetry.io/otel/metric v0.23.0 h1:mYCcDxi60P4T27/0jchIDFa1WHEfQeU3zH9UEMpnj2c=
926926
go.opentelemetry.io/otel/metric v0.23.0/go.mod h1:G/Nn9InyNnIv7J6YVkQfpc0JCfKBNJaERBGw08nqmVQ=
927927
go.opentelemetry.io/otel/sdk v1.0.0-RC3 h1:iRMkET+EmJUn5mW0hJzygBraXRmrUwzbOtNvTCh/oKs=
928928
go.opentelemetry.io/otel/sdk v1.0.0-RC3/go.mod h1:78H6hyg2fka0NYT9fqGuFLvly2yCxiBXDJAgLKo/2Us=
929+
go.opentelemetry.io/otel/sdk v1.0.0 h1:BNPMYUONPNbLneMttKSjQhOTlFLOD9U22HNG1KrIN2Y=
930+
go.opentelemetry.io/otel/sdk v1.0.0/go.mod h1:PCrDHlSy5x1kjezSdL37PhbFUMjrsLRshJ2zCzeXwbM=
929931
go.opentelemetry.io/otel/sdk/export/metric v0.23.0 h1:7NeoKPPx6NdZBVHLEp/LY5Lq85Ff1WNZnuJkuRy+azw=
930932
go.opentelemetry.io/otel/sdk/export/metric v0.23.0/go.mod h1:SuMiREmKVRIwFKq73zvGTvwFpxb/ZAYkMfyqMoOtDqs=
931933
go.opentelemetry.io/otel/sdk/metric v0.23.0 h1:xlZhPbiue1+jjSFEth94q9QCmX8Q24mOtue9IAmlVyI=
932934
go.opentelemetry.io/otel/sdk/metric v0.23.0/go.mod h1:wa0sKK13eeIFW+0OFjcC3S1i7FTRRiLAXe1kjBVbhwg=
933935
go.opentelemetry.io/otel/trace v1.0.0-RC3 h1:9F0ayEvlxv8BmNmPbU005WK7hC+7KbOazCPZjNa1yME=
934936
go.opentelemetry.io/otel/trace v1.0.0-RC3/go.mod h1:VUt2TUYd8S2/ZRX09ZDFZQwn2RqfMB5MzO17jBojGxo=
937+
go.opentelemetry.io/otel/trace v1.0.0 h1:TSBr8GTEtKevYMG/2d21M989r5WJYVimhTHBKVEZuh4=
938+
go.opentelemetry.io/otel/trace v1.0.0/go.mod h1:PXTWqayeFUlJV1YDNhsJYB184+IvAH814St6o6ajzIs=
935939
go.opentelemetry.io/proto/otlp v0.7.0 h1:rwOQPCuKAKmwGKq2aVNnYIibI6wnV7EvzgfTCzcdGg8=
936940
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
937941
go.opentelemetry.io/proto/otlp v0.9.0 h1:C0g6TWmQYvjKRnljRULLWUVJGy8Uvu0NEL/5frY2/t4=

main.go

+60-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import (
55
"errors"
66
"fmt"
77
"log"
8+
"net/url"
89
"os"
910
"strconv"
1011
"strings"
1112

1213
"github.com/google/go-github/v39/github"
13-
"github.com/lightstep/otel-launcher-go/launcher"
14+
"github.com/lightstep/otel-launcher-go/pipelines"
15+
"go.opentelemetry.io/collector/translator/conventions"
1416
"go.opentelemetry.io/otel"
17+
"go.opentelemetry.io/otel/attribute"
18+
"go.opentelemetry.io/otel/sdk/resource"
19+
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
1520
"go.opentelemetry.io/otel/trace"
1621
)
1722

@@ -21,6 +26,7 @@ type actionConfig struct {
2126
owner string
2227
repo string
2328
runID string
29+
pipelineConfig pipelines.PipelineConfig
2430
}
2531

2632
// TODO: add attributes using https://docs.github.com/en/actions/learn-github-actions/environment-variables
@@ -71,7 +77,39 @@ func getSteps(ctx context.Context, conf actionConfig) error {
7177
return nil
7278
}
7379

80+
// Code inspired from the opentelemetry-go OTLP exporter
81+
//
82+
// https://github.com/open-telemetry/opentelemetry-go/blob/92551d3933c9c7ef5eaf4f93f876a5487d0024b9/exporters/otlp/otlpmetric/internal/otlpconfig/envconfig.go#L172
83+
func stringToHeader(value string) map[string]string {
84+
headersPairs := strings.Split(value, ",")
85+
headers := make(map[string]string)
86+
87+
for _, header := range headersPairs {
88+
nameValue := strings.SplitN(header, "=", 2)
89+
if len(nameValue) < 2 {
90+
continue
91+
}
92+
name, err := url.QueryUnescape(nameValue[0])
93+
if err != nil {
94+
continue
95+
}
96+
trimmedName := strings.TrimSpace(name)
97+
trimmedValue := strings.TrimSpace(nameValue[1])
98+
99+
headers[trimmedName] = trimmedValue
100+
}
101+
102+
return headers
103+
}
104+
74105
func parseConfig() (actionConfig, error) {
106+
endpoint, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
107+
if !ok || len(endpoint) == 0 {
108+
return actionConfig{}, errors.New("invalid endpoint")
109+
}
110+
111+
headers := stringToHeader(os.Getenv("OTEL_EXPORTER_OTLP_HEADERS"))
112+
75113
githubRepository, ok := os.LookupEnv("GITHUB_REPOSITORY")
76114
if !ok {
77115
return actionConfig{}, errors.New("missing variable: GITHUB_REPOSITORY")
@@ -91,12 +129,30 @@ func parseConfig() (actionConfig, error) {
91129
if len(parts) < 2 {
92130
return actionConfig{}, fmt.Errorf("invalid variable GITHUB_REPOSITORY: %s", githubRepository)
93131
}
132+
133+
attributes := []attribute.KeyValue{
134+
attribute.String(conventions.AttributeServiceName, githubRepository),
135+
}
136+
137+
r, _ := resource.New(context.Background(),
138+
resource.WithSchemaURL(semconv.SchemaURL),
139+
resource.WithAttributes(attributes...),
140+
)
141+
142+
insecure := false
94143
conf := actionConfig{
95144
workflow: workflowName,
96145
githubRepository: githubRepository,
97146
owner: parts[0],
98147
repo: parts[1],
99148
runID: runID,
149+
pipelineConfig: pipelines.PipelineConfig{
150+
Endpoint: endpoint,
151+
Insecure: insecure, // TODO: provide config for this
152+
Headers: headers,
153+
Propagators: []string{"tracecontext"}, // TODO: provide config for this
154+
Resource: r,
155+
},
100156
}
101157

102158
return conf, nil
@@ -108,10 +164,9 @@ func main() {
108164
if err != nil {
109165
log.Fatal(err)
110166
}
111-
lsOtel := launcher.ConfigureOpentelemetry(
112-
launcher.WithServiceName(conf.githubRepository),
113-
)
114-
defer lsOtel.Shutdown()
167+
168+
pipelineShutdown, err := pipelines.NewTracePipeline(conf.pipelineConfig)
169+
defer pipelineShutdown()
115170

116171
if err != nil {
117172
log.Printf("%v", err)

main_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ import (
88
)
99

1010
func TestParseConfig(t *testing.T) {
11+
1112
_, err := parseConfig()
1213
require.Error(t, err)
14+
require.Contains(t, err.Error(), "invalid endpoint")
15+
16+
os.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "endpoint:443")
17+
18+
_, err = parseConfig()
19+
require.Error(t, err)
1320
require.Contains(t, err.Error(), "missing variable: GITHUB_REPOSITORY")
1421

1522
os.Setenv("GITHUB_REPOSITORY", "garbage")
1623

1724
_, err = parseConfig()
1825
require.Error(t, err)
19-
require.Contains(t, err.Error(), "missing variable: GITHUB_JOB")
26+
require.Contains(t, err.Error(), "missing variable: GITHUB_RUN_ID")
2027

21-
os.Setenv("GITHUB_JOB", "123")
28+
os.Setenv("GITHUB_RUN_ID", "123")
2229

2330
_, err = parseConfig()
2431
require.Error(t, err)

0 commit comments

Comments
 (0)