Skip to content

Commit 1d03a52

Browse files
authored
chore: Minor cleanup (#39)
Signed-off-by: Joonas Bergius <joonas@bergi.us>
1 parent fd7d2c9 commit 1d03a52

4 files changed

Lines changed: 29 additions & 24 deletions

File tree

host.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package provider
22

33
import "encoding/json"
44

5-
const (
6-
OtelProtocolHTTP = "Http"
7-
OtelProtocolGRPC = "Grpc"
8-
)
9-
105
type RedactedString string
116

127
func (rs RedactedString) String() string {

observability.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"strings"
89
"time"
910

1011
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
@@ -22,9 +23,13 @@ import (
2223
)
2324

2425
const (
25-
OtelMetricExportInterval = 1 * time.Minute
26-
OtelTraceExportInterval = 1 * time.Minute
27-
OtelLogExportInterval = 10 * time.Second
26+
OtelMetricExportInterval = 1 * time.Minute
27+
OtelTraceExportInterval = 1 * time.Minute
28+
OtelLogExportInterval = 10 * time.Second
29+
OtelProtocolHTTP = "http"
30+
OtelProtocolGRPC = "grpc"
31+
OtelSpanLimitAttributePerEvent = 16
32+
OtelSpanLimitEventCount = 64
2833
)
2934

3035
func newPropagator() propagation.TextMapPropagator {
@@ -43,21 +48,22 @@ func newTracerProvider(ctx context.Context, config OtelConfig, serviceResource *
4348
endpoint = config.ObservabilityEndpoint
4449
}
4550

46-
switch config.Protocol {
51+
switch strings.ToLower(config.Protocol) {
4752
case OtelProtocolGRPC:
4853
exporter, err = otlptracegrpc.New(ctx, otlptracegrpc.WithEndpointURL(endpoint))
4954
case OtelProtocolHTTP:
5055
exporter, err = otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(endpoint))
5156
default:
52-
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
57+
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
5358
}
59+
5460
if err != nil {
5561
return nil, err
5662
}
5763

5864
spanLimits := trace.SpanLimits{
59-
AttributePerEventCountLimit: 16,
60-
EventCountLimit: 64,
65+
AttributePerEventCountLimit: OtelSpanLimitAttributePerEvent,
66+
EventCountLimit: OtelSpanLimitEventCount,
6167
}
6268

6369
traceProvider := trace.NewTracerProvider(
@@ -81,14 +87,15 @@ func newMeterProvider(ctx context.Context, config OtelConfig, serviceResource *r
8187
endpoint = config.ObservabilityEndpoint
8288
}
8389

84-
switch config.Protocol {
90+
switch strings.ToLower(config.Protocol) {
8591
case OtelProtocolGRPC:
8692
exporter, err = otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithEndpointURL(endpoint))
8793
case OtelProtocolHTTP:
8894
exporter, err = otlpmetrichttp.New(ctx, otlpmetrichttp.WithEndpointURL(endpoint))
8995
default:
90-
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
96+
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
9197
}
98+
9299
if err != nil {
93100
return nil, err
94101
}
@@ -111,14 +118,15 @@ func newLoggerProvider(ctx context.Context, config OtelConfig, serviceResource *
111118
endpoint = config.ObservabilityEndpoint
112119
}
113120

114-
switch config.Protocol {
121+
switch strings.ToLower(config.Protocol) {
115122
case OtelProtocolGRPC:
116123
exporter, err = otlploggrpc.New(ctx, otlploggrpc.WithEndpointURL(endpoint))
117124
case OtelProtocolHTTP:
118125
exporter, err = otlploghttp.New(ctx, otlploghttp.WithEndpointURL(endpoint))
119126
default:
120-
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
127+
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
121128
}
129+
122130
if err != nil {
123131
return nil, err
124132
}
@@ -137,12 +145,11 @@ func newServiceResource(ctx context.Context, name string) (*resource.Resource, e
137145
if err != nil {
138146
return nil, err
139147
}
140-
serviceName := semconv.ServiceNameKey.String(filepath.Base(providerBinary))
141-
providerName := semconv.ServiceInstanceIDKey.String(name)
148+
142149
return resource.New(ctx,
143150
resource.WithAttributes(
144-
serviceName,
145-
providerName,
151+
semconv.ServiceNameKey.String(filepath.Base(providerBinary)),
152+
semconv.ServiceInstanceIDKey.String(name),
146153
),
147154
)
148155
}

provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
wrpcnats "wrpc.io/go/nats"
2323
)
2424

25+
const (
26+
hostDataReadTimeout = 5 * time.Second
27+
)
28+
2529
type WasmcloudProvider struct {
2630
Id string
2731

@@ -92,7 +96,7 @@ func NewWithHostDataSource(source io.Reader, options ...ProviderHandler) (*Wasmc
9296
if err != nil {
9397
return nil, err
9498
}
95-
case <-time.After(5 * time.Second):
99+
case <-time.After(hostDataReadTimeout):
96100
log.Fatal("failed to read host data, did not receive after 5 seconds")
97101
}
98102

@@ -567,7 +571,6 @@ func (wp *WasmcloudProvider) isLinked(sourceId string, target string) bool {
567571
} else if target == wp.Id {
568572
_, exists := wp.targetLinks[sourceId]
569573
return exists
570-
} else {
571-
return false
572574
}
575+
return false
573576
}

secrets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *SecretStringValue) UnmarshalJSON(data []byte) error {
7272
func DecryptSecrets(encryptedBytes *[]byte, xkey nkeys.KeyPair, sender string) (map[string]SecretValue, error) {
7373
var sourceSecrets = make(map[string]SecretValue)
7474
// If the source secrets are empty or not present, we don't need to decrypt/unmarshal them
75-
if encryptedBytes != nil && len(*encryptedBytes) >= 0 {
75+
if encryptedBytes != nil && len(*encryptedBytes) != 0 {
7676
sourceSecretBytes, err := xkey.Open(*encryptedBytes, sender)
7777
if err != nil {
7878
return sourceSecrets, err

0 commit comments

Comments
 (0)