diff --git a/host.go b/host.go index 832fa7e..44fbcfe 100644 --- a/host.go +++ b/host.go @@ -2,11 +2,6 @@ package provider import "encoding/json" -const ( - OtelProtocolHTTP = "Http" - OtelProtocolGRPC = "Grpc" -) - type RedactedString string func (rs RedactedString) String() string { diff --git a/observability.go b/observability.go index 65677af..90d21ba 100644 --- a/observability.go +++ b/observability.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" @@ -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 { @@ -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( @@ -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 } @@ -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 } @@ -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), ), ) } diff --git a/provider.go b/provider.go index 5825e8a..c504a6d 100644 --- a/provider.go +++ b/provider.go @@ -22,6 +22,10 @@ import ( wrpcnats "wrpc.io/go/nats" ) +const ( + hostDataReadTimeout = 5 * time.Second +) + type WasmcloudProvider struct { Id string @@ -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") } @@ -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 } diff --git a/secrets.go b/secrets.go index f227a54..a4ed233 100644 --- a/secrets.go +++ b/secrets.go @@ -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