refactor state to recorder, finish implementing

This commit is contained in:
Ryan McGuire 2025-03-21 09:22:32 -04:00
parent a5948cf334
commit 42eea2346b
7 changed files with 89 additions and 56 deletions

View File

@ -3,7 +3,8 @@ services:
ambient-local-exporter: ambient-local-exporter:
build: . build: .
ports: ports:
- 8080:8080 - 8080:8080 # HTTP
- 8081:8081 # GRPC
volumes: volumes:
- ./config.yaml:/app/config.yaml - ./config.yaml:/app/config.yaml
command: command:

20
main.go
View File

@ -9,19 +9,16 @@ import (
grpcopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts" grpcopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts" httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"k8s.io/utils/ptr"
weatherpb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather" weatherpb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/ambienthttp" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/ambienthttp"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
weathergrpc "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/grpc" weathergrpc "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/grpc"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/state"
) )
const ( const (
defaultMetricPrefix = "weather" defaultMetricPrefix = "weather"
defaultUpdatesToKeep = 120
) )
func main() { func main() {
@ -48,11 +45,6 @@ func main() {
} }
func prepareApp(ctx context.Context, aw *ambient.AmbientWeather) *app.App { func prepareApp(ctx context.Context, aw *ambient.AmbientWeather) *app.App {
// Config updates / defaults
if aw.Config.UpdatesToKeep == nil || *aw.Config.UpdatesToKeep < 1 {
aw.Config.UpdatesToKeep = ptr.To(defaultUpdatesToKeep)
}
// Load ambient routes into app // Load ambient routes into app
awApp := &app.App{ awApp := &app.App{
AppContext: ctx, AppContext: ctx,
@ -88,13 +80,9 @@ func prepareApp(ctx context.Context, aw *ambient.AmbientWeather) *app.App {
GRPC: &grpcopts.AppGRPC{ GRPC: &grpcopts.AppGRPC{
Services: []*grpcopts.GRPCService{ Services: []*grpcopts.GRPCService{
{ {
Name: "Weather Service", Name: "Weather Service",
Type: &weatherpb.AmbientLocalWeatherService_ServiceDesc, Type: &weatherpb.AmbientLocalWeatherService_ServiceDesc,
Service: weathergrpc.NewGRPCWeather(ctx, state.NewWeatherState( Service: weathergrpc.NewGRPCWeather(ctx, aw.GetState()),
&state.Opts{
Ctx: ctx,
KeepLast: *aw.Config.UpdatesToKeep,
})),
}, },
}, },
}, },

View File

@ -20,19 +20,24 @@ import (
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/awn" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/awn"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/wunderground" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/wunderground"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder"
) )
const defUpdatesToKeep = 120
type AmbientWeather struct { type AmbientWeather struct {
// These providers implement support for the update sent // These providers implement support for the update sent
// when either "AmbientWeather" or "Wunderground" are selected // when either "AmbientWeather" or "Wunderground" are selected
// in the "Custom" section of the AWNet app, or the web UI // in the "Custom" section of the AWNet app, or the web UI
// of an Ambient WeatherHub // of an Ambient WeatherHub
Config *config.AmbientLocalExporterConfig Config *config.AmbientLocalExporterConfig
awnProvider provider.AmbientProvider awnProvider provider.AmbientProvider
wuProvider provider.AmbientProvider wuProvider provider.AmbientProvider
appCtx context.Context weatherState *recorder.WeatherRecorder
metrics *weather.WeatherMetrics appCtx context.Context
l *zerolog.Logger metrics *weather.WeatherMetrics
l *zerolog.Logger
*sync.RWMutex
} }
func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather { func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather {
@ -44,11 +49,33 @@ func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *A
// Initialize with defaults, set logger from context // Initialize with defaults, set logger from context
func (aw *AmbientWeather) Init() *AmbientWeather { func (aw *AmbientWeather) Init() *AmbientWeather {
tracer := otel.GetTracer(aw.appCtx, "ambientWeather")
_, span := tracer.Start(aw.appCtx, "ambientWeather.init",
trace.WithAttributes(
attribute.String("name", aw.Config.Name),
attribute.Bool("grpcEnabled", aw.Config.GRPCEnabled()),
attribute.Bool("httpEnabled", aw.Config.HTTPEnabled()),
attribute.Int("weatherStations", len(aw.Config.WeatherStations)),
))
defer span.End()
aw.awnProvider = &awn.AWNProvider{} aw.awnProvider = &awn.AWNProvider{}
aw.wuProvider = &wunderground.WUProvider{} aw.wuProvider = &wunderground.WUProvider{}
aw.l = zerolog.Ctx(aw.appCtx) aw.l = zerolog.Ctx(aw.appCtx)
updatesToKeep := defUpdatesToKeep
if aw.Config.UpdatesToKeep != nil && *aw.Config.UpdatesToKeep > 0 {
updatesToKeep = *aw.Config.UpdatesToKeep
}
span.SetAttributes(attribute.Int("updatesToKeep", updatesToKeep))
aw.weatherState = recorder.NewWeatherRecorder(&recorder.Opts{
Ctx: aw.appCtx,
KeepLast: updatesToKeep,
})
aw.l.Trace().Any("awConfig", aw.Config).Send() aw.l.Trace().Any("awConfig", aw.Config).Send()
span.SetStatus(codes.Ok, "")
return aw return aw
} }
@ -78,6 +105,9 @@ func (aw *AmbientWeather) handleProviderRequest(
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
) { ) {
aw.Lock()
defer aw.Unlock()
l := zerolog.Ctx(aw.appCtx) l := zerolog.Ctx(aw.appCtx)
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler") tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
@ -110,6 +140,9 @@ func (aw *AmbientWeather) handleProviderRequest(
updateSpan.SetAttributes(attribute.String("stationName", update.StationConfig.Name)) updateSpan.SetAttributes(attribute.String("stationName", update.StationConfig.Name))
} }
// Record state
aw.weatherState.Set(ctx, update)
// Update metrics // Update metrics
aw.metricsUpdate(ctx, p, update) aw.metricsUpdate(ctx, p, update)
@ -242,3 +275,10 @@ func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
} }
} }
} }
func (aw *AmbientWeather) GetState() *recorder.WeatherRecorder {
aw.RLock()
defer aw.RUnlock()
return aw.weatherState
}

View File

@ -13,21 +13,21 @@ import (
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel" "gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather" pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/state" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder"
) )
type GRPCWeather struct { type GRPCWeather struct {
ctx context.Context ctx context.Context
state *state.WeatherState recorder *recorder.WeatherRecorder
tracer trace.Tracer tracer trace.Tracer
*pb.UnimplementedAmbientLocalWeatherServiceServer *pb.UnimplementedAmbientLocalWeatherServiceServer
} }
func NewGRPCWeather(ctx context.Context, state *state.WeatherState) *GRPCWeather { func NewGRPCWeather(ctx context.Context, recorder *recorder.WeatherRecorder) *GRPCWeather {
return &GRPCWeather{ return &GRPCWeather{
ctx: ctx, ctx: ctx,
state: state, recorder: recorder,
tracer: otel.GetTracer(ctx, "grpcWeather"), tracer: otel.GetTracer(ctx, "grpcWeather"),
} }
} }
@ -46,7 +46,7 @@ func (w *GRPCWeather) GetWeather(ctx context.Context, req *pb.GetWeatherRequest)
span.SetAttributes(attribute.Int("limit", limit)) span.SetAttributes(attribute.Int("limit", limit))
updates, err := w.state.Get(ctx, limit) updates, err := w.recorder.Get(ctx, limit)
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)
span.SetStatus(otelcodes.Error, err.Error()) span.SetStatus(otelcodes.Error, err.Error())

View File

@ -1,4 +1,4 @@
package state package recorder
import ( import (
"context" "context"
@ -12,7 +12,7 @@ import (
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
) )
type WeatherState struct { type WeatherRecorder struct {
updates []*weather.WeatherUpdate updates []*weather.WeatherUpdate
keep int keep int
ctx context.Context ctx context.Context
@ -26,16 +26,17 @@ type Opts struct {
KeepLast int KeepLast int
} }
func NewWeatherState(opts *Opts) *WeatherState { func NewWeatherRecorder(opts *Opts) *WeatherRecorder {
if opts.KeepLast < 1 { if opts.KeepLast < 1 {
opts.KeepLast = 1 opts.KeepLast = 1
} }
return &WeatherState{ return &WeatherRecorder{
updates: make([]*weather.WeatherUpdate, 0), updates: make([]*weather.WeatherUpdate, 0, opts.KeepLast),
keep: opts.KeepLast, keep: opts.KeepLast,
ctx: opts.Ctx, ctx: opts.Ctx,
tracer: otel.GetTracer(opts.Ctx, "weatherState"), tracer: otel.GetTracer(opts.Ctx, "weatherRecorder"),
meter: otel.GetMeter(opts.Ctx, "weatherState"), meter: otel.GetMeter(opts.Ctx, "weatherRecorder"),
RWMutex: &sync.RWMutex{},
} }
} }

View File

@ -1,4 +1,4 @@
package state package recorder
import ( import (
"context" "context"
@ -12,14 +12,17 @@ import (
) )
// Returns last requested number of weather updates // Returns last requested number of weather updates
func (w *WeatherState) Get(ctx context.Context, last int) ( // If negative number given, will return all weather observations
func (w *WeatherRecorder) Get(ctx context.Context, last int) (
[]*weather.WeatherUpdate, error, []*weather.WeatherUpdate, error,
) { ) {
if last < 1 { if last < 0 {
last = w.keep
} else if last < 1 {
last = 1 last = 1
} }
ctx, span := w.tracer.Start(ctx, "getWeatherState") ctx, span := w.tracer.Start(ctx, "getWeatherRecorder")
span.SetAttributes( span.SetAttributes(
attribute.Int("last", last), attribute.Int("last", last),
attribute.Int("keep", w.keep), attribute.Int("keep", w.keep),
@ -38,7 +41,7 @@ func (w *WeatherState) Get(ctx context.Context, last int) (
return updates, err return updates, err
} }
func (w *WeatherState) get(ctx context.Context, last int) ( func (w *WeatherRecorder) get(ctx context.Context, last int) (
[]*weather.WeatherUpdate, error, []*weather.WeatherUpdate, error,
) { ) {
span := trace.SpanFromContext(ctx) span := trace.SpanFromContext(ctx)
@ -46,16 +49,16 @@ func (w *WeatherState) get(ctx context.Context, last int) (
w.RLock() w.RLock()
defer w.Unlock() defer w.Unlock()
span.AddEvent("acquired lock on state cache") span.AddEvent("acquired lock on recorder cache")
updates := w.updates updates := w.updates
if w.count() == 0 { if w.count() == 0 {
err := errors.New("no state to get") err := errors.New("no recorded updates to get")
span.RecordError(err) span.RecordError(err)
return nil, err return nil, err
} else if w.count() <= last { } else if w.count() <= last {
span.RecordError(errors.New("requested more state than exists")) span.RecordError(errors.New("requested more updates than recorded"))
} else { } else {
updates = updates[len(updates)-last:] updates = updates[len(updates)-last:]
} }
@ -67,8 +70,8 @@ func (w *WeatherState) get(ctx context.Context, last int) (
} }
// Returns count of retained weather updates // Returns count of retained weather updates
func (w *WeatherState) Count() int { func (w *WeatherRecorder) Count() int {
_, span := w.tracer.Start(w.ctx, "countWeatherState") _, span := w.tracer.Start(w.ctx, "countWeatherRecorder")
defer span.End() defer span.End()
count := w.count() count := w.count()
@ -79,7 +82,7 @@ func (w *WeatherState) Count() int {
return count return count
} }
func (w *WeatherState) count() int { func (w *WeatherRecorder) count() int {
w.RLock() w.RLock()
defer w.RUnlock() defer w.RUnlock()

View File

@ -1,4 +1,4 @@
package state package recorder
import ( import (
"context" "context"
@ -9,8 +9,8 @@ import (
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
) )
func (w *WeatherState) Set(ctx context.Context, u *weather.WeatherUpdate) error { func (w *WeatherRecorder) Set(ctx context.Context, u *weather.WeatherUpdate) error {
_, span := w.tracer.Start(ctx, "setWeatherState") _, span := w.tracer.Start(ctx, "recordWeatherUpdate")
span.SetAttributes( span.SetAttributes(
attribute.Int("countWeatherUpdates", w.Count()), attribute.Int("countWeatherUpdates", w.Count()),
attribute.Int("keepUpdates", w.keep), attribute.Int("keepUpdates", w.keep),
@ -20,16 +20,16 @@ func (w *WeatherState) Set(ctx context.Context, u *weather.WeatherUpdate) error
return w.set(span, u) return w.set(span, u)
} }
func (w *WeatherState) set(span trace.Span, u *weather.WeatherUpdate) error { func (w *WeatherRecorder) set(span trace.Span, u *weather.WeatherUpdate) error {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
if len(w.updates) > w.keep { if len(w.updates) > w.keep {
w.updates = w.updates[1:] w.updates = w.updates[1:]
span.AddEvent("trimmed state updates by 1") span.AddEvent("trimmed recorded updates by 1")
} }
w.updates = append(w.updates, u) w.updates = append(w.updates, u)
span.AddEvent("recorded weather state") span.AddEvent("recorded weather update")
return nil return nil
} }