Skip to content

Commit

Permalink
Add logs scraper in factory
Browse files Browse the repository at this point in the history
  • Loading branch information
sincejune committed Jan 17, 2025
1 parent e970f8b commit ab028c9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .chloggen/add-logs-scraper-in-factory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: scraper

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add logs scraper in scraper factory

# One or more tracking issues or pull requests related to the change
issues: [11822]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
34 changes: 33 additions & 1 deletion scraper/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ type Factory interface {
// CreateMetrics creates a Metrics scraper based on this config.
// If the scraper type does not support metrics,
// this function returns the error [pipeline.ErrSignalNotSupported].
// Implementers can assume `next` is never nil.
CreateMetrics(ctx context.Context, set Settings, cfg component.Config) (Metrics, error)

// CreateLogs creates a Logs scraper based on this config.
// If the scraper type does not support logs,
// this function returns the error [pipeline.ErrSignalNotSupported].
CreateLogs(ctx context.Context, set Settings, cfg component.Config) (Logs, error)

// MetricsStability gets the stability level of the Metrics scraper.
MetricsStability() component.StabilityLevel

// LogsStability gets the stability level of the Logs scraper.
LogsStability() component.StabilityLevel

unexportedFactoryFunc()
}

Expand All @@ -60,7 +67,9 @@ type factory struct {
cfgType component.Type
component.CreateDefaultConfigFunc
CreateMetricsFunc
CreateLogsFunc
metricsStabilityLevel component.StabilityLevel
logsStabilityLevel component.StabilityLevel
}

func (f *factory) Type() component.Type {
Expand All @@ -73,9 +82,16 @@ func (f *factory) MetricsStability() component.StabilityLevel {
return f.metricsStabilityLevel
}

func (f *factory) LogsStability() component.StabilityLevel {
return f.logsStabilityLevel
}

// CreateMetricsFunc is the equivalent of Factory.CreateMetrics().
type CreateMetricsFunc func(context.Context, Settings, component.Config) (Metrics, error)

// CreateLogsFunc is the equivalent of Factory.CreateLogs().
type CreateLogsFunc func(context.Context, Settings, component.Config) (Logs, error)

// CreateMetrics implements Factory.CreateMetrics.
func (f CreateMetricsFunc) CreateMetrics(ctx context.Context, set Settings, cfg component.Config) (Metrics, error) {
if f == nil {
Expand All @@ -84,6 +100,14 @@ func (f CreateMetricsFunc) CreateMetrics(ctx context.Context, set Settings, cfg
return f(ctx, set, cfg)
}

// CreateLogs implements Factory.CreateLogs.
func (f CreateLogsFunc) CreateLogs(ctx context.Context, set Settings, cfg component.Config) (Logs, error) {
if f == nil {
return nil, pipeline.ErrSignalNotSupported
}
return f(ctx, set, cfg)
}

// WithMetrics overrides the default "error not supported" implementation for CreateMetrics and the default "undefined" stability level.
func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
Expand All @@ -92,6 +116,14 @@ func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) F
})
}

// WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.logsStabilityLevel = sl
o.CreateLogsFunc = createLogs
})
}

// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
f := &factory{
Expand Down
12 changes: 11 additions & 1 deletion scraper/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func TestNewFactory(t *testing.T) {
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())
_, err := f.CreateMetrics(context.Background(), nopSettings(), &defaultCfg)
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateLogs(context.Background(), nopSettings(), &defaultCfg)
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
}

func TestNewFactoryWithOptions(t *testing.T) {
Expand All @@ -41,13 +43,17 @@ func TestNewFactoryWithOptions(t *testing.T) {
f := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithMetrics(createMetrics, component.StabilityLevelAlpha))
WithMetrics(createMetrics, component.StabilityLevelAlpha),
WithLogs(createLogs, component.StabilityLevelAlpha))
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())

assert.Equal(t, component.StabilityLevelAlpha, f.MetricsStability())
assert.Equal(t, component.StabilityLevelAlpha, f.LogsStability())
_, err := f.CreateMetrics(context.Background(), Settings{}, &defaultCfg)
require.NoError(t, err)
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg)
require.NoError(t, err)
}

func TestMakeFactoryMap(t *testing.T) {
Expand Down Expand Up @@ -90,3 +96,7 @@ func TestMakeFactoryMap(t *testing.T) {
func createMetrics(context.Context, Settings, component.Config) (Metrics, error) {
return NewMetrics(newTestScrapeMetricsFunc(nil))
}

func createLogs(context.Context, Settings, component.Config) (Logs, error) {
return NewLogs(newTestScrapeLogsFunc(nil))
}

0 comments on commit ab028c9

Please sign in to comment.