forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package telemetry | ||
|
||
import "gopkg.in/yaml.v3" | ||
|
||
// Config represents the configuration for the telemetry module | ||
type Config struct { | ||
Port uint `yaml:"port"` | ||
} | ||
|
||
// NewConfig allows to build a new Config instance | ||
func NewConfig(port uint) *Config { | ||
return &Config{ | ||
Port: port, | ||
} | ||
} | ||
|
||
// ParseConfig allows to parse a byte array as a Config instance | ||
func ParseConfig(bytes []byte) (*Config, error) { | ||
type T struct { | ||
Telemetry *Config `yaml:"telemetry"` | ||
} | ||
var cfg T | ||
err := yaml.Unmarshal(bytes, &cfg) | ||
return cfg.Telemetry, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
// RunAdditionalOperations runs the module additional operations | ||
func RunAdditionalOperations(cfg *Config) error { | ||
err := checkConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go startPrometheus(cfg) | ||
|
||
return nil | ||
} | ||
|
||
// checkConfig checks if the given config is valid | ||
func checkConfig(cfg *Config) error { | ||
if cfg == nil { | ||
return fmt.Errorf("no telemetry config found") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// startPrometheus starts a Prometheus server using the given configuration | ||
func startPrometheus(cfg *Config) { | ||
router := mux.NewRouter() | ||
router.Handle("/metrics", promhttp.Handler()) | ||
|
||
// Create a new server | ||
server := http.Server{ | ||
Addr: fmt.Sprintf(":%d", cfg.Port), | ||
Handler: router, | ||
ReadTimeout: 5 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
} | ||
|
||
err := server.ListenAndServe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package telemetry | ||
|
||
import ( | ||
"github.com/forbole/juno/v5/modules" | ||
"github.com/forbole/juno/v5/types/config" | ||
) | ||
|
||
const ( | ||
ModuleName = "telemetry" | ||
) | ||
|
||
var ( | ||
_ modules.Module = &Module{} | ||
_ modules.AdditionalOperationsModule = &Module{} | ||
) | ||
|
||
// Module represents the telemetry module | ||
type Module struct { | ||
cfg *Config | ||
} | ||
|
||
// NewModule returns a new Module implementation | ||
func NewModule(cfg config.Config) *Module { | ||
bz, err := cfg.GetBytes() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
telemetryCfg, err := ParseConfig(bz) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return &Module{ | ||
cfg: telemetryCfg, | ||
} | ||
} | ||
|
||
// Name implements modules.Module | ||
func (m *Module) Name() string { | ||
return ModuleName | ||
} | ||
|
||
// RunAdditionalOperations implements modules.AdditionalOperationsModule | ||
func (m *Module) RunAdditionalOperations() error { | ||
return RunAdditionalOperations(m.cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.