improve redis recorder
This commit is contained in:
@ -6,16 +6,28 @@ import (
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (w *WeatherRecorder) Set(ctx context.Context, u *weather.WeatherUpdate) error {
|
||||
if u.StationConfig == nil {
|
||||
u.StationConfig = &config.WeatherStation{
|
||||
Name: "unregistered",
|
||||
Equipment: "unknown",
|
||||
ProxyToAWN: false,
|
||||
ProxyToWunderground: false,
|
||||
KeepMetrics: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
ctx, span := w.tracer.Start(ctx, "setRecorderUpdate", trace.WithAttributes(
|
||||
attribute.String("stationName", u.StationConfig.Name),
|
||||
attribute.String("stationType", util.DerefStr(u.StationType)),
|
||||
attribute.String("stationEquipment", u.StationConfig.Equipment),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
return w.recorder.Set(ctx, u)
|
||||
}
|
||||
|
@ -38,8 +38,9 @@ func (r *RedisRecorder) get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Int("limit", limit))
|
||||
r.log.Debug().Int("limit", limit).Msg("getting updates from redis")
|
||||
|
||||
datas, err := r.redis.LRange(ctx, r.Key(), 0, int64(limit)).Result()
|
||||
datas, err := r.redis.LRange(ctx, r.Key(), 0, int64(limit)-1).Result()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
@ -48,6 +49,7 @@ func (r *RedisRecorder) get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
}
|
||||
|
||||
span.AddEvent("redis queried")
|
||||
r.log.Debug().Int("results", len(datas)).Msg("redis queried")
|
||||
|
||||
updates, err := jsonDatasToUpdates(datas)
|
||||
if err != nil {
|
||||
@ -87,7 +89,7 @@ func jsonDatasToUpdates(datas []string) ([]*weather.WeatherUpdate, error) {
|
||||
err := json.Unmarshal([]byte(data), update)
|
||||
errs = errors.Join(errs, err)
|
||||
|
||||
if err != nil {
|
||||
if err == nil {
|
||||
updates = append(updates, update)
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
@ -20,7 +21,7 @@ func (r *RedisRecorder) Set(ctx context.Context, u *weather.WeatherUpdate) error
|
||||
defer span.End()
|
||||
|
||||
r.Lock()
|
||||
defer r.RUnlock()
|
||||
defer r.Unlock()
|
||||
|
||||
// First ensure we can prepare our payload
|
||||
data, err := json.Marshal(u)
|
||||
@ -42,11 +43,14 @@ func (r *RedisRecorder) set(ctx context.Context, data []byte) error {
|
||||
defer span.End()
|
||||
|
||||
// Atomic, push and trim
|
||||
tx := r.redis.TxPipeline()
|
||||
tx.LPush(ctx, r.Key(), data)
|
||||
tx.LTrim(ctx, r.Key(), 0, int64(r.keep)-1)
|
||||
|
||||
if rErr, err := tx.Exec(ctx); err != nil {
|
||||
var count *redis.IntCmd
|
||||
rErr, err := r.redis.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
pipe.LPush(ctx, r.Key(), data)
|
||||
pipe.LTrim(ctx, r.Key(), 0, int64(r.keep)-1)
|
||||
count = pipe.LLen(ctx, r.Key())
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
for _, cmd := range rErr {
|
||||
span.RecordError(cmd.Err())
|
||||
}
|
||||
@ -55,7 +59,16 @@ func (r *RedisRecorder) set(ctx context.Context, data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Int("updateCount", r.count(ctx)))
|
||||
// Get new update count
|
||||
|
||||
r.log.Debug().
|
||||
Int("updateBytes", len(data)).
|
||||
Int64("updateCount", count.Val()).
|
||||
Str("redis", r.redis.String()).
|
||||
Str("key", r.Key()).
|
||||
Msg("pushed update to redis")
|
||||
|
||||
span.SetAttributes(attribute.Int64("updateCount", count.Val()))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user