Skip to content

Commit 0a79d9e

Browse files
committed
feat: add receiver config and factory go
Signed-off-by: Steve Freed <[email protected]>
1 parent 479717d commit 0a79d9e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

receiver/redfishreceiver/config.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package redfishreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redfishreceiver"
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"go.opentelemetry.io/collector/config/configopaque"
8+
"go.opentelemetry.io/collector/scraper/scraperhelper"
9+
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redfishreceiver/internal/metadata"
11+
)
12+
13+
type redfishConfig struct {
14+
Version string `mapstructure:"version"`
15+
}
16+
17+
type Server struct {
18+
Host string `mapstructure:"host"`
19+
User string `mapstructure:"username"`
20+
Pwd configopaque.String `mapstructure:"password"`
21+
Insecure bool `mapstructure:"insecure"`
22+
Timeout string `mapstructure:"timeout"`
23+
Redfish redfishConfig `mapstructure:"redfish"`
24+
ComputerSystemId string `mapstructure:"computer_system_id"`
25+
Resources []Resource `mapstructure:"resources"`
26+
}
27+
28+
type Config struct {
29+
scraperhelper.ControllerConfig `mapstructure:",squash"`
30+
metadata.MetricsBuilderConfig `mapstructure:",squash"`
31+
Servers []Server `mapstructure:"servers"`
32+
33+
// prevent unkeyed literal initialization
34+
_ struct{}
35+
}
36+
37+
func (cfg *Config) Validate() error {
38+
if len(cfg.Servers) == 0 {
39+
return fmt.Errorf("servers must not be empty")
40+
}
41+
42+
for _, server := range cfg.Servers {
43+
if server.Redfish.Version != "v1" {
44+
return fmt.Errorf("redfish version must be once of the following values: 'v1'")
45+
}
46+
if len(server.Resources) == 0 {
47+
return fmt.Errorf("resources must not be empty")
48+
}
49+
if _, err := time.ParseDuration(server.Timeout); err != nil && server.Timeout != "" {
50+
return fmt.Errorf("invalid server timeout")
51+
}
52+
}
53+
return nil
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package redfishreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redfishreceiver"
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"go.opentelemetry.io/collector/component"
9+
"go.opentelemetry.io/collector/consumer"
10+
"go.opentelemetry.io/collector/receiver"
11+
"go.opentelemetry.io/collector/scraper"
12+
"go.opentelemetry.io/collector/scraper/scraperhelper"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redfishreceiver/internal/metadata"
15+
)
16+
17+
var typeStr = component.MustNewType("redfishreceiver")
18+
19+
func createDefaultConfig() component.Config {
20+
cfg := scraperhelper.NewDefaultControllerConfig()
21+
cfg.CollectionInterval = 60 * time.Second
22+
23+
return &Config{
24+
ControllerConfig: cfg,
25+
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
26+
Servers: make([]Server, 0),
27+
}
28+
}
29+
30+
func createMetricsReceiver(_ context.Context, params receiver.Settings, baseCfg component.Config, consumer consumer.Metrics) (receiver.Metrics, error) {
31+
cfg, ok := baseCfg.(*Config)
32+
if !ok {
33+
return nil, fmt.Errorf("invalid redfishreceiver config")
34+
}
35+
36+
redfishScraper := newScraper(cfg, params)
37+
s, err := scraper.NewMetrics(redfishScraper.scrape, scraper.WithStart(redfishScraper.start))
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
return scraperhelper.NewMetricsController(&cfg.ControllerConfig, params, consumer, scraperhelper.AddScraper(metadata.Type, s))
43+
}
44+
45+
// NewFactory creates a factory for redfish receiver.
46+
func NewFactory() receiver.Factory {
47+
return receiver.NewFactory(
48+
typeStr,
49+
createDefaultConfig,
50+
receiver.WithMetrics(createMetricsReceiver, component.StabilityLevelAlpha))
51+
}

0 commit comments

Comments
 (0)