Skip to content

Commit

Permalink
chore: Minor cleanup (#39)
Browse files Browse the repository at this point in the history
Signed-off-by: Joonas Bergius <joonas@bergi.us>
  • Loading branch information
joonas committed Sep 23, 2024
1 parent fd7d2c9 commit 1d03a52
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
5 changes: 0 additions & 5 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package provider

import "encoding/json"

const (
OtelProtocolHTTP = "Http"
OtelProtocolGRPC = "Grpc"
)

type RedactedString string

func (rs RedactedString) String() string {
Expand Down
37 changes: 22 additions & 15 deletions observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

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

const (
OtelMetricExportInterval = 1 * time.Minute
OtelTraceExportInterval = 1 * time.Minute
OtelLogExportInterval = 10 * time.Second
OtelMetricExportInterval = 1 * time.Minute
OtelTraceExportInterval = 1 * time.Minute
OtelLogExportInterval = 10 * time.Second
OtelProtocolHTTP = "http"
OtelProtocolGRPC = "grpc"
OtelSpanLimitAttributePerEvent = 16
OtelSpanLimitEventCount = 64
)

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

switch config.Protocol {
switch strings.ToLower(config.Protocol) {
case OtelProtocolGRPC:
exporter, err = otlptracegrpc.New(ctx, otlptracegrpc.WithEndpointURL(endpoint))
case OtelProtocolHTTP:
exporter, err = otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(endpoint))
default:
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}

if err != nil {
return nil, err
}

spanLimits := trace.SpanLimits{
AttributePerEventCountLimit: 16,
EventCountLimit: 64,
AttributePerEventCountLimit: OtelSpanLimitAttributePerEvent,
EventCountLimit: OtelSpanLimitEventCount,
}

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

switch config.Protocol {
switch strings.ToLower(config.Protocol) {
case OtelProtocolGRPC:
exporter, err = otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithEndpointURL(endpoint))
case OtelProtocolHTTP:
exporter, err = otlpmetrichttp.New(ctx, otlpmetrichttp.WithEndpointURL(endpoint))
default:
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}

if err != nil {
return nil, err
}
Expand All @@ -111,14 +118,15 @@ func newLoggerProvider(ctx context.Context, config OtelConfig, serviceResource *
endpoint = config.ObservabilityEndpoint
}

switch config.Protocol {
switch strings.ToLower(config.Protocol) {
case OtelProtocolGRPC:
exporter, err = otlploggrpc.New(ctx, otlploggrpc.WithEndpointURL(endpoint))
case OtelProtocolHTTP:
exporter, err = otlploghttp.New(ctx, otlploghttp.WithEndpointURL(endpoint))
default:
return nil, fmt.Errorf("unknown observability protocol '%s'", config.Protocol)
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}

if err != nil {
return nil, err
}
Expand All @@ -137,12 +145,11 @@ func newServiceResource(ctx context.Context, name string) (*resource.Resource, e
if err != nil {
return nil, err
}
serviceName := semconv.ServiceNameKey.String(filepath.Base(providerBinary))
providerName := semconv.ServiceInstanceIDKey.String(name)

return resource.New(ctx,
resource.WithAttributes(
serviceName,
providerName,
semconv.ServiceNameKey.String(filepath.Base(providerBinary)),
semconv.ServiceInstanceIDKey.String(name),
),
)
}
9 changes: 6 additions & 3 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
wrpcnats "wrpc.io/go/nats"
)

const (
hostDataReadTimeout = 5 * time.Second
)

type WasmcloudProvider struct {
Id string

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

Expand Down Expand Up @@ -567,7 +571,6 @@ func (wp *WasmcloudProvider) isLinked(sourceId string, target string) bool {
} else if target == wp.Id {
_, exists := wp.targetLinks[sourceId]
return exists
} else {
return false
}
return false
}
2 changes: 1 addition & 1 deletion secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *SecretStringValue) UnmarshalJSON(data []byte) error {
func DecryptSecrets(encryptedBytes *[]byte, xkey nkeys.KeyPair, sender string) (map[string]SecretValue, error) {
var sourceSecrets = make(map[string]SecretValue)
// If the source secrets are empty or not present, we don't need to decrypt/unmarshal them
if encryptedBytes != nil && len(*encryptedBytes) >= 0 {
if encryptedBytes != nil && len(*encryptedBytes) != 0 {
sourceSecretBytes, err := xkey.Open(*encryptedBytes, sender)
if err != nil {
return sourceSecrets, err
Expand Down

0 comments on commit 1d03a52

Please sign in to comment.