From be2c91ee2d87232b1f3c6a43fcbd53b45a03d25c Mon Sep 17 00:00:00 2001 From: BominRahmani Date: Wed, 5 Jun 2024 15:40:55 -0400 Subject: [PATCH] progress --- .../exporter/catchpoint/catchpoint.go | 44 ++++++------ .../exporter/catchpoint/catchpoint_test.go | 8 +-- .../catchpoint_exporter.go | 69 +++++++++++++++++++ .../catchpoint_exporter_test.go | 64 +++++++++++++++++ 4 files changed, 157 insertions(+), 28 deletions(-) create mode 100644 static/integrations/catchpoint_exporter/catchpoint_exporter.go create mode 100644 static/integrations/catchpoint_exporter/catchpoint_exporter_test.go diff --git a/internal/component/prometheus/exporter/catchpoint/catchpoint.go b/internal/component/prometheus/exporter/catchpoint/catchpoint.go index a230a35f427d..c180f8e84c8f 100644 --- a/internal/component/prometheus/exporter/catchpoint/catchpoint.go +++ b/internal/component/prometheus/exporter/catchpoint/catchpoint.go @@ -1,41 +1,40 @@ package catchpoint import ( - "github.com/grafana/agent/component" - "github.com/grafana/agent/component/prometheus/exporter" - "github.com/grafana/agent/pkg/integrations" - "github.com/grafana/agent/pkg/integrations/catchpoint_exporter" - "github.com/grafana/agent/pkg/river/rivertypes" - config_util "github.com/prometheus/common/config" + "github.com/grafana/agent/internal/component" + "github.com/grafana/agent/internal/component/prometheus/exporter" + "github.com/grafana/agent/internal/featuregate" + "github.com/grafana/agent/static/integrations" + "github.com/grafana/agent/static/integrations/catchpoint_exporter" ) func init() { component.Register(component.Registration{ - Name: "prometheus.exporter.catchpoint", - Args: Arguments{}, - Exports: exporter.Exports{}, - Build: exporter.New(createExporter, "catchpoint"), + Name: "prometheus.exporter.catchpoint", + Stability: featuregate.StabilityStable, + Args: Arguments{}, + Exports: exporter.Exports{}, + Build: exporter.New(createExporter, "catchpoint"), }) } -func createExporter(opts component.Options, args component.Arguments) (integrations.Integration, error) { +func createExporter(opts component.Options, args component.Arguments, defaultInstanceKey string) (integrations.Integration, string, error) { a := args.(Arguments) - return a.Convert().NewIntegration(opts.Logger) + return integrations.NewIntegrationWithInstanceKey(opts.Logger, a.Convert(), defaultInstanceKey) } // DefaultArguments holds the default settings for the catchpoint exporter var DefaultArguments = Arguments{ - Verbose: false, - WebhookPath: "/catchpoint-webhook", - Port: "9090", + Verbose: false, + WebhookPath: "/catchpoint-webhook", + Port: "9090", } // Arguments controls the catchpoint exporter. type Arguments struct { - Verbose bool `river:"verbose,attr"` - WebhookPath string `river:"webhookpath,attr"` - Port string `river:"port,attr"` - + Verbose bool `river:"verbose,attr"` + WebhookPath string `river:"webhookpath,attr"` + Port string `river:"port,attr"` } // UnmarshalRiver implements River unmarshalling for Arguments. @@ -48,9 +47,8 @@ func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error { func (a *Arguments) Convert() *catchpoint_exporter.Config { return &catchpoint_exporter.Config{ - Verbose: a.Verbose, - WebhookPath: a.WebhookPath, - Port: a.Port, + Verbose: a.Verbose, + WebhookPath: a.WebhookPath, + Port: a.Port, } } - diff --git a/internal/component/prometheus/exporter/catchpoint/catchpoint_test.go b/internal/component/prometheus/exporter/catchpoint/catchpoint_test.go index 4a05a76969c2..56bd78e34093 100644 --- a/internal/component/prometheus/exporter/catchpoint/catchpoint_test.go +++ b/internal/component/prometheus/exporter/catchpoint/catchpoint_test.go @@ -3,16 +3,14 @@ package catchpoint import ( "testing" - "github.com/grafana/agent/pkg/integrations/catchpoint_exporter" - "github.com/grafana/agent/pkg/river" - "github.com/grafana/agent/pkg/river/rivertypes" - config_util "github.com/prometheus/common/config" + "github.com/grafana/agent/static/integrations/catchpoint_exporter" + "github.com/grafana/river" "github.com/stretchr/testify/require" ) func TestRiverUnmarshal(t *testing.T) { riverConfig := ` - port = "3030" + port = "3030" verbose = true webhookpath = "/nondefault-webhook-path" ` diff --git a/static/integrations/catchpoint_exporter/catchpoint_exporter.go b/static/integrations/catchpoint_exporter/catchpoint_exporter.go new file mode 100644 index 000000000000..b51578b6fcd3 --- /dev/null +++ b/static/integrations/catchpoint_exporter/catchpoint_exporter.go @@ -0,0 +1,69 @@ +package catchpoint_exporter + +import ( + "github.com/go-kit/log" + "github.com/grafana/agent/static/integrations" + integrations_v2 "github.com/grafana/agent/static/integrations/v2" + "github.com/grafana/agent/static/integrations/v2/metricsutils" + collector "github.com/grafana/catchpoint-prometheus-exporter" +) + +// DefaultConfig is the default config for the snowflake integration +var DefaultConfig = Config{ + Verbose: false, + WebhookPath: "/catchpoint-webhook", + Port: "9090", +} + +// Config is the configuration for the snowflake integration +type Config struct { + Verbose bool `yaml:"verbose,omitempty"` + WebhookPath string `yaml:"webhookpath,omitempty"` + Port string `yaml:"port,omitempty"` +} + +func (c *Config) exporterConfig() *collector.Config { + return &collector.Config{ + Verbose: c.Verbose, + WebhookPath: c.WebhookPath, + Port: string(c.Port), + } +} + +// Identifier returns a string that identifies the integration. +func (c *Config) InstanceKey(agentKey string) (string, error) { + return c.Port, nil +} + +// UnmarshalYAML implements yaml.Unmarshaler for Config +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { + *c = DefaultConfig + + type plain Config + return unmarshal((*plain)(c)) +} + +// Name returns the name of the integration this config is for. +func (c *Config) Name() string { + return "catchpoint" +} + +func init() { + integrations.RegisterIntegration(&Config{}) + integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("catchpoint")) +} + +// NewIntegration creates a new integration from the config. +func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) { + exporterConfig := c.exporterConfig() + + if err := exporterConfig.Validate(); err != nil { + return nil, err + } + + col := collector.NewCollector(l, exporterConfig) + return integrations.NewCollectorIntegration( + c.Name(), + integrations.WithCollectors(col), + ), nil +} diff --git a/static/integrations/catchpoint_exporter/catchpoint_exporter_test.go b/static/integrations/catchpoint_exporter/catchpoint_exporter_test.go new file mode 100644 index 000000000000..676c4df0b316 --- /dev/null +++ b/static/integrations/catchpoint_exporter/catchpoint_exporter_test.go @@ -0,0 +1,64 @@ +package catchpoint_exporter + +import ( + "os" + "testing" + + "github.com/go-kit/log" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" +) + +func TestConfig_UnmarshalYaml(t *testing.T) { + strConfig := ` + port = "3030" + verbose = true + webhookpath = "/nondefault-webhook-path" + ` + + var c Config + + require.NoError(t, yaml.UnmarshalStrict([]byte(strConfig), &c)) + + require.Equal(t, Config{ + Verbose: true, + Port: "3030", + WebhookPath: "/nondefault-webhook-path", + }, c) +} + +func TestConfig_NewIntegration(t *testing.T) { + t.Run("integration with valid config", func(t *testing.T) { + c := &Config{ + Verbose: true, + Port: "3030", + WebhookPath: "/nondefault-webhook-path", + } + + i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) + require.NoError(t, err) + require.NotNil(t, i) + }) + + t.Run("integration with invalid config", func(t *testing.T) { + c := &Config{ + Verbose: "incorrect_value", + Port: "3030", + WebhookPath: "/nondefault-webhook-path", + } + + i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) + require.Nil(t, i) + require.ErrorContains(t, err, "") + }) +} + +func TestConfig_AgentKey(t *testing.T) { + c := DefaultConfig + c.Port = "3030" + + ik := "agent-key" + id, err := c.InstanceKey(ik) + require.NoError(t, err) + require.Equal(t, "localhost:3030", id) +}