From 8425162d6cf66499ba3fe6cac4d4ef60b917362d Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Mon, 25 Dec 2023 18:08:48 +0100 Subject: [PATCH] chore: Add example pkgs * Demonstrates how the current package can be extended with third party packages Signed-off-by: Mahendra Paipuri --- .../mock_exporter/cmd/mock_exporter/main.go | 21 ++++++++ .../pkg/collector/mock_metric.go | 54 +++++++++++++++++++ .../mock_scheduler/cmd/mock_scheduler/main.go | 21 ++++++++ .../pkg/scheduler/mock_scheduler.go | 52 ++++++++++++++++++ pkg/emissions/types.go | 2 +- 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 examples/mock_exporter/cmd/mock_exporter/main.go create mode 100644 examples/mock_exporter/pkg/collector/mock_metric.go create mode 100644 examples/mock_scheduler/cmd/mock_scheduler/main.go create mode 100644 examples/mock_scheduler/pkg/scheduler/mock_scheduler.go diff --git a/examples/mock_exporter/cmd/mock_exporter/main.go b/examples/mock_exporter/cmd/mock_exporter/main.go new file mode 100644 index 00000000..a786a566 --- /dev/null +++ b/examples/mock_exporter/cmd/mock_exporter/main.go @@ -0,0 +1,21 @@ +// Boiler plate code to create a new instance of BatchJobExporter entrypoint + +package main + +// Ensure to import the current mock collector +import ( + _ "github.com/mahendrapaipuri/batchjob_monitoring/examples/mock_exporter/pkg/collector" + batchjob_collector "github.com/mahendrapaipuri/batchjob_monitoring/pkg/collector" +) + +// Main entry point for `batchjob_exporter` app +func main() { + // Create a new app + batchJobExporterApp, err := batchjob_collector.NewBatchJobExporter() + if err != nil { + panic("Failed to create an instance of BatchJobExporter App") + } + + // Main entrypoint of the app + batchJobExporterApp.Main() +} diff --git a/examples/mock_exporter/pkg/collector/mock_metric.go b/examples/mock_exporter/pkg/collector/mock_metric.go new file mode 100644 index 00000000..a338c489 --- /dev/null +++ b/examples/mock_exporter/pkg/collector/mock_metric.go @@ -0,0 +1,54 @@ +package collector + +import ( + "math/rand" + + "github.com/go-kit/log" + "github.com/mahendrapaipuri/batchjob_monitoring/pkg/collector" + "github.com/prometheus/client_golang/prometheus" +) + +// A mock collector that exports a random value between 0 and configurable maximum value +const mockCollectorSubsystem = "mock" + +// Define mock collector struct +type mockCollector struct { + logger log.Logger + mockMetricDesc *prometheus.Desc +} + +// Define vars and flags necessary to configure collector +var ( + maxRandInt = collector.BatchJobExporterApp.Flag( + "collector.mock.max", + "Maximum value of the mock metric.", + ).Default("100").Int() +) + +// Register mock collector +func init() { + collector.RegisterCollector(mockCollectorSubsystem, true, NewMockCollector) +} + +// NewMockCollector returns a new Collector exposing mock metrics. +func NewMockCollector(logger log.Logger) (collector.Collector, error) { + // Define mock metric description + mockMetricDesc := prometheus.NewDesc( + prometheus.BuildFQName(collector.Namespace, mockCollectorSubsystem, "mockunit_total"), + "Current mock metric", []string{}, nil, + ) + + // Create a new mockCollector struct + collector := mockCollector{ + logger: logger, + mockMetricDesc: mockMetricDesc, + } + return &collector, nil +} + +// Update implements Collector and exposes mock metrics. +func (c *mockCollector) Update(ch chan<- prometheus.Metric) error { + // Return a random value + ch <- prometheus.MustNewConstMetric(c.mockMetricDesc, prometheus.CounterValue, float64(rand.Intn(*maxRandInt))) + return nil +} diff --git a/examples/mock_scheduler/cmd/mock_scheduler/main.go b/examples/mock_scheduler/cmd/mock_scheduler/main.go new file mode 100644 index 00000000..6ffa4ea4 --- /dev/null +++ b/examples/mock_scheduler/cmd/mock_scheduler/main.go @@ -0,0 +1,21 @@ +// Boiler plate code to create a new instance of BatchJobStatsServer entrypoint + +package main + +// Ensure to import the current mock scheduler +import ( + _ "github.com/mahendrapaipuri/batchjob_monitoring/examples/mock_scheduler/pkg/scheduler" + batchjob_stats "github.com/mahendrapaipuri/batchjob_monitoring/pkg/jobstats" +) + +// Main entry point for `batchjob_stats_server` app +func main() { + // Create a new app + batchJobStatsServerApp, err := batchjob_stats.NewBatchJobStatsServer() + if err != nil { + panic("Failed to create an instance of Batch Job Stats Server App") + } + + // Main entrypoint of the app + batchJobStatsServerApp.Main() +} diff --git a/examples/mock_scheduler/pkg/scheduler/mock_scheduler.go b/examples/mock_scheduler/pkg/scheduler/mock_scheduler.go new file mode 100644 index 00000000..f8487888 --- /dev/null +++ b/examples/mock_scheduler/pkg/scheduler/mock_scheduler.go @@ -0,0 +1,52 @@ +package scheduler + +import ( + "time" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "github.com/mahendrapaipuri/batchjob_monitoring/pkg/jobstats" +) + +type mockScheduler struct { + logger log.Logger +} + +const mockBatchScheduler = "mock" + +var ( + slurmUserUid int + slurmUserGid int + macctPath = jobstats.BatchJobStatsServerApp.Flag( + "mock.acct.path", + "Absolute path to macct executable.", + ).Default("/usr/local/bin/macct").String() +) + +func init() { + // Register batch scheduler with jobstats pkg + jobstats.RegisterBatch(mockBatchScheduler, true, NewMockScheduler) +} + +// Do all basic checks here +func preflightChecks(logger log.Logger) error { + return nil +} + +// NewMockScheduler returns a new MockScheduler that returns batch job stats +func NewMockScheduler(logger log.Logger) (jobstats.Batch, error) { + err := preflightChecks(logger) + if err != nil { + level.Error(logger).Log("msg", "Failed to create mock batch scheduler for retreiving jobs.", "err", err) + return nil, err + } + level.Info(logger).Log("msg", "Jobs from mock batch scheduler will be retrieved.") + return &mockScheduler{ + logger: logger, + }, nil +} + +// Add the logic here to get jobs from batch scheduler and return slice of BatchJob structs +func (s *mockScheduler) GetJobs(start time.Time, end time.Time) ([]jobstats.BatchJob, error) { + return []jobstats.BatchJob{{Jobid: "1000"}, {Jobid: "1100"}}, nil +} diff --git a/pkg/emissions/types.go b/pkg/emissions/types.go index 0d548de7..68e835bf 100644 --- a/pkg/emissions/types.go +++ b/pkg/emissions/types.go @@ -152,7 +152,7 @@ type Source interface { Update() (float64, error) } -// NewEmissionSources implements the interface to collect +// EmissionSources implements the interface to collect // emission factors from different sources. type EmissionSources struct { Sources map[string]Source