Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
BominRahmani committed Jun 5, 2024
1 parent 92f8ce8 commit be2c91e
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 28 deletions.
44 changes: 21 additions & 23 deletions internal/component/prometheus/exporter/catchpoint/catchpoint.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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"
`
Expand Down
69 changes: 69 additions & 0 deletions static/integrations/catchpoint_exporter/catchpoint_exporter.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit be2c91e

Please sign in to comment.