diff --git a/cron/cron.go b/cron/cron.go index d533f32..e912fc8 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause + package cron import ( @@ -30,7 +33,7 @@ type state struct { } type cron struct { jobs []job - state *locker.Locked[state] + state *locker.Locked[*state] granularity time.Duration } diff --git a/env/env.go b/env/env.go index 305fb15..0f52570 100644 --- a/env/env.go +++ b/env/env.go @@ -1,7 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause + package env import ( - "log" + "log/slog" "os" "strings" ) @@ -10,10 +13,10 @@ func Default(name, defaultValue string) string { name = strings.TrimSpace(name) defaultValue = strings.TrimSpace(defaultValue) if v := strings.TrimSpace(os.Getenv(name)); v != "" { - log.Println("# ", name, "=", v) + slog.Info("env", name, v) return v } - log.Println("# ", name, "=", defaultValue, "(default)") + slog.Info("env", name, defaultValue+" (default)") return defaultValue } @@ -32,9 +35,9 @@ func Secret(name, defaultValue string) secret { name = strings.TrimSpace(name) defaultValue = strings.TrimSpace(defaultValue) if v := strings.TrimSpace(os.Getenv(name)); v != "" { - log.Println("# ", name, "=", secret(v)) + slog.Info("env", name, secret(v)) return secret(v) } - log.Println("# ", name, "=", secret(defaultValue), "(default)") + slog.Info("env", name, secret(defaultValue).String()+" (default)") return secret(defaultValue) } diff --git a/go.mod b/go.mod index ae6af4d..280e529 100644 --- a/go.mod +++ b/go.mod @@ -25,8 +25,8 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect - github.com/prometheus/common v0.42.0 // indirect - github.com/prometheus/procfs v0.10.1 // indirect + github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/procfs v0.11.0 // indirect go.opentelemetry.io/contrib v1.16.1 // indirect ) @@ -35,7 +35,7 @@ require ( github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect github.com/prometheus/client_golang v1.16.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 go.opentelemetry.io/contrib/instrumentation/runtime v0.42.0 @@ -46,10 +46,10 @@ require ( go.opentelemetry.io/otel/sdk v1.16.0 go.opentelemetry.io/otel/trace v1.16.0 go.opentelemetry.io/proto/otlp v0.19.0 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/sys v0.8.0 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/sys v0.10.0 // indirect golang.org/x/text v0.9.0 // indirect google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect google.golang.org/grpc v1.55.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go.sum b/go.sum index a1362a8..bc9375c 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru/v2 v2.0.3 h1:kmRrRLlInXvng0SmLxmQpQkpbYAvcXm7NPDrgxJa9mE= @@ -173,10 +173,8 @@ github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lF github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= -github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM= -github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= -github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= -github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY= +github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk= github.com/ravilushqa/otelgqlgen v0.13.1 h1:V+zFE75iDd2/CSzy5kKnb+Fi09SsE5535wv9U2nUEFE= github.com/ravilushqa/otelgqlgen v0.13.1/go.mod h1:ZIyWykK2paCuNi9k8gk5edcNSwDJuxZaW90vZXpafxw= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -290,8 +288,7 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -336,8 +333,7 @@ golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -480,8 +476,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/lg/tracer.go b/lg/tracer.go index fecae6c..6f43764 100644 --- a/lg/tracer.go +++ b/lg/tracer.go @@ -1,9 +1,13 @@ +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause + package lg import ( "context" "fmt" "log" + "log/slog" "net/http" "runtime" "strconv" @@ -50,10 +54,51 @@ func attrs(ctx context.Context) (string, []attribute.KeyValue) { return name, attrs } +type wrapSpan struct { + trace.Span +} + +func (w wrapSpan) AddEvent(name string, options ...trace.EventOption) { + w.Span.AddEvent(name, options...) + + cfg := trace.NewEventConfig(options...) + + attrs := cfg.Attributes() + args := make([]any, len(attrs)*2) + + for i, a := range attrs { + args[2*i] = a.Key + args[2*i+1] = a.Value + } + + slog.Info(name, args...) +} + +func (w wrapSpan) RecordError(err error, options ...trace.EventOption) { + w.Span.RecordError(err, options...) + + if err == nil { + return + } + cfg := trace.NewEventConfig(options...) + + attrs := cfg.Attributes() + args := make([]any, len(attrs)*2) + + for i, a := range attrs { + args[2*i] = a.Key + args[2*i+1] = a.Value + } + + slog.Error(err.Error(), args...) +} + func Span(ctx context.Context, opts ...trace.SpanStartOption) (context.Context, trace.Span) { name, attrs := attrs(ctx) attrs = append(attrs, attribute.String("name", name)) ctx, span := Tracer(ctx).Start(ctx, name, opts...) + span = &wrapSpan{span} + span.SetAttributes(attrs...) return ctx, span diff --git a/locker/locker.go b/locker/locker.go index 9b5441e..ef3adf5 100644 --- a/locker/locker.go +++ b/locker/locker.go @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause + package locker import ( @@ -10,13 +13,13 @@ import ( ) type Locked[T any] struct { - state chan *T + state chan T } // New creates a new locker for the given value. -func New[T any](initial *T) *Locked[T] { +func New[T any](initial T) *Locked[T] { s := &Locked[T]{} - s.state = make(chan *T, 1) + s.state = make(chan T, 1) s.state <- initial return s } @@ -24,7 +27,7 @@ func New[T any](initial *T) *Locked[T] { type ctxKey struct{ name string } // Use will call the function with the locked value -func (s *Locked[T]) Use(ctx context.Context, fn func(context.Context, *T) error) error { +func (s *Locked[T]) Use(ctx context.Context, fn func(context.Context, T) error) error { if s == nil { return fmt.Errorf("locker not initialized") } @@ -61,10 +64,8 @@ func (s *Locked[T]) Use(ctx context.Context, fn func(context.Context, *T) error) func (s *Locked[T]) Copy(ctx context.Context) (T, error) { var t T - err := s.Use(ctx, func(ctx context.Context, c *T) error { - if c != nil { - t = *c - } + err := s.Use(ctx, func(ctx context.Context, c T) error { + t = c return nil }) diff --git a/xdg/path_darwin.go b/xdg/path_darwin.go index 10c2ba1..08ad5fa 100644 --- a/xdg/path_darwin.go +++ b/xdg/path_darwin.go @@ -1,5 +1,7 @@ //go:build darwin // +build darwin +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause package xdg diff --git a/xdg/path_linux.go b/xdg/path_linux.go index ca8cffe..b0f29b9 100644 --- a/xdg/path_linux.go +++ b/xdg/path_linux.go @@ -1,5 +1,7 @@ //go:build linux // +build linux +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause package xdg diff --git a/xdg/path_windows.go b/xdg/path_windows.go index 6886bf0..6b8a4ff 100644 --- a/xdg/path_windows.go +++ b/xdg/path_windows.go @@ -1,5 +1,7 @@ //go:build windows // +build windows +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause package xdg diff --git a/xdg/xdg.go b/xdg/xdg.go index 8f2f709..70a27af 100644 --- a/xdg/xdg.go +++ b/xdg/xdg.go @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2023 Jon Lundy +// SPDX-License-Identifier: BSD-3-Clause + package xdg import (