Added Makefile, added documentation

This commit is contained in:
tess1o
2024-06-14 14:57:27 +03:00
parent daabcad5e1
commit a0e511f521
8 changed files with 103 additions and 25 deletions

View File

@@ -9,7 +9,7 @@ COPY go.mod go.sum ./
RUN go mod download
# Copy the source code
COPY main.go ./
COPY *.go ./
# Build the Go application
RUN CGO_ENABLED=0 GOOS=linux go build -o ecoflow-exporter .

24
Makefile Normal file
View File

@@ -0,0 +1,24 @@
.PHONY: build push start start-local
DOCKER_IMAGE_NAME = tess1o/go-ecoflow-exporter
DOCKER_COMPOSE_FILE = docker-compose/compose.yaml
DOCKER_COMPOSE_LOCAL_BUILD_FILE = docker-compose/compose-local-build.yaml
build:
docker build --platform linux/amd64 -t $(DOCKER_IMAGE_NAME):latest .
push:
# you have to login to docker registry first
docker push $(DOCKER_IMAGE_NAME)
start:
# start exporter, grafana and prometheus from external images
docker-compose -f $(DOCKER_COMPOSE_FILE) up
docker-compose -f $(DOCKER_COMPOSE_FILE) ps
start-local:
#build exporter from local source and then start it with grafana and prometheus
docker-compose -f $(DOCKER_COMPOSE_LOCAL_BUILD_FILE) build --no-cache
docker-compose -f $(DOCKER_COMPOSE_LOCAL_BUILD_FILE) up -d
docker-compose -f $(DOCKER_COMPOSE_LOCAL_BUILD_FILE) ps
build-push: build push

View File

@@ -1,8 +1,4 @@
# EcoFlow to Prometheus exporter in Go via Rest API
# Caution
It's a beta version. It works fine, I need to provide more documentation
# EcoFlow to Prometheus exporter in Go via Rest API
## About the project
@@ -12,6 +8,64 @@ API. More details about the API are on their website: https://developer-eu.ecofl
Other known to me projects use MQTT protocol to scrap the metrics, this implementation uses Rest API.
## How to get Access Token and Secret Token
1. Go to https://developer-eu.ecoflow.com/
2. Click on "Become a Developer"
3. Login with your Ecoflow username and Password
4. Wait until the access is approved by Ecoflow
5. Receive email with subject "Approval notice from EcoFlow Developer Platform". May take some time
6. Go to https://developer-eu.ecoflow.com/us/security and create new AccessKey and SecretKey
## How to run the Exporter, Prometheus and Grafana using docker-compose
1. Go to docker-compose folder: `cd docker-compose`
2. Update `.env` file with two mandatory parameters:
- `ECOFLOW_ACCESS_KEY`
- `ECOFLOW_SECRET_KEY`
3. (OPTIONALLY) Update other variables if you need to:
- `METRIC_PREFIX`: the prefix that will be added to all metrics. Default value is `ecoflow`. For instance
metric `bms_bmsStatus.minCellTemp` will be exported to prometheus as `ecoflow.bms_bmsStatus.minCellTemp`. With
default value `ecoflow` you can use Grafana Dashboard with ID `17812` without any changes.
- `PROMETHEUS_INTERVAL` - scrapping interval in seconds. How often should the exporter execute requests to Ecoflow
Rest API in order to get the data. Default value is 30 seconds. Align this value
with `docker-compose/prometheus/prometheus.yml`
- `DEBUG_ENABLED` - enable debug log messages. Default value is "false". To enable use values `true` or `1`
- `GRAFANA_USERNAME` - admin username in Grafana. Can be changed later in Grafana UI
- `GRAFANA_PASSWORD` - admin password in Grafana. Can be changed later in Grafana UI
4. Save `.env` file with your changes.
5. Start all containers: `docker-compose -f compose.yaml up -d`
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93c9cf317861 docker-compose-go_ecoflow_exporter "/app/ecoflow-export…" 6 seconds ago Up 5 seconds 0.0.0.0:2112->2112/tcp, :::2112->2112/tcp go_ecoflow_exporter
fea150b4ef5d grafana/grafana "/run.sh" 16 minutes ago Up 5 seconds 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp grafana
823c6adfad90 prom/prometheus "/bin/prometheus --c…" 16 minutes ago Up 5 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus
```
6. The services are available here:
- http://localhost:2112/metrics - the exporter
- http://localhost:9090 - Prometheus
- http://localhost:3000 - Grafana
7. Navigate to http://localhost:3000 in your web browser and use GRAFANA_USERNAME / GRAFANA_PASSWORD credentials from
.env file to access Grafana. It is already configured with prometheus as the default datasource.
Navigate to Dashboards → Import dashboard → import ID `17812`, select the only existing Prometheus datasource.
(The Grafana dashboard was implemented
in https://github.com/berezhinskiy/ecoflow_exporter/tree/master/docker-compose)
### Build from source code
In order to build image locally and run the services use the instruction above. Step#5 should be replaced with the
commands:
```
docker-compose -f compose-local-build.yaml build --no-cache
docker-compose -f compose-local-build.yaml up -d
```
It will build the exporter from local source code and start the exporter, prometheus and grafana
## Compare to other exporters
This implementation is inspired by https://github.com/berezhinskiy/ecoflow_exporter, and it's fully
@@ -33,9 +87,8 @@ Some difference between this project and https://github.com/berezhinskiy/ecoflow
4. This implementation is extremely lightweight and barely consumes any RAM & CPU (it needs less than 10MB of RAM to
scrap metrics from 2 devices)
# TODO
1. Instructions how to get AccessToken and SecretToken
2. Instructions how to run exporter, grafana and prometheus
3. Instructions how to import grafana dashboard
4. Github workflow to automatically push new
# TODO
- Github workflow to automatically push new image on commit
- Add arm64 docker image

View File

@@ -1,7 +1,7 @@
ECOFLOW_ACCESS_KEY=your_access_key_from_ecoflow
ECOFLOW_SECRET_KEY=your_secret_key_from_ecoflow
METRIC_PREFIX=ecoflow
PROMETHEUS_INTERVAL=10
DEBUG_ENABLED=false
GRAFANA_USERNAME=grafana
GRAFANA_PASSWORD=grafana
ECOFLOW_ACCESS_KEY=your_access_key_from_ecoflow
ECOFLOW_SECRET_KEY=your_secret_key_from_ecoflow
METRIC_PREFIX=ecoflow
PROMETHEUS_INTERVAL=30
DEBUG_ENABLED=false
GRAFANA_USERNAME=grafana
GRAFANA_PASSWORD=grafana

View File

@@ -33,8 +33,8 @@ services:
- 2112:2112
restart: unless-stopped
environment:
ACCESS_KEY: ${ACCESS_KEY}
SECRET_KEY: ${SECRET_KEY}
ECOFLOW_ACCESS_KEY: ${ECOFLOW_ACCESS_KEY}
ECOFLOW_SECRET_KEY: ${ECOFLOW_SECRET_KEY}
METRIC_PREFIX: ${METRIC_PREFIX}
PROMETHEUS_INTERVAL: ${PROMETHEUS_INTERVAL}

View File

@@ -1,7 +1,7 @@
global:
scrape_interval: 10s # Set the scrape interval to every 10 seconds. Default is every 1 minute.
scrape_interval: 30s # Set the scrape interval to every 30 seconds. Default is every 1 minute.
scrape_timeout: 10s
evaluation_interval: 10s # Evaluate rules every 10 seconds. The default is every 1 minute.
evaluation_interval: 30s # Evaluate rules every 30 seconds. The default is every 1 minute.
scrape_configs:
- job_name: prometheus

View File

@@ -22,7 +22,7 @@ func main() {
secretKey := os.Getenv("ECOFLOW_SECRET_KEY")
if accessKey == "" || secretKey == "" {
slog.Error("AccessKey and SecretKey are mandatory")
slog.Error("ECOFLOW_ACCESS_KEY and ECOFLOW_SECRET_KEY are mandatory")
return
}

View File

@@ -42,8 +42,9 @@ func RecordPrometheusMetrics(c *ecoflow.Client, config *PrometheusConfig) {
ticker := time.NewTicker(config.Interval)
var metrics = make(map[string]prometheus.Gauge)
go func() {
for n := time.Now(); ; _ = <-ticker.C {
slog.Info("Getting ecoflow parameters.", "time", n)
now := time.Now()
for _ = time.Now(); ; now = <-ticker.C {
slog.Info("Getting ecoflow parameters.", "time", now)
devices, err := c.GetDeviceList(context.Background())
if err != nil {
slog.Error("Cannot get devices list", "error", err)