Skip to content

Commit

Permalink
header bump [goreleaser]
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Sep 11, 2024
1 parent 48f2afc commit 4db816f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/metrics/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package metrics

// HeadersToMap converts a string of headers to a map.
func HeadersToMap(val string) map[string]string {
return headersToMap(val)
}
34 changes: 34 additions & 0 deletions core/metrics/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const (
otelEndpointEnv = "OTEL_EXPORTER_OTLP_ENDPOINT"
otelTransportEnv = "OTEL_EXPORTER_OTLP_TRANSPORT"
otelInsecureEvn = "INSECURE_MODE"
otelHeadersEnv = "OTEL_EXPORTER_OTLP_HEADERS"
)

//go:generate go run golang.org/x/tools/cmd/stringer -type=otlpTransportType -linecomment
Expand All @@ -137,6 +138,7 @@ func makeOTLPExporter(ctx context.Context, envSuffix string) (*otlptrace.Exporte
transport := transportFromString(getEnvSuffix(otelTransportEnv, envSuffix, otlpTransportHTTP.String()))
url := getEnvSuffix(otelEndpointEnv, envSuffix, "")
insecure := getEnvSuffix(otelInsecureEvn, envSuffix, "false")
headers := getEnvSuffix(otelHeadersEnv, envSuffix, "")

if url == "" {
return nil, fmt.Errorf("could not create exporter: url is empty")
Expand All @@ -146,6 +148,7 @@ func makeOTLPExporter(ctx context.Context, envSuffix string) (*otlptrace.Exporte
transport,
WithURL(url),
WithInsecure(insecure == "true"),
WithHeaders(headers),
)
if err != nil {
return nil, fmt.Errorf("could not create client from transport: %w", err)
Expand Down Expand Up @@ -214,6 +217,37 @@ func WithInsecure(isInsecure bool) Option {
}
}

func WithHeaders(headers string) Option {

Check failure on line 220 in core/metrics/otlp.go

View workflow job for this annotation

GitHub Actions / Lint (core)

exported: exported function WithHeaders should have comment or be unexported (revive)
return func(o *transportOptions) error {
realHeaders := headersToMap(headers)
o.httpOptions = append(o.httpOptions, otlptracehttp.WithHeaders(realHeaders))
o.grpcOptions = append(o.grpcOptions, otlptracegrpc.WithHeaders(realHeaders))
return nil
}
}

func headersToMap(input string) map[string]string {
// Initialize the map
result := make(map[string]string)

// Split the input string by comma to get key=value pairs
pairs := strings.Split(input, ",")

// Iterate over each pair
for _, pair := range pairs {
// Split each pair by '=' to get the key and value
kv := strings.SplitN(pair, "=", 2)

Check failure on line 239 in core/metrics/otlp.go

View workflow job for this annotation

GitHub Actions / Lint (core)

Magic number: 2, in <argument> detected (mnd)
if len(kv) == 2 {

Check failure on line 240 in core/metrics/otlp.go

View workflow job for this annotation

GitHub Actions / Lint (core)

Magic number: 2, in <condition> detected (mnd)
key := kv[0]
value := kv[1]
// Add the key and value to the map
result[key] = value
}
}

return result
}

// transportFromString converts a string to a transport type.
// Defaults to http if the string is not recognized.
func transportFromString(transport string) otlpTransportType {
Expand Down
71 changes: 71 additions & 0 deletions core/metrics/otlp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package metrics_test

import (
"github.com/synapsecns/sanguine/core/metrics"
"reflect"
"testing"
)

func TestHeadersToMap(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]string
}{
{
name: "basic input",
input: "key1=value1,key2=value2",
expected: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
{
name: "empty input",
input: "",
expected: map[string]string{},
},
{
name: "input with extra spaces",
input: "key1 = value1 , key2= value2 ",
expected: map[string]string{
"key1 ": " value1 ",
" key2": " value2 ",
},
},
{
name: "input with missing value",
input: "key1=value1,key2=",
expected: map[string]string{
"key1": "value1",
"key2": "",
},
},
{
name: "input with missing key",
input: "=value1,key2=value2",
expected: map[string]string{
"": "value1",
"key2": "value2",
},
},
{
name: "input with multiple equal signs",
input: "key1=value1=extra,key2=value2",
expected: map[string]string{
"key1": "value1=extra",
"key2": "value2",
},
},
}

for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
result := metrics.HeadersToMap(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("HeadersToMap(%v) = %v; want %v", tt.input, result, tt.expected)
}
})
}
}

0 comments on commit 4db816f

Please sign in to comment.