Compare commits

..

No commits in common. "0b27285b86e29c3f1ae8bb8a7748f34953bc7413" and "a5948cf334d52c03e93888d2b8df33ae697449b1" have entirely different histories.

8 changed files with 64 additions and 116 deletions

View File

@ -25,7 +25,7 @@ jobs:
if: startsWith(github.ref, 'refs/tags/v') # Only run on tag push if: startsWith(github.ref, 'refs/tags/v') # Only run on tag push
steps: steps:
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v4 uses: actions/checkout@v3
- name: Set up Go Environment - name: Set up Go Environment
uses: actions/setup-go@v4 uses: actions/setup-go@v4
@ -89,29 +89,11 @@ jobs:
VER_PKG=${{ env.VER_PKG }} VER_PKG=${{ env.VER_PKG }}
VERSION=${{ github.ref_name }} VERSION=${{ github.ref_name }}
# Detect if the helm chart was updated
chart-updated:
runs-on: ubuntu-latest
outputs:
chart-updated: ${{ steps.filter.outputs.chart }}
steps:
- uses: actions/checkout@v4
- name: Check Chart Changed
uses: dorny/paths-filter@v3
id: filter
with:
base: ${{ github.ref }}
filters: |
chart:
- **/Chart.yaml
helm-release: helm-release:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: chart-updated
if: ${{ needs.chart-updated.outputs.chart-updated == 'true' }}
steps: steps:
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v4 uses: actions/checkout@v3
- name: Install Helm - name: Install Helm
env: env:
@ -121,7 +103,7 @@ jobs:
- name: Package Chart - name: Package Chart
run: | run: |
helm package --app-version ${VERSION} ${CHART_DIR} helm package --app-version ${VERSION#v} ${CHART_DIR}
- name: Publish Chart - name: Publish Chart
env: env:

View File

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

20
main.go
View File

@ -9,16 +9,19 @@ 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() {
@ -45,6 +48,11 @@ 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,
@ -80,9 +88,13 @@ 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, aw.GetState()), Service: weathergrpc.NewGRPCWeather(ctx, state.NewWeatherState(
&state.Opts{
Ctx: ctx,
KeepLast: *aw.Config.UpdatesToKeep,
})),
}, },
}, },
}, },

View File

@ -20,63 +20,35 @@ 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
weatherState *recorder.WeatherRecorder appCtx context.Context
appCtx context.Context metrics *weather.WeatherMetrics
metrics *weather.WeatherMetrics l *zerolog.Logger
l *zerolog.Logger
*sync.RWMutex
} }
func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather { func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather {
return &AmbientWeather{ return &AmbientWeather{
Config: awConfig, Config: awConfig,
appCtx: appCtx, appCtx: appCtx,
RWMutex: &sync.RWMutex{},
} }
} }
// 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
} }
@ -106,9 +78,6 @@ 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")
@ -141,9 +110,6 @@ 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)
@ -276,10 +242,3 @@ 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/recorder" "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/state"
) )
type GRPCWeather struct { type GRPCWeather struct {
ctx context.Context ctx context.Context
recorder *recorder.WeatherRecorder state *state.WeatherState
tracer trace.Tracer tracer trace.Tracer
*pb.UnimplementedAmbientLocalWeatherServiceServer *pb.UnimplementedAmbientLocalWeatherServiceServer
} }
func NewGRPCWeather(ctx context.Context, recorder *recorder.WeatherRecorder) *GRPCWeather { func NewGRPCWeather(ctx context.Context, state *state.WeatherState) *GRPCWeather {
return &GRPCWeather{ return &GRPCWeather{
ctx: ctx, ctx: ctx,
recorder: recorder, state: state,
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.recorder.Get(ctx, limit) updates, err := w.state.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 recorder package state
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 WeatherRecorder struct { type WeatherState struct {
updates []*weather.WeatherUpdate updates []*weather.WeatherUpdate
keep int keep int
ctx context.Context ctx context.Context
@ -26,17 +26,16 @@ type Opts struct {
KeepLast int KeepLast int
} }
func NewWeatherRecorder(opts *Opts) *WeatherRecorder { func NewWeatherState(opts *Opts) *WeatherState {
if opts.KeepLast < 1 { if opts.KeepLast < 1 {
opts.KeepLast = 1 opts.KeepLast = 1
} }
return &WeatherRecorder{ return &WeatherState{
updates: make([]*weather.WeatherUpdate, 0, opts.KeepLast), updates: make([]*weather.WeatherUpdate, 0),
keep: opts.KeepLast, keep: opts.KeepLast,
ctx: opts.Ctx, ctx: opts.Ctx,
tracer: otel.GetTracer(opts.Ctx, "weatherRecorder"), tracer: otel.GetTracer(opts.Ctx, "weatherState"),
meter: otel.GetMeter(opts.Ctx, "weatherRecorder"), meter: otel.GetMeter(opts.Ctx, "weatherState"),
RWMutex: &sync.RWMutex{},
} }
} }

View File

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

View File

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