Skip to content

Commit

Permalink
Merge branch 'r4ptor-add-history-data'
Browse files Browse the repository at this point in the history
  • Loading branch information
scross01 committed Dec 17, 2022
2 parents 764ac98 + 2bcdc18 commit d43eca7
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

v0.7.1
------

- Added history data metrics including total print time or total filament used.
Add the new `history` module in your `prometheus.yml` config. Thanks to @r4ptor #12

v0.7.0
------

Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ These changes affect the following metrics groups
- `klipper_temperature_fan_`*
- `klipper_output_pin_`*


For example:

- `klipper_network_`**<code>wlan0</code>**`_rx_bytes` becomes `klipper_network_rx_bytes{interface="`**<code>wlan0</code>**`"}`
- `klipper_temperature_sensor_`**<code>mtu</code>**`_temperature` becomes `klipper_temperature_sensor_temperature{sensor="`**<code>mtu</code>**`"}`



Usage
-----

Expand All @@ -57,7 +54,7 @@ scrape_configs:
static_configs:
- targets: [ 'klipper.local:7125' ]
params:
modules: [ "process_stats", "job_queue", "system_info", "network_stats", "directory_info", "printer_objects" ]
modules: [ "process_stats", "job_queue", "system_info", "network_stats", "directory_info", "printer_objects", "history" ]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
Expand Down Expand Up @@ -161,3 +158,4 @@ group of metrics is queried from a different Moonraker API endpoint.
| `directory_info` | | `klipper_disk_usage_available`<br/>`klipper_disk_usage_total`<br/>`klipper_disk_usage_used` |
| `temperature` | | `klipper_extruder_power`<br/>`klipper_extruder_target`<br/>`klipper_extruder_temperature`<br/>`klipper_heater_bed_power`<br/>`klipper_heater_bed_target`<br/>`klipper_heater_bed_temperature`<br/>`klipper_temperature_sensor_*_temperature` |
| `printer_objects` | | `klipper_extruder_power`<br/>`klipper_extruder_pressure_advance`<br/>`klipper_extruder_smooth_time`<br/>`klipper_extruder_target`<br/>`klipper_extruder_temperature`<br/>`klipper_fan_rpm`<br/>`klipper_fan_speed`<br/>`klipper_gcode_extrude_factor`<br/>`klipper_gcode_speed_factor`<br/>`klipper_gcode_speed`<br/>`klipper_heater_bed_power`<br/>`klipper_heater_bed_target`<br/>`klipper_heater_bed_temperature`<br/>`klipper_output_pin_value{pin="`*pin*`"}`<br/>`klipper_printing_time`<br/>`klipper_print_filament_used`<br/>`klipper_print_file_position`<br/>`klipper_print_file_progress`<br/>`klipper_print_gcode_progress`<br/>`klipper_print_total_duration`<br/>`klipper_temperature_fan_speed{fan="`*fan*`"}`<br/>`klipper_temperature_fan_temperature{fan="`*fan*`"}`<br/>`klipper_temperature_fan_target{fan="`*fan*`"}`<br/>`klipper_temperature_sensor_temperature{sensor="`*sensor*`"}`<br/>`klipper_temperature_sensor_measured_max_temp{sensor="`*sensor*`"}`<br/>`klipper_temperature_sensor_measured_min_temp{sensor="`*sensor*`"}`<br/>`klipper_toolhead_estimated_print_time`<br/>`klipper_toolhead_max_accel_to_decel`<br/>`klipper_toolhead_max_accel`<br/>`klipper_toolhead_max_velocity`<br/>`klipper_toolhead_print_time`<br/>`klipper_toolhead_square_corner_velocity` |
| `history` | | `klipper_total_jobs`<br/>`klipper_total_time`<br/>`klipper_total_print_time`<br/>`klipper_total_filament_used`<br/>`klipper_longest_job`<br/>`klipper_longest_print`<br/>
35 changes: 32 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,35 @@ func (c collector) Collect(ch chan<- prometheus.Metric) {
prometheus.GaugeValue,
float64(len(result.Result.QueuedJobs)))
}
// Job History
if slices.Contains(c.modules, "history") {
c.logger.Infof("Collecting history for %s", c.target)
result, _ := c.fetchMoonrakerHistory(c.target)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_total_jobs", "Klipper number of total jobs.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.Jobs))
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_total_time", "Klipper total time.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.TotalTime))
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_total_print_time", "Klipper total print time.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.PrintTime))
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_total_filament_used", "Klipper total meters of filament used.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.FilamentUsed))
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_longest_job", "Klipper total longest job.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.LongestJob))
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("klipper_longest_print", "Klipper total longest print.", nil, nil),
prometheus.GaugeValue,
float64(result.Result.JobTotals.LongestPrint))
}

// System Info
if slices.Contains(c.modules, "system_info") {
Expand Down Expand Up @@ -355,17 +384,17 @@ func (c collector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
temperatureSensor,
prometheus.GaugeValue,
sv.Temperature,
sv.Temperature,
sensorName)
ch <- prometheus.MustNewConstMetric(
temperatureSensorMinTemp,
prometheus.GaugeValue,
sv.MeasuredMinTemp,
sv.MeasuredMinTemp,
sensorName)
ch <- prometheus.MustNewConstMetric(
temperatureSensorMaxTemp,
prometheus.GaugeValue,
sv.MeasuredMaxTemp,
sv.MeasuredMaxTemp,
sensorName)
}

Expand Down
49 changes: 49 additions & 0 deletions collector/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package collector

// https://moonraker.readthedocs.io/en/latest/web_api/#history-apis

import (
"encoding/json"
"io/ioutil"

"net/http"
)

type MoonrakerHistoryResponse struct {
Result struct {
JobTotals struct {
Jobs int64 `json:"total_jobs"`
TotalTime float64 `json:"total_time"`
PrintTime float64 `json:"total_print_time"`
FilamentUsed float64 `json:"total_filament_used"`
LongestJob float64 `json:"longest_job"`
LongestPrint float64 `json:"longest_print"`
} `json:"job_totals"`
} `json:"result"`
}

func (c collector) fetchMoonrakerHistory(klipperHost string) (*MoonrakerHistoryResponse, error) {
var procStatsUrl = "http://" + klipperHost + "/server/history/totals"
c.logger.Debug("Collecting metrics from " + procStatsUrl)
res, err := http.Get(procStatsUrl)
if err != nil {
c.logger.Error(err)
return nil, err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
c.logger.Fatal(err)
return nil, err
}

var response MoonrakerHistoryResponse

err = json.Unmarshal(data, &response)
if err != nil {
c.logger.Fatal(err)
return nil, err
}

return &response, nil
}
6 changes: 3 additions & 3 deletions example/prometheus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ scrape_configs:
static_configs:
- targets: [ 'klipper.home.lan:7125' ]
params:
modules: [ 'process_stats', 'network_stats', 'system_info', 'job_queue', 'directory_info', 'printer_objects' ]
modules: [ 'process_stats', 'network_stats', 'system_info', 'job_queue', 'directory_info', 'printer_objects', 'history' ]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: klipper-exporter:9101
replacement: study-imac.home.lan:9101

- job_name: "klipper-exporter"
scrape_interval: 5s
metrics_path: /metrics
static_configs:
- targets: [ 'klipper-exporter:9101' ]
- targets: [ 'study-imac.home.lan:9101' ]
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.7.0
v0.7.1

0 comments on commit d43eca7

Please sign in to comment.