Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mem leak #24

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
on: pull_request
name: Pull Request
defaults:
run:
shell: powershell
jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ">=1.22.0"
- name: gofmt
working-directory: ./Companion
run: |
if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then
exit 1
fi
test:
defaults:
run:
shell: powershell
name: test
runs-on: windows-latest
steps:
Expand Down Expand Up @@ -34,4 +50,4 @@ jobs:

- name: Test README generation
run: |
make readme
make readme
4 changes: 2 additions & 2 deletions Companion/exporter/collector_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"

"github.com/AP-Hunt/FicsitRemoteMonitoringCompanion/Companion/exporter"
. "github.com/onsi/ginkgo/v2"
"github.com/coder/quartz"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ var _ = Describe("CollectorRunner", func() {
})

It("sanitizes session name", func() {
Expect(exporter.SanitizeSessionName(`it's giving -- 123456!@#$%^&*() yo hollar "'"`)).To(Equal(`its giving 123456 yo hollar ` ))
Expect(exporter.SanitizeSessionName(`it's giving -- 123456!@#$%^&*() yo hollar "'"`)).To(Equal(`its giving 123456 yo hollar `))
})
})
})
3 changes: 1 addition & 2 deletions Companion/exporter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ func retrieveData(frmAddress string, details any) error {
return err
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("non-200 returned when retireving data: %d", resp.StatusCode)
}

defer resp.Body.Close()

decoder := json.NewDecoder(resp.Body)

err = decoder.Decode(&details)
Expand Down
6 changes: 3 additions & 3 deletions Companion/exporter/factory_building_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ var _ = Describe("FactoryBuildingCollector", func() {
IsPaused: false,
CircuitGroupId: 0,
PowerInfo: exporter.PowerInfo{
CircuitGroupId: 1,
PowerConsumed: 23,
MaxPowerConsumed: 4,
CircuitGroupId: 1,
PowerConsumed: 23,
MaxPowerConsumed: 4,
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion Companion/exporter/frm_server_fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type FRMServerFake struct {
extractorData []exporter.ExtractorDetails
portalData []exporter.PortalDetails
hypertubeData []exporter.HypertubeDetails
frackingData []exporter.FrackingDetails
frackingData []exporter.FrackingDetails
}

func NewFRMServerFake() *FRMServerFake {
Expand Down
12 changes: 6 additions & 6 deletions Companion/exporter/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var _ = Describe("metrics dropper", func() {
globalReg.Unregister(metric)
})
It("Keeps fresh metrics", func() {
dropper.CacheFreshMetricLabel(prometheus.Labels{"label1":"val1"})
metric.WithLabelValues("val1","val2").Set(1)
dropper.CacheFreshMetricLabel(prometheus.Labels{"label1": "val1"})
metric.WithLabelValues("val1", "val2").Set(1)
dropper.DropStaleMetricLabels()
expectGauge(metric, "val1", "val2").To(Equal(1.0))
})
It("drops old metrics", func() {
dropper.CacheFreshMetricLabel(prometheus.Labels{"label1":"val1"})
metric.WithLabelValues("val1","val2").Set(1)
metric.WithLabelValues("val3","val4").Set(1)
dropper.CacheFreshMetricLabel(prometheus.Labels{"label1": "val1"})
metric.WithLabelValues("val1", "val2").Set(1)
metric.WithLabelValues("val3", "val4").Set(1)
dropper.DropStaleMetricLabels()
metric.WithLabelValues("val3","val4").Set(2)
metric.WithLabelValues("val3", "val4").Set(2)
dropper.DropStaleMetricLabels()
expectGauge(metric, "val1", "val2").To(Equal(0.0))
expectGauge(metric, "val3", "val4").To(Equal(2.0))
Expand Down
4 changes: 2 additions & 2 deletions Companion/exporter/vehicle_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type VehicleCollector struct {
endpoint string
TrackedVehicles map[string]*VehicleDetails
metricsDropper *MetricsDropper
metricsDropper *MetricsDropper
}

type VehicleDetails struct {
Expand All @@ -23,7 +23,7 @@ type VehicleDetails struct {
PathName string `json:"PathName"`
DepartTime time.Time
Departed bool
LastTracked time.Time
LastTracked time.Time
}

type Fuel struct {
Expand Down
15 changes: 10 additions & 5 deletions Companion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"text/template"
Expand Down Expand Up @@ -46,18 +47,21 @@ func main() {
frmHostname = lookupEnvWithDefault("FRM_HOST", frmHostname)
frmPort = lookupEnvWithDefault("FRM_PORT", frmPort)
frmHostnames = lookupEnvWithDefault("FRM_HOSTS", frmHostnames)
logStdout, _ := strconv.ParseBool(lookupEnvWithDefault("FRM_LOG_STDOUT", "0"))

if genReadme {
generateReadme()
os.Exit(0)
}

logFile, err := createLogFile()
if err != nil {
fmt.Printf("error creating log file: %s", err)
os.Exit(1)
if !logStdout {
logFile, err := createLogFile()
if err != nil {
fmt.Printf("error creating log file: %s", err)
os.Exit(1)
}
log.Default().SetOutput(logFile)
}
log.Default().SetOutput(logFile)

// Create exporter
frmUrls := []string{}
Expand All @@ -75,6 +79,7 @@ func main() {
promExporter = exporter.NewPrometheusExporter(frmUrls)

var prom *prometheus.PrometheusWrapper
var err error
if !noProm {
// Create prometheus
prom, err = prometheus.NewPrometheusWrapper()
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ The [Prometheus metrics server](https://prometheus.io/) allows you to [explore t

(* Link goes to the address on your local system. If FRMC is not running, you will see an error)

### Env vars

`FRM_HOST`: The host to the Ficsit Remote Monitoring server. EG: `172.17.0.1`.

`FRM_PORT`: The port of the Ficist Remote Monitoring server. EG: `8080`.

`FRM_HOSTS`: A comma separated list of Ficsit Remote Monitoring servers. If protocol is unspecified, it defaults to http. EG: `http://myserver1.frm.example:8080,myserver2.frm.example:8080,https://myserver3.frm.example:8081`

`FRM_LOG_STDOUT`: If FRMC should print to stdout rather than a separate logfile. Useful for docker/containerization. default false.

## What metrics are available


Expand Down
4 changes: 2 additions & 2 deletions map/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion map/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "map",
"version": "1.3.0",
"version": "1.3.1",
"description": "Realtime map for Ficsit Remote Monitoring Companion",
"author": "Andy Hunt <[email protected]>",
"main": "index.js",
Expand Down
10 changes: 10 additions & 0 deletions readme/readme.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ The [Prometheus metrics server](https://prometheus.io/) allows you to [explore t

(&#42; Link goes to the address on your local system. If FRMC is not running, you will see an error)

### Env vars

`FRM_HOST`: The host to the Ficsit Remote Monitoring server. EG: `172.17.0.1`.

`FRM_PORT`: The port of the Ficist Remote Monitoring server. EG: `8080`.

`FRM_HOSTS`: A comma separated list of Ficsit Remote Monitoring servers. If protocol is unspecified, it defaults to http. EG: `http://myserver1.frm.example:8080,myserver2.frm.example:8080,https://myserver3.frm.example:8081`

`FRM_LOG_STDOUT`: If FRMC should print to stdout rather than a separate logfile. Useful for docker/containerization. default false.

## What metrics are available

{{ template "MetricsTable" . }}
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
1.3.1
Loading