Skip to content

Commit

Permalink
chore: Add example pkgs
Browse files Browse the repository at this point in the history
* Demonstrates how the current package can be extended with third party packages

Signed-off-by: Mahendra Paipuri <[email protected]>
  • Loading branch information
mahendrapaipuri committed Dec 25, 2023
1 parent cae81df commit 8425162
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/mock_exporter/cmd/mock_exporter/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
54 changes: 54 additions & 0 deletions examples/mock_exporter/pkg/collector/mock_metric.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions examples/mock_scheduler/cmd/mock_scheduler/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
52 changes: 52 additions & 0 deletions examples/mock_scheduler/pkg/scheduler/mock_scheduler.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/emissions/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8425162

Please sign in to comment.