Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
f079d106a1 | |||
fc2d05e957 | |||
59f5bfbba1 | |||
0adab13221 | |||
3b0748da25 | |||
2d683e61b0 | |||
82bc3acfc3 | |||
6e1ec0b5c1 | |||
46a213f314 | |||
b483fc22a3 | |||
fb6941e6bd | |||
a5abbbec1f | |||
e9b70fe6e0 | |||
ca18eba167 | |||
c020c4eec8 | |||
df7c2dff40 | |||
0b27285b86 | |||
1f097b1fd7 | |||
42eea2346b | |||
a5948cf334 | |||
deb831a2c5 |
@ -2,6 +2,7 @@ name: Build and Publish
|
||||
on:
|
||||
push:
|
||||
tags: ["v*"]
|
||||
branches: ["main"]
|
||||
|
||||
env:
|
||||
PACKAGE_NAME: ambient-local-exporter
|
||||
@ -21,9 +22,10 @@ env:
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
if: startsWith(github.ref, 'refs/tags/v') # Only run on tag push
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go Environment
|
||||
uses: actions/setup-go@v4
|
||||
@ -87,12 +89,29 @@ jobs:
|
||||
VER_PKG=${{ env.VER_PKG }}
|
||||
VERSION=${{ github.ref_name }}
|
||||
|
||||
# Detect if the helm chart was updated
|
||||
check-chart:
|
||||
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:
|
||||
- helm/ambient-local-exporter/Chart.yaml
|
||||
|
||||
helm-release:
|
||||
runs-on: ubuntu-latest
|
||||
needs: release
|
||||
needs: check-chart
|
||||
if: ${{ needs.check-chart.outputs.chart-updated == 'true' }}
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Helm
|
||||
env:
|
||||
@ -102,7 +121,7 @@ jobs:
|
||||
|
||||
- name: Package Chart
|
||||
run: |
|
||||
helm package --app-version ${VERSION#v} ${CHART_DIR}
|
||||
helm package --app-version ${VERSION} ${CHART_DIR}
|
||||
|
||||
- name: Publish Chart
|
||||
env:
|
||||
|
3
Makefile
3
Makefile
@ -33,6 +33,9 @@ build: test
|
||||
done
|
||||
go build -ldflags "-X $(VER_PKG)=$(VERSION)" -o bin/${CMD_NAME}
|
||||
|
||||
schema:
|
||||
go run . -schema > contrib/schema.json
|
||||
|
||||
docker:
|
||||
@echo "Building Docker image $(DOCKER_IMG):$(VERSION)"
|
||||
docker build \
|
||||
|
10
TODO.md
10
TODO.md
@ -1,10 +1,16 @@
|
||||
# Issues
|
||||
- [x] Redis recorder panic
|
||||
|
||||
# TODO
|
||||
- [ ] Finish implementing weather GRPC
|
||||
- [ ] Add json schema to CI and README
|
||||
- [ ] Update README
|
||||
- [ ] Add Grafana dashboard
|
||||
- [ ] Add new spans
|
||||
|
||||
## Done
|
||||
- [x] Stop Marshaling sensors / batteries with no data
|
||||
- [x] Finish implementing weather GRPC
|
||||
- [x] Add json schema for config
|
||||
- [x] Add new spans
|
||||
- [x] Helm Chart
|
||||
- [x] Add proxy to upstream support
|
||||
- [x] Fix wunderground 401
|
||||
|
@ -207,6 +207,7 @@ type WeatherUpdate struct {
|
||||
DewPointF *float64 `protobuf:"fixed64,25,opt,name=dew_point_f,json=dewPointF,proto3,oneof" json:"dew_point_f,omitempty"`
|
||||
WindChillF *float64 `protobuf:"fixed64,26,opt,name=wind_chill_f,json=windChillF,proto3,oneof" json:"wind_chill_f,omitempty"`
|
||||
TempHumiditySensors []*TempHumiditySensor `protobuf:"bytes,27,rep,name=temp_humidity_sensors,json=tempHumiditySensors,proto3" json:"temp_humidity_sensors,omitempty"`
|
||||
UpdateTimestamp *timestamppb.Timestamp `protobuf:"bytes,28,opt,name=update_timestamp,json=updateTimestamp,proto3" json:"update_timestamp,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@ -430,6 +431,13 @@ func (x *WeatherUpdate) GetTempHumiditySensors() []*TempHumiditySensor {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *WeatherUpdate) GetUpdateTimestamp() *timestamppb.Timestamp {
|
||||
if x != nil {
|
||||
return x.UpdateTimestamp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Represents a temperature and humidity sensor
|
||||
type TempHumiditySensor struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
@ -576,7 +584,7 @@ var file_weather_weather_proto_rawDesc = string([]byte{
|
||||
0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x01, 0x52, 0x0b, 0x73, 0x74, 0x61, 0x74,
|
||||
0x69, 0x6f, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x73,
|
||||
0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x42, 0x0f, 0x0a, 0x0d, 0x5f,
|
||||
0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x22, 0xb7, 0x0c, 0x0a,
|
||||
0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x22, 0xfe, 0x0c, 0x0a,
|
||||
0x0d, 0x57, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x12, 0x21,
|
||||
0x0a, 0x0c, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x4e, 0x61, 0x6d,
|
||||
@ -651,51 +659,56 @@ var file_weather_weather_proto_rawDesc = string([]byte{
|
||||
0x65, 0x6e, 0x74, 0x2e, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x2e, 0x54, 0x65, 0x6d, 0x70,
|
||||
0x48, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x52, 0x13,
|
||||
0x74, 0x65, 0x6d, 0x70, 0x48, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x53, 0x65, 0x6e, 0x73,
|
||||
0x6f, 0x72, 0x73, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x6f, 0x75, 0x74,
|
||||
0x64, 0x6f, 0x6f, 0x72, 0x5f, 0x66, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x74, 0x65, 0x6d, 0x70, 0x5f,
|
||||
0x69, 0x6e, 0x64, 0x6f, 0x6f, 0x72, 0x5f, 0x66, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x68, 0x75, 0x6d,
|
||||
0x69, 0x64, 0x69, 0x74, 0x79, 0x5f, 0x6f, 0x75, 0x74, 0x64, 0x6f, 0x6f, 0x72, 0x42, 0x12, 0x0a,
|
||||
0x10, 0x5f, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x5f, 0x69, 0x6e, 0x64, 0x6f, 0x6f,
|
||||
0x72, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x73, 0x70, 0x65, 0x65, 0x64,
|
||||
0x5f, 0x6d, 0x70, 0x68, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x67, 0x75,
|
||||
0x73, 0x74, 0x5f, 0x6d, 0x70, 0x68, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x6d, 0x61, 0x78, 0x5f, 0x64,
|
||||
0x61, 0x69, 0x6c, 0x79, 0x5f, 0x67, 0x75, 0x73, 0x74, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x77, 0x69,
|
||||
0x6e, 0x64, 0x5f, 0x64, 0x69, 0x72, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f,
|
||||
0x64, 0x69, 0x72, 0x5f, 0x61, 0x76, 0x67, 0x5f, 0x31, 0x30, 0x6d, 0x42, 0x05, 0x0a, 0x03, 0x5f,
|
||||
0x75, 0x76, 0x42, 0x12, 0x0a, 0x10, 0x5f, 0x73, 0x6f, 0x6c, 0x61, 0x72, 0x5f, 0x72, 0x61, 0x64,
|
||||
0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x68, 0x6f, 0x75, 0x72, 0x6c,
|
||||
0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x65, 0x76,
|
||||
0x65, 0x6e, 0x74, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x10, 0x0a, 0x0e, 0x5f,
|
||||
0x64, 0x61, 0x69, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x11, 0x0a,
|
||||
0x0f, 0x5f, 0x77, 0x65, 0x65, 0x6b, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e,
|
||||
0x42, 0x12, 0x0a, 0x10, 0x5f, 0x6d, 0x6f, 0x6e, 0x74, 0x68, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69,
|
||||
0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x79, 0x65, 0x61, 0x72, 0x6c, 0x79, 0x5f,
|
||||
0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x74, 0x6f, 0x74, 0x61,
|
||||
0x6c, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x14, 0x0a, 0x12, 0x5f, 0x62, 0x61,
|
||||
0x72, 0x6f, 0x6d, 0x5f, 0x72, 0x65, 0x6c, 0x61, 0x74, 0x69, 0x76, 0x65, 0x5f, 0x69, 0x6e, 0x42,
|
||||
0x14, 0x0a, 0x12, 0x5f, 0x62, 0x61, 0x72, 0x6f, 0x6d, 0x5f, 0x61, 0x62, 0x73, 0x6f, 0x6c, 0x75,
|
||||
0x74, 0x65, 0x5f, 0x69, 0x6e, 0x42, 0x0e, 0x0a, 0x0c, 0x5f, 0x64, 0x65, 0x77, 0x5f, 0x70, 0x6f,
|
||||
0x69, 0x6e, 0x74, 0x5f, 0x66, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x63,
|
||||
0x68, 0x69, 0x6c, 0x6c, 0x5f, 0x66, 0x22, 0x7d, 0x0a, 0x12, 0x54, 0x65, 0x6d, 0x70, 0x48, 0x75,
|
||||
0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x12, 0x12, 0x0a, 0x04,
|
||||
0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65,
|
||||
0x12, 0x1a, 0x0a, 0x06, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x66, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01,
|
||||
0x48, 0x00, 0x52, 0x05, 0x74, 0x65, 0x6d, 0x70, 0x46, 0x88, 0x01, 0x01, 0x12, 0x1f, 0x0a, 0x08,
|
||||
0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x48, 0x01,
|
||||
0x52, 0x08, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a,
|
||||
0x07, 0x5f, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x66, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x68, 0x75, 0x6d,
|
||||
0x69, 0x64, 0x69, 0x74, 0x79, 0x22, 0x55, 0x0a, 0x0d, 0x42, 0x61, 0x74, 0x74, 0x65, 0x72, 0x79,
|
||||
0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e,
|
||||
0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x6f, 0x6d, 0x70, 0x6f,
|
||||
0x6e, 0x65, 0x6e, 0x74, 0x12, 0x1b, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02,
|
||||
0x20, 0x01, 0x28, 0x05, 0x48, 0x00, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x88, 0x01,
|
||||
0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x54, 0x5a, 0x52,
|
||||
0x67, 0x69, 0x74, 0x65, 0x61, 0x2e, 0x6c, 0x69, 0x62, 0x72, 0x65, 0x74, 0x65, 0x63, 0x68, 0x63,
|
||||
0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x6d,
|
||||
0x63, 0x67, 0x75, 0x69, 0x72, 0x65, 0x2f, 0x61, 0x6d, 0x62, 0x69, 0x65, 0x6e, 0x74, 0x2d, 0x6c,
|
||||
0x6f, 0x63, 0x61, 0x6c, 0x2d, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x72, 0x2f, 0x61, 0x70,
|
||||
0x69, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x31, 0x2f, 0x77, 0x65, 0x61, 0x74, 0x68,
|
||||
0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x6f, 0x72, 0x73, 0x12, 0x45, 0x0a, 0x10, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69,
|
||||
0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x1c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e,
|
||||
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e,
|
||||
0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0f, 0x75, 0x70, 0x64, 0x61, 0x74,
|
||||
0x65, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x74,
|
||||
0x65, 0x6d, 0x70, 0x5f, 0x6f, 0x75, 0x74, 0x64, 0x6f, 0x6f, 0x72, 0x5f, 0x66, 0x42, 0x10, 0x0a,
|
||||
0x0e, 0x5f, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x69, 0x6e, 0x64, 0x6f, 0x6f, 0x72, 0x5f, 0x66, 0x42,
|
||||
0x13, 0x0a, 0x11, 0x5f, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x5f, 0x6f, 0x75, 0x74,
|
||||
0x64, 0x6f, 0x6f, 0x72, 0x42, 0x12, 0x0a, 0x10, 0x5f, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74,
|
||||
0x79, 0x5f, 0x69, 0x6e, 0x64, 0x6f, 0x6f, 0x72, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x77, 0x69, 0x6e,
|
||||
0x64, 0x5f, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, 0x6d, 0x70, 0x68, 0x42, 0x10, 0x0a, 0x0e, 0x5f,
|
||||
0x77, 0x69, 0x6e, 0x64, 0x5f, 0x67, 0x75, 0x73, 0x74, 0x5f, 0x6d, 0x70, 0x68, 0x42, 0x11, 0x0a,
|
||||
0x0f, 0x5f, 0x6d, 0x61, 0x78, 0x5f, 0x64, 0x61, 0x69, 0x6c, 0x79, 0x5f, 0x67, 0x75, 0x73, 0x74,
|
||||
0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x64, 0x69, 0x72, 0x42, 0x13, 0x0a,
|
||||
0x11, 0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x64, 0x69, 0x72, 0x5f, 0x61, 0x76, 0x67, 0x5f, 0x31,
|
||||
0x30, 0x6d, 0x42, 0x05, 0x0a, 0x03, 0x5f, 0x75, 0x76, 0x42, 0x12, 0x0a, 0x10, 0x5f, 0x73, 0x6f,
|
||||
0x6c, 0x61, 0x72, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x11, 0x0a,
|
||||
0x0f, 0x5f, 0x68, 0x6f, 0x75, 0x72, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e,
|
||||
0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f,
|
||||
0x69, 0x6e, 0x42, 0x10, 0x0a, 0x0e, 0x5f, 0x64, 0x61, 0x69, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69,
|
||||
0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x11, 0x0a, 0x0f, 0x5f, 0x77, 0x65, 0x65, 0x6b, 0x6c, 0x79, 0x5f,
|
||||
0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x12, 0x0a, 0x10, 0x5f, 0x6d, 0x6f, 0x6e, 0x74,
|
||||
0x68, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x11, 0x0a, 0x0f, 0x5f,
|
||||
0x79, 0x65, 0x61, 0x72, 0x6c, 0x79, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e, 0x42, 0x10,
|
||||
0x0a, 0x0e, 0x5f, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x5f, 0x72, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x6e,
|
||||
0x42, 0x14, 0x0a, 0x12, 0x5f, 0x62, 0x61, 0x72, 0x6f, 0x6d, 0x5f, 0x72, 0x65, 0x6c, 0x61, 0x74,
|
||||
0x69, 0x76, 0x65, 0x5f, 0x69, 0x6e, 0x42, 0x14, 0x0a, 0x12, 0x5f, 0x62, 0x61, 0x72, 0x6f, 0x6d,
|
||||
0x5f, 0x61, 0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x5f, 0x69, 0x6e, 0x42, 0x0e, 0x0a, 0x0c,
|
||||
0x5f, 0x64, 0x65, 0x77, 0x5f, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x5f, 0x66, 0x42, 0x0f, 0x0a, 0x0d,
|
||||
0x5f, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x63, 0x68, 0x69, 0x6c, 0x6c, 0x5f, 0x66, 0x22, 0x7d, 0x0a,
|
||||
0x12, 0x54, 0x65, 0x6d, 0x70, 0x48, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x53, 0x65, 0x6e,
|
||||
0x73, 0x6f, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x06, 0x74, 0x65, 0x6d, 0x70, 0x5f,
|
||||
0x66, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x48, 0x00, 0x52, 0x05, 0x74, 0x65, 0x6d, 0x70, 0x46,
|
||||
0x88, 0x01, 0x01, 0x12, 0x1f, 0x0a, 0x08, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x18,
|
||||
0x03, 0x20, 0x01, 0x28, 0x05, 0x48, 0x01, 0x52, 0x08, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74,
|
||||
0x79, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x66, 0x42,
|
||||
0x0b, 0x0a, 0x09, 0x5f, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x22, 0x55, 0x0a, 0x0d,
|
||||
0x42, 0x61, 0x74, 0x74, 0x65, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a,
|
||||
0x09, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x09, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x12, 0x1b, 0x0a, 0x06, 0x73,
|
||||
0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x48, 0x00, 0x52, 0x06, 0x73,
|
||||
0x74, 0x61, 0x74, 0x75, 0x73, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x73, 0x74, 0x61,
|
||||
0x74, 0x75, 0x73, 0x42, 0x54, 0x5a, 0x52, 0x67, 0x69, 0x74, 0x65, 0x61, 0x2e, 0x6c, 0x69, 0x62,
|
||||
0x72, 0x65, 0x74, 0x65, 0x63, 0x68, 0x63, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67,
|
||||
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x6d, 0x63, 0x67, 0x75, 0x69, 0x72, 0x65, 0x2f, 0x61, 0x6d,
|
||||
0x62, 0x69, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x2d, 0x65, 0x78, 0x70, 0x6f,
|
||||
0x72, 0x74, 0x65, 0x72, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x61, 0x6c, 0x70, 0x68, 0x61,
|
||||
0x31, 0x2f, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x33,
|
||||
})
|
||||
|
||||
var (
|
||||
@ -726,11 +739,12 @@ var file_weather_weather_proto_depIdxs = []int32{
|
||||
3, // 2: ambient.weather.GetWeatherResponse.weather_updates:type_name -> ambient.weather.WeatherUpdate
|
||||
5, // 3: ambient.weather.WeatherUpdate.batteries:type_name -> ambient.weather.BatteryStatus
|
||||
4, // 4: ambient.weather.WeatherUpdate.temp_humidity_sensors:type_name -> ambient.weather.TempHumiditySensor
|
||||
5, // [5:5] is the sub-list for method output_type
|
||||
5, // [5:5] is the sub-list for method input_type
|
||||
5, // [5:5] is the sub-list for extension type_name
|
||||
5, // [5:5] is the sub-list for extension extendee
|
||||
0, // [0:5] is the sub-list for field type_name
|
||||
6, // 5: ambient.weather.WeatherUpdate.update_timestamp:type_name -> google.protobuf.Timestamp
|
||||
6, // [6:6] is the sub-list for method output_type
|
||||
6, // [6:6] is the sub-list for method input_type
|
||||
6, // [6:6] is the sub-list for extension type_name
|
||||
6, // [6:6] is the sub-list for extension extendee
|
||||
0, // [0:6] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_weather_weather_proto_init() }
|
||||
|
61
app.go
Normal file
61
app.go
Normal file
@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
|
||||
grpcopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
||||
httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
|
||||
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/ambienthttp"
|
||||
weathergrpc "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/grpc"
|
||||
)
|
||||
|
||||
func prepareApp(ctx context.Context, aw *ambient.AmbientWeather) *app.App {
|
||||
// Load ambient routes into app
|
||||
awApp := &app.App{
|
||||
AppContext: ctx,
|
||||
|
||||
// HTTP Endpoints for Ambient Weather Stations
|
||||
HTTP: &httpopts.AppHTTP{
|
||||
Funcs: []httpopts.HTTPFunc{
|
||||
{
|
||||
Path: "/weatherstation/updateweatherstation.php",
|
||||
HandlerFunc: aw.GetWundergroundHandlerFunc(ctx),
|
||||
},
|
||||
{
|
||||
Path: "/data/report",
|
||||
HandlerFunc: aw.GetAWNHandlerFunc(ctx),
|
||||
},
|
||||
},
|
||||
|
||||
// HTTP Listener that fixes broken requests generated by
|
||||
// some versions of awn firmware
|
||||
CustomListener: ambienthttp.NewAWNMutatingListener(ctx,
|
||||
aw.Config.HTTP.Listen), // Necessary to fix certain bad AWN firmware
|
||||
|
||||
// Health check funcs
|
||||
HealthChecks: []httpopts.HealthCheckFunc{
|
||||
// TODO: Implement
|
||||
func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// GRPC Service for retrieving weather
|
||||
GRPC: &grpcopts.AppGRPC{
|
||||
Services: []*grpcopts.GRPCService{
|
||||
{
|
||||
Name: "Weather Service",
|
||||
Type: &weatherpb.AmbientLocalWeatherService_ServiceDesc,
|
||||
Service: weathergrpc.NewGRPCWeather(ctx, aw.GetRecorder()),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return awApp
|
||||
}
|
226
contrib/schema.json
Normal file
226
contrib/schema.json
Normal file
@ -0,0 +1,226 @@
|
||||
{
|
||||
"definitions": {
|
||||
"ConfigGRPCConfig": {
|
||||
"properties": {
|
||||
"enableInstrumentation": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"enableReflection": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"enabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"listen": {
|
||||
"type": "string"
|
||||
},
|
||||
"logRequests": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigHTTPConfig": {
|
||||
"properties": {
|
||||
"enabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"idleTimeout": {
|
||||
"type": "string"
|
||||
},
|
||||
"listen": {
|
||||
"type": "string"
|
||||
},
|
||||
"logRequests": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"readTimeout": {
|
||||
"type": "string"
|
||||
},
|
||||
"writeTimeout": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigLogConfig": {
|
||||
"properties": {
|
||||
"enabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"format": {
|
||||
"type": "string"
|
||||
},
|
||||
"level": {
|
||||
"type": "string"
|
||||
},
|
||||
"output": {
|
||||
"type": "string"
|
||||
},
|
||||
"timeFormat": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigOTELConfig": {
|
||||
"properties": {
|
||||
"enabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"metricIntervalSecs": {
|
||||
"type": "integer"
|
||||
},
|
||||
"prometheusEnabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"prometheusPath": {
|
||||
"type": "string"
|
||||
},
|
||||
"stdoutEnabled": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigRecorderConfig": {
|
||||
"properties": {
|
||||
"keepLast": {
|
||||
"default": 120,
|
||||
"type": "integer"
|
||||
},
|
||||
"redisConfig": {
|
||||
"$ref": "#/definitions/ConfigRedisConfig"
|
||||
},
|
||||
"type": {
|
||||
"enum": [
|
||||
"memory",
|
||||
"redis",
|
||||
"noop"
|
||||
],
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigRedisConfig": {
|
||||
"properties": {
|
||||
"redisDB": {
|
||||
"default": 0,
|
||||
"type": "integer"
|
||||
},
|
||||
"redisHost": {
|
||||
"default": "127.0.0.1",
|
||||
"type": "string"
|
||||
},
|
||||
"redisPassword": {
|
||||
"type": "string"
|
||||
},
|
||||
"redisPort": {
|
||||
"default": 6379,
|
||||
"type": "integer"
|
||||
},
|
||||
"redisTLS": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"redisTLSInsecure": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"redisUser": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"ConfigWeatherStation": {
|
||||
"properties": {
|
||||
"awnPassKey": {
|
||||
"type": "string"
|
||||
},
|
||||
"dropMetrics": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"equipment": {
|
||||
"type": "string"
|
||||
},
|
||||
"keepMetrics": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"proxyToAWN": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"proxyToWunderground": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"sensorMappings": {
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"wundergroundID": {
|
||||
"type": "string"
|
||||
},
|
||||
"wundergroundPassword": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"environment": {
|
||||
"type": "string"
|
||||
},
|
||||
"grpc": {
|
||||
"$ref": "#/definitions/ConfigGRPCConfig"
|
||||
},
|
||||
"http": {
|
||||
"$ref": "#/definitions/ConfigHTTPConfig"
|
||||
},
|
||||
"logging": {
|
||||
"$ref": "#/definitions/ConfigLogConfig"
|
||||
},
|
||||
"metricPrefix": {
|
||||
"default": "weather",
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"otel": {
|
||||
"$ref": "#/definitions/ConfigOTELConfig"
|
||||
},
|
||||
"recorderConfig": {
|
||||
"$ref": "#/definitions/ConfigRecorderConfig"
|
||||
},
|
||||
"updatesToKeep": {
|
||||
"default": 1,
|
||||
"type": [
|
||||
"null",
|
||||
"integer"
|
||||
]
|
||||
},
|
||||
"version": {
|
||||
"type": "string"
|
||||
},
|
||||
"weatherStations": {
|
||||
"items": {
|
||||
"$ref": "#/definitions/ConfigWeatherStation"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
}
|
89
contrib/wu_sample.json
Normal file
89
contrib/wu_sample.json
Normal file
@ -0,0 +1,89 @@
|
||||
{
|
||||
"PASSKEY": [
|
||||
"DE:AD:BE:EF:BE:EF"
|
||||
],
|
||||
"baromabsin": [
|
||||
"29.025"
|
||||
],
|
||||
"baromrelin": [
|
||||
"30.005"
|
||||
],
|
||||
"batt1": [
|
||||
"1"
|
||||
],
|
||||
"battin": [
|
||||
"1"
|
||||
],
|
||||
"battout": [
|
||||
"1"
|
||||
],
|
||||
"battrain": [
|
||||
"1"
|
||||
],
|
||||
"dailyrainin": [
|
||||
"0.000"
|
||||
],
|
||||
"dateutc": [
|
||||
"2025-03-23 14:07:59"
|
||||
],
|
||||
"eventrainin": [
|
||||
"0.000"
|
||||
],
|
||||
"hourlyrainin": [
|
||||
"0.000"
|
||||
],
|
||||
"humidity": [
|
||||
"55"
|
||||
],
|
||||
"humidity1": [
|
||||
"53"
|
||||
],
|
||||
"humidityin": [
|
||||
"36"
|
||||
],
|
||||
"maxdailygust": [
|
||||
"20.36"
|
||||
],
|
||||
"monthlyrainin": [
|
||||
"1.969"
|
||||
],
|
||||
"solarradiation": [
|
||||
"142.15"
|
||||
],
|
||||
"stationtype": [
|
||||
"WeatherHub_V1.0.2"
|
||||
],
|
||||
"temp1f": [
|
||||
"-2.74"
|
||||
],
|
||||
"tempf": [
|
||||
"33.98"
|
||||
],
|
||||
"tempinf": [
|
||||
"70.52"
|
||||
],
|
||||
"totalrainin": [
|
||||
"2.421"
|
||||
],
|
||||
"uv": [
|
||||
"1"
|
||||
],
|
||||
"weeklyrainin": [
|
||||
"0.000"
|
||||
],
|
||||
"winddir": [
|
||||
"117"
|
||||
],
|
||||
"winddir_avg10m": [
|
||||
"127"
|
||||
],
|
||||
"windgustmph": [
|
||||
"17.22"
|
||||
],
|
||||
"windspeedmph": [
|
||||
"10.29"
|
||||
],
|
||||
"yearlyrainin": [
|
||||
"2.421"
|
||||
]
|
||||
}
|
9
contrib/wu_test_sample.sh
Executable file
9
contrib/wu_test_sample.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!env sh
|
||||
SCRIPT_DIR=$( dirname $0 )
|
||||
JSON_FILE="${SCRIPT_DIR}/wu_sample.json"
|
||||
BASE_URL="http://127.0.0.1:8080/data/report"
|
||||
|
||||
# Convert JSON to query parameters
|
||||
QUERY_STRING=$(jq -r 'to_entries | map("\(.key)=\(.value[0] | @uri)") | join("&")' "$JSON_FILE")
|
||||
|
||||
curl -G --data "$QUERY_STRING" "$BASE_URL"
|
@ -3,7 +3,8 @@ services:
|
||||
ambient-local-exporter:
|
||||
build: .
|
||||
ports:
|
||||
- 8080:8080
|
||||
- 8080:8080 # HTTP
|
||||
- 8081:8081 # GRPC
|
||||
volumes:
|
||||
- ./config.yaml:/app/config.yaml
|
||||
command:
|
||||
|
18
go.mod
18
go.mod
@ -3,17 +3,18 @@ module gitea.libretechconsulting.com/rmcguire/ambient-local-exporter
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.6.3
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.7.0
|
||||
github.com/go-resty/resty/v2 v2.16.5
|
||||
github.com/gorilla/schema v1.4.1
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/redis/go-redis/v9 v9.7.3
|
||||
github.com/rs/zerolog v1.34.0
|
||||
go.opentelemetry.io/otel v1.35.0
|
||||
go.opentelemetry.io/otel/metric v1.35.0
|
||||
go.opentelemetry.io/otel/trace v1.35.0
|
||||
golang.org/x/sys v0.31.0
|
||||
google.golang.org/grpc v1.71.0
|
||||
google.golang.org/protobuf v1.36.5
|
||||
k8s.io/utils v0.0.0-20241210054802-24370beab758
|
||||
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
|
||||
)
|
||||
|
||||
require (
|
||||
@ -21,6 +22,7 @@ require (
|
||||
github.com/caarlos0/env/v11 v11.3.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
@ -33,8 +35,10 @@ require (
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/prometheus/client_golang v1.21.1 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.62.0 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
github.com/prometheus/common v0.63.0 // indirect
|
||||
github.com/prometheus/procfs v0.16.0 // indirect
|
||||
github.com/swaggest/jsonschema-go v0.3.73 // indirect
|
||||
github.com/swaggest/refl v1.3.1 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
|
||||
@ -49,7 +53,7 @@ require (
|
||||
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
|
||||
golang.org/x/net v0.37.0 // indirect
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
23
go.sum
23
go.sum
@ -4,6 +4,8 @@ gitea.libretechconsulting.com/rmcguire/go-app v0.6.2 h1:vpEdZu7WI8qIil5NLf6OUF/T
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.6.2/go.mod h1:S3/vdMEiRWWIdD0Fr+tjJc627VzxNzO4Ia2HgTBXe+g=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.6.3 h1:dXYHJxK/1vmWBj1wqbqEUncFt3O92agy9gNWoa9NpA0=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.6.3/go.mod h1:S3/vdMEiRWWIdD0Fr+tjJc627VzxNzO4Ia2HgTBXe+g=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.7.0 h1:yhRRwV/dxN4Bey1Qv8/rHHV5QvALrZuDJFI+zFPc7sU=
|
||||
gitea.libretechconsulting.com/rmcguire/go-app v0.7.0/go.mod h1:EM3Z9QcRD+b7UlKGA9y37ppfUBC0Reyf5sYEC6vFZcY=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
|
||||
@ -15,6 +17,8 @@ github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
@ -63,15 +67,28 @@ github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
||||
github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA98k=
|
||||
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/prometheus/procfs v0.16.0 h1:xh6oHhKwnOJKMYiYBDWmkHqQPyiY40sny36Cmx2bbsM=
|
||||
github.com/prometheus/procfs v0.16.0/go.mod h1:8veyXUu3nGP7oaCxhX6yeaM5u4stL2FeMXnCqhDthZg=
|
||||
github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
|
||||
github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/swaggest/jsonschema-go v0.3.73 h1:gU1pBzF3pkZ1GDD3dRMdQoCjrA0sldJ+QcM7aSSPgvc=
|
||||
github.com/swaggest/jsonschema-go v0.3.73/go.mod h1:qp+Ym2DIXHlHzch3HKz50gPf2wJhKOrAB/VYqLS2oJU=
|
||||
github.com/swaggest/refl v1.3.1 h1:XGplEkYftR7p9cz1lsiwXMM2yzmOymTE9vneVVpaOh4=
|
||||
github.com/swaggest/refl v1.3.1/go.mod h1:4uUVFVfPJ0NSX9FPwMPspeHos9wPFlCMGoPRllUbpvA=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 h1:x7wzEgXfnzJcHDwStJT+mxOz4etr2EcexjqhBvmoakw=
|
||||
@ -117,8 +134,12 @@ golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
|
||||
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb h1:p31xT4yrYrSM/G4Sn2+TNUkVhFCbG9y8itM2S6Th950=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:jbe3Bkdp+Dh2IrslsFCklNhweNTBgSYanP1UXhJDhKg=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 h1:IFnXJq3UPB3oBREOodn1v1aGQeZYQclEmvWRMN0PSsY=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:c8q6Z6OCqnfVIqUFJkCzKcrj8eCvUrz+K4KRzSTuANg=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb h1:TLPQVbx1GJ8VKZxz52VAxl1EBgKXXbTiU9Fc5fZeLn4=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 h1:iK2jbkWL86DXjEx0qiHcRE9dE4/Ahua5k6V8OWFb//c=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
|
||||
google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
|
||||
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
@ -130,3 +151,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJJI8IUa1AmH/qa0=
|
||||
k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e h1:KqK5c/ghOm8xkHYhlodbp6i6+r+ChV2vuAuVRdFbLro=
|
||||
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
|
@ -15,13 +15,13 @@ type: application
|
||||
# This is the chart version. This version number should be incremented each time you make changes
|
||||
# to the chart and its templates, including the app version.
|
||||
# Versions are expected to follow Semantic Versioning (https://semver.org/)
|
||||
version: 0.1.0
|
||||
version: 0.1.4
|
||||
|
||||
# This is the version number of the application being deployed. This version number should be
|
||||
# incremented each time you make changes to the application. Versions are not expected to
|
||||
# follow Semantic Versioning. They should reflect the version the application is using.
|
||||
# It is recommended to use it with quotes.
|
||||
appVersion: "v0.10.1"
|
||||
appVersion: "v0.11.2"
|
||||
|
||||
dependencies:
|
||||
- name: hull
|
||||
|
86
main.go
86
main.go
@ -2,28 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
|
||||
grpcopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
||||
httpopts "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
"golang.org/x/sys/unix"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
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/ambienthttp"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
|
||||
weathergrpc "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/grpc"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/state"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMetricPrefix = "weather"
|
||||
defaultUpdatesToKeep = 120
|
||||
defaultMetricPrefix = "weather"
|
||||
)
|
||||
|
||||
var schema bool
|
||||
|
||||
func main() {
|
||||
ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt, unix.SIGTERM)
|
||||
defer cncl()
|
||||
@ -39,6 +35,12 @@ func main() {
|
||||
// and set up logging, tracing, etc..
|
||||
aw := ambient.New(ctx, awConfig).Init()
|
||||
|
||||
// Just show schema if that's all we were asked to do
|
||||
if schema {
|
||||
printSchema(awConfig)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Load http and grpc routes, prepare the app
|
||||
awApp := prepareApp(ctx, aw)
|
||||
|
||||
@ -47,58 +49,16 @@ func main() {
|
||||
<-awApp.Done()
|
||||
}
|
||||
|
||||
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
|
||||
awApp := &app.App{
|
||||
AppContext: ctx,
|
||||
|
||||
// HTTP Endpoints for Ambient Weather Stations
|
||||
HTTP: &httpopts.AppHTTP{
|
||||
Funcs: []httpopts.HTTPFunc{
|
||||
{
|
||||
Path: "/weatherstation/updateweatherstation.php",
|
||||
HandlerFunc: aw.GetWundergroundHandlerFunc(ctx),
|
||||
},
|
||||
{
|
||||
Path: "/data/report",
|
||||
HandlerFunc: aw.GetAWNHandlerFunc(ctx),
|
||||
},
|
||||
},
|
||||
|
||||
// HTTP Listener that fixes broken requests generated by
|
||||
// some versions of awn firmware
|
||||
CustomListener: ambienthttp.NewAWNMutatingListener(ctx,
|
||||
aw.Config.HTTP.Listen), // Necessary to fix certain bad AWN firmware
|
||||
|
||||
// Health check funcs
|
||||
HealthChecks: []httpopts.HealthCheckFunc{
|
||||
// TODO: Implement
|
||||
func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// GRPC Service for retrieving weather
|
||||
GRPC: &grpcopts.AppGRPC{
|
||||
Services: []*grpcopts.GRPCService{
|
||||
{
|
||||
Name: "Weather Service",
|
||||
Type: &weatherpb.AmbientLocalWeatherService_ServiceDesc,
|
||||
Service: weathergrpc.NewGRPCWeather(ctx, state.NewWeatherState(
|
||||
&state.Opts{
|
||||
Ctx: ctx,
|
||||
KeepLast: *aw.Config.UpdatesToKeep,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return awApp
|
||||
// flag.Parse will be called by go-app
|
||||
func init() {
|
||||
flag.BoolVar(&schema, "schema", false, "generate json schema and exit")
|
||||
}
|
||||
|
||||
func printSchema(config *config.AmbientLocalExporterConfig) {
|
||||
bytes, err := app.CustomSchema(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
|
@ -20,35 +20,85 @@ import (
|
||||
"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/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders/memory"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders/noop"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders/redis"
|
||||
)
|
||||
|
||||
const defUpdatesToKeep = 120
|
||||
|
||||
type AmbientWeather struct {
|
||||
// These providers implement support for the update sent
|
||||
// when either "AmbientWeather" or "Wunderground" are selected
|
||||
// in the "Custom" section of the AWNet app, or the web UI
|
||||
// of an Ambient WeatherHub
|
||||
Config *config.AmbientLocalExporterConfig
|
||||
awnProvider provider.AmbientProvider
|
||||
wuProvider provider.AmbientProvider
|
||||
appCtx context.Context
|
||||
metrics *weather.WeatherMetrics
|
||||
l *zerolog.Logger
|
||||
Config *config.AmbientLocalExporterConfig
|
||||
awnProvider provider.AmbientProvider
|
||||
wuProvider provider.AmbientProvider
|
||||
weatherRecorder *recorder.WeatherRecorder
|
||||
appCtx context.Context
|
||||
metrics *weather.WeatherMetrics
|
||||
l *zerolog.Logger
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather {
|
||||
return &AmbientWeather{
|
||||
Config: awConfig,
|
||||
appCtx: appCtx,
|
||||
Config: awConfig,
|
||||
appCtx: appCtx,
|
||||
RWMutex: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize with defaults, set logger from context
|
||||
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.wuProvider = &wunderground.WUProvider{}
|
||||
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))
|
||||
|
||||
// Choose weather recorder for grpc / api requests,
|
||||
// default is memory recorder
|
||||
var r recorders.Recorder
|
||||
if aw.Config == nil || aw.Config.RecorderConfig == nil {
|
||||
r = &memory.MemoryRecorder{}
|
||||
} else {
|
||||
switch aw.Config.RecorderConfig.Type {
|
||||
case config.TypeMemory:
|
||||
r = &memory.MemoryRecorder{}
|
||||
case config.TypeRedis:
|
||||
r = &redis.RedisRecorder{}
|
||||
case config.TypeNoop:
|
||||
r = &noop.NoopRecorder{}
|
||||
}
|
||||
}
|
||||
|
||||
aw.weatherRecorder = recorder.MustNewWeatherRecorder(&recorder.Opts{
|
||||
AppConfig: aw.Config,
|
||||
Ctx: aw.appCtx,
|
||||
KeepLast: updatesToKeep,
|
||||
Recorder: r,
|
||||
})
|
||||
|
||||
aw.l.Trace().Any("awConfig", aw.Config).Send()
|
||||
span.SetStatus(codes.Ok, "")
|
||||
return aw
|
||||
}
|
||||
|
||||
@ -78,6 +128,9 @@ func (aw *AmbientWeather) handleProviderRequest(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
aw.Lock()
|
||||
defer aw.Unlock()
|
||||
|
||||
l := zerolog.Ctx(aw.appCtx)
|
||||
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
|
||||
|
||||
@ -110,6 +163,9 @@ func (aw *AmbientWeather) handleProviderRequest(
|
||||
updateSpan.SetAttributes(attribute.String("stationName", update.StationConfig.Name))
|
||||
}
|
||||
|
||||
// Record state
|
||||
aw.weatherRecorder.Set(ctx, update)
|
||||
|
||||
// Update metrics
|
||||
aw.metricsUpdate(ctx, p, update)
|
||||
|
||||
@ -242,3 +298,10 @@ func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (aw *AmbientWeather) GetRecorder() *recorder.WeatherRecorder {
|
||||
aw.RLock()
|
||||
defer aw.RUnlock()
|
||||
|
||||
return aw.weatherRecorder
|
||||
}
|
||||
|
@ -6,24 +6,25 @@ import (
|
||||
|
||||
// This configuration includes all config from go-app/config.AppConfig
|
||||
type AmbientLocalExporterConfig struct {
|
||||
MetricPrefix string `yaml:"metricPrefix" default:"weather" env:"AMBIENT_METRIC_PREFIX"`
|
||||
UpdatesToKeep *int `yaml:"updatesToKeep" default:"1" env:"AMBIENT_UPDATES_TO_KEEP"`
|
||||
WeatherStations []WeatherStation `yaml:"weatherStations" env:"weatherStations"` // No env, too complex, not worth the time
|
||||
MetricPrefix string `yaml:"metricPrefix" default:"weather" env:"AMBIENT_METRIC_PREFIX" json:"metricPrefix,omitempty"`
|
||||
UpdatesToKeep *int `yaml:"updatesToKeep" default:"1" env:"AMBIENT_UPDATES_TO_KEEP" json:"updatesToKeep,omitempty"`
|
||||
WeatherStations []WeatherStation `yaml:"weatherStations" json:"weatherStations,omitempty"` // No env, too complex, not worth the time
|
||||
RecorderConfig *RecorderConfig `yaml:"recorderConfig" json:"recorderConfig,omitempty"`
|
||||
*config.AppConfig // Extends app config
|
||||
}
|
||||
|
||||
type WeatherStation struct {
|
||||
Name string `yaml:"name"` // Human Friendly Name (e.g. Back Yard Weather)
|
||||
Equipment string `yaml:"equipment"` // Equipment Type (e.g. WS-5000)
|
||||
Name string `yaml:"name" json:"name,omitempty"` // Human Friendly Name (e.g. Back Yard Weather)
|
||||
Equipment string `yaml:"equipment" json:"equipment,omitempty"` // Equipment Type (e.g. WS-5000)
|
||||
|
||||
// Required if proxying to awn/wu is enabled
|
||||
WundergroundID string `yaml:"wundergroundID"`
|
||||
WundergroundPassword string `yaml:"wundergroundPassword"`
|
||||
AWNPassKey string `yaml:"awnPassKey"`
|
||||
WundergroundID string `yaml:"wundergroundID" json:"wundergroundID,omitempty"`
|
||||
WundergroundPassword string `yaml:"wundergroundPassword" json:"wundergroundPassword,omitempty"`
|
||||
AWNPassKey string `yaml:"awnPassKey" json:"awnPassKey,omitempty"`
|
||||
|
||||
// Proxy updates to AWN or Wunderground
|
||||
ProxyToAWN bool `yaml:"proxyToAWN"`
|
||||
ProxyToWunderground bool `yaml:"proxyToWunderground"`
|
||||
ProxyToAWN bool `yaml:"proxyToAWN" json:"proxyToAWN,omitempty"`
|
||||
ProxyToWunderground bool `yaml:"proxyToWunderground" json:"proxyToWunderground,omitempty"`
|
||||
|
||||
// Unreliable / unwanted metrics by name of WeatherUpdate Field
|
||||
// will be excluded if present in discardMetrics
|
||||
@ -32,8 +33,8 @@ type WeatherStation struct {
|
||||
// ignoring discardMetrics.
|
||||
//
|
||||
// Check weather.WeatherUpdateField for options
|
||||
KeepMetrics []string `yaml:"keepMetrics"`
|
||||
DropMetrics []string `yaml:"dropMetrics"`
|
||||
KeepMetrics []string `yaml:"keepMetrics" json:"keepMetrics,omitempty"`
|
||||
DropMetrics []string `yaml:"dropMetrics" json:"dropMetrics,omitempty"`
|
||||
|
||||
// Relabels battery and sensor names
|
||||
// Temp+Humidity Sensors:
|
||||
@ -43,5 +44,5 @@ type WeatherStation struct {
|
||||
// - OutdoorSensor
|
||||
// - RainSensor
|
||||
// - CO2Sensor
|
||||
SensorMappings map[string]string `yaml:"sensorMappings"`
|
||||
SensorMappings map[string]string `yaml:"sensorMappings" json:"sensorMappings,omitempty"`
|
||||
}
|
||||
|
25
pkg/ambient/config/config_recorder.go
Normal file
25
pkg/ambient/config/config_recorder.go
Normal file
@ -0,0 +1,25 @@
|
||||
package config
|
||||
|
||||
type RecorderType string
|
||||
|
||||
const (
|
||||
TypeMemory RecorderType = "memory" // Stores weather updates in memory
|
||||
TypeRedis RecorderType = "redis" // Required for replicas > 1
|
||||
TypeNoop RecorderType = "noop" // No-op implementation
|
||||
)
|
||||
|
||||
type RecorderConfig struct {
|
||||
Type RecorderType `yaml:"type" env:"RECORDER_TYPE" json:"type,omitempty" enum:"memory,redis,noop"`
|
||||
KeepLast int `yaml:"keepLast" env:"RECORDER_KEEP_LAST" json:"keepLast,omitempty" default:"120"`
|
||||
RedisConfig *RedisConfig `yaml:"redisConfig,omitempty" json:"redisConfig,omitempty"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
RedisHost string `yaml:"redisHost" env:"REDIS_HOST" default:"127.0.0.1" json:"redisHost,omitempty"`
|
||||
RedisPort int `yaml:"redisPort" env:"REDIS_PORT" default:"6379" json:"redisPort,omitempty"`
|
||||
RedisUser string `yaml:"redisUser" env:"REDIS_USER" json:"redisUser,omitempty"`
|
||||
RedisPassword string `yaml:"redisPassword" env:"REDIS_PASSWORD" json:"redisPassword,omitempty"`
|
||||
RedisDB int `yaml:"redisDB" env:"REDIS_DB" default:"0" json:"redisDB,omitempty"`
|
||||
RedisTLS bool `yaml:"redisTLS" env:"REDIS_TLS" default:"false" json:"redisTLS,omitempty"`
|
||||
RedisTLSInsecure bool `yaml:"redisTLSInsecure" env:"REDIS_TLS_INSECURE" default:"false" json:"redisTLSInsecure,omitempty"`
|
||||
}
|
104
pkg/util/util.go
Normal file
104
pkg/util/util.go
Normal file
@ -0,0 +1,104 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
const defaultLimit = -1
|
||||
|
||||
func DerefStr(s *string) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func Int32ptr(i *int) *int32 {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
return ptr.To(int32(*i))
|
||||
}
|
||||
|
||||
// Generates a hash that will be consistent
|
||||
// across all running replicas
|
||||
func GetAppHash(conf *config.AppConfig) string {
|
||||
hashName := fmt.Sprintf("%s-%s-%s",
|
||||
conf.Name,
|
||||
conf.Environment,
|
||||
conf.Version,
|
||||
)
|
||||
|
||||
hash := md5.Sum([]byte(hashName))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
// Get a limit from req, applying a default if unset or not sane
|
||||
func GetLimitFromReq(req *pb.GetWeatherRequest) int {
|
||||
if req == nil || req.Limit == nil {
|
||||
return defaultLimit
|
||||
}
|
||||
|
||||
if req.GetLimit() == 0 {
|
||||
return defaultLimit
|
||||
}
|
||||
|
||||
return int(req.GetLimit())
|
||||
}
|
||||
|
||||
// Simple helper to trim a list of updates
|
||||
func LimitUpdates(updates []*weather.WeatherUpdate, limit int) []*weather.WeatherUpdate {
|
||||
if limit < 0 {
|
||||
return updates // No limit
|
||||
}
|
||||
|
||||
if len(updates) > limit {
|
||||
return updates[len(updates)-limit:] // Trim to limit
|
||||
}
|
||||
|
||||
return updates // Within limit
|
||||
}
|
||||
|
||||
func ApplyOptsToUpdates(updates []*weather.WeatherUpdate, limit int, opts *pb.GetWeatherOpts) []*weather.WeatherUpdate {
|
||||
if opts == nil {
|
||||
return updates
|
||||
} else if opts.StationName == nil && opts.StationType == nil {
|
||||
return updates
|
||||
}
|
||||
|
||||
filtered := make([]*weather.WeatherUpdate, 0, limit)
|
||||
|
||||
for i := len(updates) - 1; i >= 0; i-- {
|
||||
update := updates[i]
|
||||
match := true
|
||||
|
||||
if opts.GetStationName() != "" {
|
||||
if update.GetStationName() != opts.GetStationName() {
|
||||
match = false
|
||||
}
|
||||
}
|
||||
if opts.GetStationType() != "" {
|
||||
if DerefStr(update.StationType) != opts.GetStationType() {
|
||||
match = false
|
||||
}
|
||||
}
|
||||
|
||||
if match {
|
||||
filtered = append(filtered, update)
|
||||
if limit > 0 && len(filtered) >= limit {
|
||||
return slices.Clip(filtered)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return slices.Clip(filtered)
|
||||
}
|
@ -1,12 +1,17 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"k8s.io/utils/ptr"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
// Converts a slice of weather.WeatherUpdate to proto structs
|
||||
func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
|
||||
updates := make([]*pb.WeatherUpdate, len(u))
|
||||
for i, update := range u {
|
||||
@ -15,21 +20,30 @@ func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
|
||||
return updates
|
||||
}
|
||||
|
||||
// Maps a weather.WeatherUpdate to the proto version.
|
||||
//
|
||||
// This should be auto-generated code, but as the proto should be
|
||||
// stable, it is probably not worth the effort.
|
||||
func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
|
||||
if u.DateUTC == nil {
|
||||
now := time.Now()
|
||||
u.DateUTC = &now
|
||||
}
|
||||
|
||||
return &pb.WeatherUpdate{
|
||||
StationName: u.StationConfig.Name,
|
||||
StationType: derefStr(u.StationType),
|
||||
StationId: derefStr(u.StationID),
|
||||
StationType: util.DerefStr(u.StationType),
|
||||
StationId: util.DerefStr(u.StationID),
|
||||
TempOutdoorF: u.TempOutdoorF,
|
||||
TempIndoorF: u.TempIndoorF,
|
||||
HumidityOutdoor: int32ptr(u.HumidityOudoor),
|
||||
HumidityIndoor: int32ptr(u.HumidityIndoor),
|
||||
HumidityOutdoor: util.Int32ptr(u.HumidityOudoor),
|
||||
HumidityIndoor: util.Int32ptr(u.HumidityIndoor),
|
||||
WindSpeedMph: u.WindSpeedMPH,
|
||||
WindGustMph: u.WindGustMPH,
|
||||
MaxDailyGust: u.MaxDailyGust,
|
||||
WindDir: int32ptr(u.WindDir),
|
||||
WindDirAvg_10M: int32ptr(u.WindDirAvg10m),
|
||||
Uv: int32ptr(u.UV),
|
||||
WindDir: util.Int32ptr(u.WindDir),
|
||||
WindDirAvg_10M: util.Int32ptr(u.WindDirAvg10m),
|
||||
Uv: util.Int32ptr(u.UV),
|
||||
SolarRadiation: u.SolarRadiation,
|
||||
HourlyRainIn: u.HourlyRainIn,
|
||||
EventRainIn: u.EventRainIn,
|
||||
@ -44,42 +58,33 @@ func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
|
||||
DewPointF: u.DewPointF,
|
||||
WindChillF: u.WindChillF,
|
||||
TempHumiditySensors: thSensorsToPbSensors(u.TempHumiditySensors),
|
||||
UpdateTimestamp: timestamppb.New(*u.DateUTC),
|
||||
}
|
||||
}
|
||||
|
||||
func batteriesToPbBatteries(batteries []weather.BatteryStatus) []*pb.BatteryStatus {
|
||||
pbBatteries := make([]*pb.BatteryStatus, len(batteries))
|
||||
for i, b := range batteries {
|
||||
pbBatteries[i] = &pb.BatteryStatus{
|
||||
Component: b.Component,
|
||||
Status: int32ptr(b.Status),
|
||||
pbBatteries := make([]*pb.BatteryStatus, 0, len(batteries))
|
||||
for _, b := range batteries {
|
||||
if b.Status != nil {
|
||||
pbBatteries = append(pbBatteries, &pb.BatteryStatus{
|
||||
Component: b.Component,
|
||||
Status: util.Int32ptr(b.Status),
|
||||
})
|
||||
}
|
||||
}
|
||||
return pbBatteries
|
||||
return slices.Clip(pbBatteries)
|
||||
}
|
||||
|
||||
func thSensorsToPbSensors(sensors []*weather.TempHumiditySensor) []*pb.TempHumiditySensor {
|
||||
pbSensors := make([]*pb.TempHumiditySensor, len(sensors))
|
||||
for i, s := range sensors {
|
||||
pbSensors[i] = &pb.TempHumiditySensor{
|
||||
Name: s.Name,
|
||||
TempF: s.TempF,
|
||||
Humidity: int32ptr(s.Humidity),
|
||||
pbSensors := make([]*pb.TempHumiditySensor, 0, len(sensors))
|
||||
for _, s := range sensors {
|
||||
if s.TempF != nil || s.Humidity != nil {
|
||||
pbSensors = append(pbSensors, &pb.TempHumiditySensor{
|
||||
Name: s.Name,
|
||||
TempF: s.TempF,
|
||||
Humidity: util.Int32ptr(s.Humidity),
|
||||
})
|
||||
}
|
||||
}
|
||||
return pbSensors
|
||||
}
|
||||
|
||||
func derefStr(s *string) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func int32ptr(i *int) *int32 {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
return ptr.To(int32(*i))
|
||||
return slices.Clip(pbSensors)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
@ -12,8 +13,10 @@ import (
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
var now = time.Now()
|
||||
|
||||
var mockUpdate = &weather.WeatherUpdate{
|
||||
DateUTC: &time.Time{},
|
||||
DateUTC: &now,
|
||||
StationConfig: &config.WeatherStation{
|
||||
Name: "50W",
|
||||
Equipment: "WS-5000",
|
||||
@ -100,6 +103,7 @@ func TestUpdateToPbUpdate(t *testing.T) {
|
||||
Humidity: nil,
|
||||
},
|
||||
},
|
||||
UpdateTimestamp: timestamppb.New(now),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -13,21 +13,21 @@ import (
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
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 {
|
||||
ctx context.Context
|
||||
state *state.WeatherState
|
||||
tracer trace.Tracer
|
||||
ctx context.Context
|
||||
recorder *recorder.WeatherRecorder
|
||||
tracer trace.Tracer
|
||||
*pb.UnimplementedAmbientLocalWeatherServiceServer
|
||||
}
|
||||
|
||||
func NewGRPCWeather(ctx context.Context, state *state.WeatherState) *GRPCWeather {
|
||||
func NewGRPCWeather(ctx context.Context, recorder *recorder.WeatherRecorder) *GRPCWeather {
|
||||
return &GRPCWeather{
|
||||
ctx: ctx,
|
||||
state: state,
|
||||
tracer: otel.GetTracer(ctx, "grpcWeather"),
|
||||
ctx: ctx,
|
||||
recorder: recorder,
|
||||
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))
|
||||
|
||||
updates, err := w.state.Get(ctx, limit)
|
||||
updates, err := w.recorder.Get(ctx, req)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(otelcodes.Error, err.Error())
|
||||
|
57
pkg/weather/recorder/recorder.go
Normal file
57
pkg/weather/recorder/recorder.go
Normal file
@ -0,0 +1,57 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders"
|
||||
)
|
||||
|
||||
type WeatherRecorder struct {
|
||||
recorder recorders.Recorder
|
||||
ctx context.Context
|
||||
tracer trace.Tracer
|
||||
meter metric.Meter
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
type Opts struct {
|
||||
AppConfig *config.AmbientLocalExporterConfig
|
||||
Ctx context.Context
|
||||
Recorder recorders.Recorder // If nil, will use memory recorder
|
||||
KeepLast int
|
||||
}
|
||||
|
||||
func MustNewWeatherRecorder(opts *Opts) *WeatherRecorder {
|
||||
if opts.KeepLast < 1 {
|
||||
opts.KeepLast = 1
|
||||
}
|
||||
|
||||
if opts.Recorder == nil {
|
||||
panic("no recorder provided")
|
||||
}
|
||||
|
||||
opts.Recorder.Init(opts.Ctx, &recorders.RecorderOpts{
|
||||
AppConfig: opts.AppConfig,
|
||||
RetainLast: opts.KeepLast,
|
||||
BaseCtx: opts.Ctx,
|
||||
})
|
||||
|
||||
zerolog.Ctx(opts.Ctx).Info().Str("recorderType", opts.Recorder.Name()).
|
||||
Msg("weather update recorder ready")
|
||||
|
||||
return &WeatherRecorder{
|
||||
ctx: opts.Ctx,
|
||||
recorder: opts.Recorder,
|
||||
tracer: otel.GetTracer(opts.Ctx, "weatherRecorder"),
|
||||
meter: otel.GetMeter(opts.Ctx, "weatherRecorder"),
|
||||
RWMutex: &sync.RWMutex{},
|
||||
}
|
||||
}
|
65
pkg/weather/recorder/recorder_get.go
Normal file
65
pkg/weather/recorder/recorder_get.go
Normal file
@ -0,0 +1,65 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"k8s.io/utils/ptr"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
// Returns last requested number of weather updates
|
||||
// If negative number given, will return all weather observations
|
||||
func (w *WeatherRecorder) Get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
if req == nil {
|
||||
req = &pb.GetWeatherRequest{Limit: ptr.To(int32(-1))}
|
||||
}
|
||||
|
||||
if req.Limit == nil || *req.Limit < 0 {
|
||||
req.Limit = ptr.To(int32(-1))
|
||||
} else if *req.Limit == 0 {
|
||||
req.Limit = ptr.To(int32(1))
|
||||
}
|
||||
|
||||
var filterSN, filterST string
|
||||
if req.Opts != nil {
|
||||
filterSN = req.Opts.GetStationName()
|
||||
filterST = req.Opts.GetStationType()
|
||||
}
|
||||
|
||||
ctx, span := w.tracer.Start(ctx, "getWeatherRecorder")
|
||||
span.SetAttributes(
|
||||
attribute.String("stationNameFilter", filterSN),
|
||||
attribute.String("stationTypeFilter", filterST),
|
||||
attribute.Int("last", int(*req.Limit)),
|
||||
attribute.Int("currentSize", w.Count(ctx)),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
updates, err := w.recorder.Get(ctx, req)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
|
||||
return updates, err
|
||||
}
|
||||
|
||||
// Returns count of retained weather updates
|
||||
func (w *WeatherRecorder) Count(ctx context.Context) int {
|
||||
ctx, span := w.tracer.Start(ctx, "countWeatherRecorder")
|
||||
defer span.End()
|
||||
|
||||
count := w.recorder.Count(ctx)
|
||||
span.SetAttributes(attribute.Int("count", count))
|
||||
|
||||
span.SetStatus(codes.Ok, "")
|
||||
return count
|
||||
}
|
33
pkg/weather/recorder/recorder_set.go
Normal file
33
pkg/weather/recorder/recorder_set.go
Normal file
@ -0,0 +1,33 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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)
|
||||
}
|
24
pkg/weather/recorder/recorders/memory/count.go
Normal file
24
pkg/weather/recorder/recorders/memory/count.go
Normal file
@ -0,0 +1,24 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
)
|
||||
|
||||
func (r *MemoryRecorder) Count(ctx context.Context) int {
|
||||
_, span := r.tracer.Start(ctx, "countWeatherRecorder")
|
||||
defer span.End()
|
||||
|
||||
count := r.count()
|
||||
|
||||
span.SetAttributes(attribute.Int("count", count))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (r *MemoryRecorder) count() int {
|
||||
return len(r.updates)
|
||||
}
|
50
pkg/weather/recorder/recorders/memory/get.go
Normal file
50
pkg/weather/recorder/recorders/memory/get.go
Normal file
@ -0,0 +1,50 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (r *MemoryRecorder) Get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
_, span := r.tracer.Start(ctx, "memoryRecorder.Get")
|
||||
defer span.End()
|
||||
|
||||
r.RLock()
|
||||
defer r.RUnlock()
|
||||
|
||||
span.AddEvent("acquired lock on recorder cache")
|
||||
|
||||
limit := util.GetLimitFromReq(req)
|
||||
if r.count() == 0 {
|
||||
err := errors.New("no recorded updates to get")
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
} else if limit > 0 && r.count() <= limit {
|
||||
span.RecordError(errors.New("requested more updates than recorded"))
|
||||
}
|
||||
|
||||
updates := r.getUpdatesFromReq(req)
|
||||
span.AddEvent("request limit/opts applied to updates")
|
||||
|
||||
span.SetAttributes(attribute.Int("retrieved", len(updates)))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
return updates, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRecorder) getUpdatesFromReq(req *pb.GetWeatherRequest) []*weather.WeatherUpdate {
|
||||
if req.Opts == nil {
|
||||
return util.LimitUpdates(r.updates, util.GetLimitFromReq(req))
|
||||
}
|
||||
|
||||
return util.ApplyOptsToUpdates(r.updates, util.GetLimitFromReq(req), req.Opts)
|
||||
}
|
40
pkg/weather/recorder/recorders/memory/memory.go
Normal file
40
pkg/weather/recorder/recorders/memory/memory.go
Normal file
@ -0,0 +1,40 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders"
|
||||
)
|
||||
|
||||
const (
|
||||
DEF_RETAIN_LAST = 120
|
||||
NAME = "memory recorder"
|
||||
)
|
||||
|
||||
type MemoryRecorder struct {
|
||||
baseCtx context.Context
|
||||
updates []*weather.WeatherUpdate
|
||||
tracer trace.Tracer
|
||||
keep int
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
func (r *MemoryRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) {
|
||||
if opts.RetainLast < 1 {
|
||||
opts.RetainLast = DEF_RETAIN_LAST
|
||||
}
|
||||
|
||||
r.updates = make([]*weather.WeatherUpdate, 0, opts.RetainLast)
|
||||
r.keep = opts.RetainLast
|
||||
r.baseCtx = opts.BaseCtx
|
||||
r.RWMutex = &sync.RWMutex{}
|
||||
r.tracer = otel.GetTracer(r.baseCtx, "memoryRecorder")
|
||||
}
|
||||
|
||||
func (r *MemoryRecorder) Name() string { return NAME }
|
38
pkg/weather/recorder/recorders/memory/set.go
Normal file
38
pkg/weather/recorder/recorders/memory/set.go
Normal file
@ -0,0 +1,38 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (r *MemoryRecorder) Set(ctx context.Context, u *weather.WeatherUpdate) error {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
ctx, span := r.tracer.Start(ctx, "memoryRecorder.Set")
|
||||
span.SetAttributes(
|
||||
attribute.Int("countWeatherUpdates", r.Count(ctx)),
|
||||
attribute.Int("keepUpdates", r.keep),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
return r.set(ctx, u)
|
||||
}
|
||||
|
||||
func (r *MemoryRecorder) set(ctx context.Context, u *weather.WeatherUpdate) error {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
|
||||
if len(r.updates) > r.keep {
|
||||
r.updates = r.updates[1:]
|
||||
span.AddEvent("trimmed recorded updates by 1")
|
||||
}
|
||||
|
||||
r.updates = append(r.updates, u)
|
||||
span.AddEvent("recorded weather update")
|
||||
|
||||
return nil
|
||||
}
|
23
pkg/weather/recorder/recorders/noop/noop.go
Normal file
23
pkg/weather/recorder/recorders/noop/noop.go
Normal file
@ -0,0 +1,23 @@
|
||||
package noop
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder/recorders"
|
||||
)
|
||||
|
||||
type NoopRecorder struct{}
|
||||
|
||||
func (n *NoopRecorder) Init(context.Context, *recorders.RecorderOpts) {}
|
||||
|
||||
func (n *NoopRecorder) Set(context.Context, *weather.WeatherUpdate) error { return nil }
|
||||
|
||||
func (n *NoopRecorder) Get(context.Context, *pb.GetWeatherRequest) ([]*weather.WeatherUpdate, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (n *NoopRecorder) Count(context.Context) int { return 0 }
|
||||
|
||||
func (r *NoopRecorder) Name() string { return "no-op recorder" }
|
23
pkg/weather/recorder/recorders/recorders.go
Normal file
23
pkg/weather/recorder/recorders/recorders.go
Normal file
@ -0,0 +1,23 @@
|
||||
package recorders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
type RecorderOpts struct {
|
||||
RetainLast int
|
||||
BaseCtx context.Context
|
||||
AppConfig *config.AmbientLocalExporterConfig
|
||||
}
|
||||
|
||||
type Recorder interface {
|
||||
Init(context.Context, *RecorderOpts)
|
||||
Set(context.Context, *weather.WeatherUpdate) error
|
||||
Get(context.Context, *pb.GetWeatherRequest) ([]*weather.WeatherUpdate, error)
|
||||
Count(context.Context) int // Best Effort
|
||||
Name() string
|
||||
}
|
38
pkg/weather/recorder/recorders/redis/count.go
Normal file
38
pkg/weather/recorder/recorders/redis/count.go
Normal file
@ -0,0 +1,38 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func (r *RedisRecorder) Count(ctx context.Context) int {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.count")
|
||||
defer span.End()
|
||||
|
||||
r.RLock()
|
||||
defer r.RUnlock()
|
||||
|
||||
return r.count(ctx)
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) count(ctx context.Context) int {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.count.redis", trace.WithAttributes(
|
||||
attribute.String("updatesKey", r.Key())))
|
||||
defer span.End()
|
||||
|
||||
count, err := r.redis.LLen(ctx, r.Key()).Result()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.log.Err(err).Send()
|
||||
return int(count)
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Int64("updatesCount", count))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
return int(count)
|
||||
}
|
98
pkg/weather/recorder/recorders/redis/get.go
Normal file
98
pkg/weather/recorder/recorders/redis/get.go
Normal file
@ -0,0 +1,98 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (r *RedisRecorder) Get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.get", trace.WithAttributes(
|
||||
attribute.Int("limit", util.GetLimitFromReq(req)),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
return r.get(ctx, req)
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) get(ctx context.Context, req *pb.GetWeatherRequest) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.get.redis")
|
||||
defer span.End()
|
||||
|
||||
limit := util.GetLimitFromReq(req)
|
||||
if limit < 1 {
|
||||
limit = r.keep
|
||||
}
|
||||
|
||||
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)-1).Result()
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.log.Err(err).Send()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
span.AddEvent("redis queried")
|
||||
r.log.Debug().Int("results", len(datas)).Msg("redis queried")
|
||||
|
||||
updates, err := jsonDatasToUpdates(datas)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.log.Err(err).Send()
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
|
||||
span.AddEvent("results unmarshalled")
|
||||
span.SetAttributes(attribute.Int("results", len(updates)))
|
||||
|
||||
filtered := util.ApplyOptsToUpdates(updates, limit, req.Opts)
|
||||
|
||||
span.AddEvent("results filtered")
|
||||
span.SetAttributes(
|
||||
attribute.Int("filteredResults", len(filtered)),
|
||||
attribute.Int("resultsFiltered", len(updates)-len(filtered)),
|
||||
)
|
||||
|
||||
r.log.Debug().
|
||||
Int("updatesRetrieved", len(updates)).
|
||||
Int("updatesAfterFiltering", len(filtered)).
|
||||
Int("updatesFiltered", len(updates)-len(filtered)).
|
||||
Msg("updates retrieved from redis")
|
||||
|
||||
return filtered, err
|
||||
}
|
||||
|
||||
func jsonDatasToUpdates(datas []string) ([]*weather.WeatherUpdate, error) {
|
||||
var errs error
|
||||
updates := make([]*weather.WeatherUpdate, 0, len(datas))
|
||||
|
||||
for _, data := range datas {
|
||||
update := new(weather.WeatherUpdate)
|
||||
err := json.Unmarshal([]byte(data), update)
|
||||
errs = errors.Join(errs, err)
|
||||
|
||||
if err == nil {
|
||||
updates = append(updates, update)
|
||||
}
|
||||
}
|
||||
|
||||
return slices.Clip(updates), errs
|
||||
}
|
114
pkg/weather/recorder/recorders/redis/redis.go
Normal file
114
pkg/weather/recorder/recorders/redis/redis.go
Normal file
@ -0,0 +1,114 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"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/recorder/recorders"
|
||||
)
|
||||
|
||||
const (
|
||||
DEF_RETAIN = 120
|
||||
UPDATES_KEY = "weatherUpdates"
|
||||
NAME = "redis recorder"
|
||||
)
|
||||
|
||||
type RedisRecorder struct {
|
||||
baseCtx context.Context
|
||||
tracer trace.Tracer
|
||||
redis *redis.Client
|
||||
config *config.AmbientLocalExporterConfig
|
||||
log *zerolog.Logger
|
||||
appKey string // prefix for redis keys, uses app name, environment, and version
|
||||
keep int
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) {
|
||||
if opts.AppConfig.RecorderConfig.RedisConfig == nil {
|
||||
panic("refusing to init redis recorder with no redisConfig")
|
||||
}
|
||||
|
||||
if opts.RetainLast < 1 {
|
||||
opts.RetainLast = DEF_RETAIN
|
||||
}
|
||||
|
||||
r.config = opts.AppConfig
|
||||
r.keep = opts.RetainLast
|
||||
r.RWMutex = &sync.RWMutex{}
|
||||
r.baseCtx = opts.BaseCtx
|
||||
r.log = zerolog.Ctx(r.baseCtx)
|
||||
|
||||
r.tracer = otel.GetTracer(r.baseCtx, "redisRecorder")
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.init", trace.WithAttributes(
|
||||
attribute.String("redisHost", opts.AppConfig.RecorderConfig.RedisConfig.RedisHost),
|
||||
attribute.Int("retainLast", opts.RetainLast),
|
||||
attribute.Int("redisPort", opts.AppConfig.RecorderConfig.RedisConfig.RedisPort),
|
||||
attribute.Bool("tls", opts.AppConfig.RecorderConfig.RedisConfig.RedisTLS),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
// Unique key prefix for this version/env/name of exporter
|
||||
// will be consistent across replicas, but resets on upgrade
|
||||
// as it is using version
|
||||
r.appKey = util.GetAppHash(r.config.AppConfig)
|
||||
|
||||
r.MustInitRedis(ctx)
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) MustInitRedis(ctx context.Context) {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.init.redis")
|
||||
defer span.End()
|
||||
|
||||
rc := r.config.RecorderConfig.RedisConfig
|
||||
|
||||
var tlsConfig *tls.Config
|
||||
if rc.RedisTLS {
|
||||
tlsConfig = &tls.Config{
|
||||
ServerName: rc.RedisHost,
|
||||
InsecureSkipVerify: rc.RedisTLSInsecure,
|
||||
}
|
||||
}
|
||||
|
||||
r.redis = redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", rc.RedisHost, rc.RedisPort),
|
||||
ClientName: fmt.Sprintf("%s-%s", r.config.Name, r.config.Environment),
|
||||
Username: rc.RedisUser,
|
||||
Password: rc.RedisPassword,
|
||||
DB: rc.RedisDB,
|
||||
TLSConfig: tlsConfig,
|
||||
})
|
||||
|
||||
span.AddEvent("redis client ready")
|
||||
|
||||
resp := r.redis.Ping(ctx)
|
||||
if resp.Err() != nil {
|
||||
span.RecordError(resp.Err())
|
||||
span.SetStatus(codes.Error, resp.Err().Error())
|
||||
r.log.Fatal().Err(resp.Err()).Msg("failed to ping redis")
|
||||
}
|
||||
|
||||
span.AddEvent("redis client ping ok")
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
r.log.Info().Str("appKey", r.appKey).
|
||||
Msg("redis ping ok, client ready")
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) Key() string {
|
||||
return fmt.Sprintf("%s:%s", r.appKey, UPDATES_KEY)
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) Name() string { return NAME }
|
74
pkg/weather/recorder/recorders/redis/set.go
Normal file
74
pkg/weather/recorder/recorders/redis/set.go
Normal file
@ -0,0 +1,74 @@
|
||||
package redis
|
||||
|
||||
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"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (r *RedisRecorder) Set(ctx context.Context, u *weather.WeatherUpdate) error {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.set", trace.WithAttributes(
|
||||
attribute.String("stationName", u.GetStationName()),
|
||||
attribute.String("stationType", util.DerefStr(u.StationType)),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
// First ensure we can prepare our payload
|
||||
data, err := json.Marshal(u)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
|
||||
r.log.Err(err).Send()
|
||||
return err
|
||||
}
|
||||
|
||||
return r.set(ctx, data)
|
||||
}
|
||||
|
||||
func (r *RedisRecorder) set(ctx context.Context, data []byte) error {
|
||||
ctx, span := r.tracer.Start(ctx, "redisRecorder.set.push", trace.WithAttributes(
|
||||
attribute.Int("updateBytes", len(data)),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
// Atomic, push and trim
|
||||
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())
|
||||
}
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
r.log.Err(err).Send()
|
||||
return err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
type WeatherState struct {
|
||||
updates []*weather.WeatherUpdate
|
||||
keep int
|
||||
ctx context.Context
|
||||
tracer trace.Tracer
|
||||
meter metric.Meter
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
type Opts struct {
|
||||
Ctx context.Context
|
||||
KeepLast int
|
||||
}
|
||||
|
||||
func NewWeatherState(opts *Opts) *WeatherState {
|
||||
if opts.KeepLast < 1 {
|
||||
opts.KeepLast = 1
|
||||
}
|
||||
|
||||
return &WeatherState{
|
||||
updates: make([]*weather.WeatherUpdate, 0),
|
||||
keep: opts.KeepLast,
|
||||
ctx: opts.Ctx,
|
||||
tracer: otel.GetTracer(opts.Ctx, "weatherState"),
|
||||
meter: otel.GetMeter(opts.Ctx, "weatherState"),
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
// Returns last requested number of weather updates
|
||||
func (w *WeatherState) Get(ctx context.Context, last int) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
if last < 1 {
|
||||
last = 1
|
||||
}
|
||||
|
||||
ctx, span := w.tracer.Start(ctx, "getWeatherState")
|
||||
span.SetAttributes(
|
||||
attribute.Int("last", last),
|
||||
attribute.Int("keep", w.keep),
|
||||
attribute.Int("currentSize", w.Count()),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
updates, err := w.get(ctx, last)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
} else {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
|
||||
return updates, err
|
||||
}
|
||||
|
||||
func (w *WeatherState) get(ctx context.Context, last int) (
|
||||
[]*weather.WeatherUpdate, error,
|
||||
) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
|
||||
w.RLock()
|
||||
defer w.Unlock()
|
||||
|
||||
span.AddEvent("acquired lock on state cache")
|
||||
|
||||
updates := w.updates
|
||||
|
||||
if w.count() == 0 {
|
||||
err := errors.New("no state to get")
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
} else if w.count() <= last {
|
||||
span.RecordError(errors.New("requested more state than exists"))
|
||||
} else {
|
||||
updates = updates[len(updates)-last:]
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.Int("retrieved", len(updates)))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
return updates, nil
|
||||
}
|
||||
|
||||
// Returns count of retained weather updates
|
||||
func (w *WeatherState) Count() int {
|
||||
_, span := w.tracer.Start(w.ctx, "countWeatherState")
|
||||
defer span.End()
|
||||
|
||||
count := w.count()
|
||||
|
||||
span.SetAttributes(attribute.Int("count", count))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (w *WeatherState) count() int {
|
||||
w.RLock()
|
||||
defer w.RUnlock()
|
||||
|
||||
return len(w.updates)
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
)
|
||||
|
||||
func (w *WeatherState) Set(ctx context.Context, u *weather.WeatherUpdate) error {
|
||||
_, span := w.tracer.Start(ctx, "setWeatherState")
|
||||
span.SetAttributes(
|
||||
attribute.Int("countWeatherUpdates", w.Count()),
|
||||
attribute.Int("keepUpdates", w.keep),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
return w.set(span, u)
|
||||
}
|
||||
|
||||
func (w *WeatherState) set(span trace.Span, u *weather.WeatherUpdate) error {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
if len(w.updates) > w.keep {
|
||||
w.updates = w.updates[1:]
|
||||
span.AddEvent("trimmed state updates by 1")
|
||||
}
|
||||
|
||||
w.updates = append(w.updates, u)
|
||||
span.AddEvent("recorded weather state")
|
||||
return nil
|
||||
}
|
@ -48,6 +48,7 @@ message WeatherUpdate {
|
||||
optional double dew_point_f = 25;
|
||||
optional double wind_chill_f = 26;
|
||||
repeated TempHumiditySensor temp_humidity_sensors = 27;
|
||||
google.protobuf.Timestamp update_timestamp = 28;
|
||||
}
|
||||
|
||||
// Represents a temperature and humidity sensor
|
||||
|
Reference in New Issue
Block a user