Skip to content

Commit

Permalink
Reduce scope
Browse files Browse the repository at this point in the history
There is no need to have the timestamp into the exporter. Just
keep the transaction number. If the transaction number does not
change, it is enough to see the issue.
  • Loading branch information
evrardjp committed Nov 30, 2024
1 parent 36dcfe5 commit 40b06a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
7 changes: 3 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ This is appropriate for monitoring on a server in which you do not want to store
You should add other monitoring tools to check consistency with other exporters.

## Features
- Metrics for the last transaction timestamp and number for each repository.
- Configurable via a JSON file.
- Metrics for the last transaction number for each repository.
- Configurable via a JSON file for multiple repos

## Usage

Expand All @@ -23,11 +23,10 @@ You should add other monitoring tools to check consistency with other exporters.
```
2. Run the exporter:
```bash
./borgbackuptransactions_exporter --config=config.json
./borgbackuptransactions_exporter -config=config.json
```

### Metrics exposed
- `borgbackup_last_transaction_timestamp`
- `borgbackup_last_transaction_number`

### How to scrape
Expand Down
25 changes: 5 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ const (
)

var (
lastTransactionTimestamp = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "borgbackup_last_transaction_timestamp",
Help: "Unix timestamp of the last transaction in the BorgBackup repository",
},
[]string{"repo"},
)
lastTransactionNumber = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "borgbackup_last_transaction_number",
Expand All @@ -54,7 +47,6 @@ var (
)

func init() {
prometheus.MustRegister(lastTransactionTimestamp)
prometheus.MustRegister(lastTransactionNumber)
}

Expand Down Expand Up @@ -187,33 +179,26 @@ func updateRepoMetrics(repo string) {
// instead of deferring as usual, close as soon as the
file.Close()

transactionNumber, timestamp, err := parseTransactionLine(lastLine)
transactionNumber, err := parseTransactionLine(lastLine)
if err != nil {
log.Printf("Failed to parse transactions file for repo %s: %v", repo, err)
return
}

lastTransactionTimestamp.WithLabelValues(repo).Set(float64(timestamp))
lastTransactionNumber.WithLabelValues(repo).Set(float64(transactionNumber))
}

func parseTransactionLine(line string) (int, int64, error) {
func parseTransactionLine(line string) (int, error) {
parts := strings.Split(line, ",")
if len(parts) < 2 {
return 0, 0, fmt.Errorf("invalid line format: %s", line)
return 0, fmt.Errorf("invalid line format: %s", line)
}

numberStr := strings.TrimPrefix(parts[0], "transaction ")
transactionNumber, err := strconv.Atoi(strings.TrimSpace(numberStr))
if err != nil {
return 0, 0, fmt.Errorf("failed to parse transaction number: %v", err)
}

timeStr := strings.TrimSpace(strings.Replace(parts[1], "UTC time", "", -1))
t, err := time.Parse("2006-01-02T15:04:05.000000", timeStr)
if err != nil {
return 0, 0, fmt.Errorf("failed to parse UTC time: %v", err)
return 0, fmt.Errorf("failed to parse transaction number: %v", err)
}

return transactionNumber, t.Unix(), nil
return transactionNumber, nil
}
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package main

import (
"testing"
"time"
)

func TestParseTransactionLine(t *testing.T) {
line := "transaction 6374, UTC time 2024-11-30T11:45:36.870201"
number, timestamp, err := parseTransactionLine(line)
// number, timestamp, err := parseTransactionLine(line)
number, err := parseTransactionLine(line)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

expectedNumber := 6374
expectedTime := time.Date(2024, 11, 30, 11, 45, 36, 870201000, time.UTC).Unix()
// expectedTime := time.Date(2024, 11, 30, 11, 45, 36, 870201000, time.UTC).Unix()

if number != expectedNumber {
t.Errorf("Expected transaction number %d, got %d", expectedNumber, number)
}

if timestamp != expectedTime {
/* if timestamp != expectedTime {
t.Errorf("Expected timestamp %d, got %d", expectedTime, timestamp)
}
} */
}

0 comments on commit 40b06a6

Please sign in to comment.