Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/geoip] Add GeoIP providers configuration and maxmind factory #33268

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
dd89cf6
feat: add geoipprovider internal package
rogercoll May 28, 2024
18c1d24
add providers to configuration
rogercoll May 28, 2024
ca50fa1
feat: add unmarshal implementation for geoip providers
rogercoll May 29, 2024
49c277b
add provider's mock and configuration load test case
rogercoll May 29, 2024
4450aa8
feat: add list of providers to the geoip processor
rogercoll May 29, 2024
a7381ae
chore: add changelog entry
rogercoll May 30, 2024
1d8dad2
fix format and linter
rogercoll May 30, 2024
48f3d8d
Merge branch 'main' into geoipprocessor_provider
rogercoll May 30, 2024
686b9e7
fix .chloggen/geoipprocessor_provider.yaml
rogercoll May 31, 2024
cb2b2dc
Merge branch 'main' into geoipprocessor_provider
rogercoll Jun 27, 2024
9bbfd46
chore: replace CreateSettings for Settings
rogercoll Jun 27, 2024
81137cc
feat: validate all providers configuration
rogercoll Jun 27, 2024
87b0161
feat: add maxmind provider factory
rogercoll Jun 28, 2024
31ba472
add configuration unmarshal test cases
rogercoll Jun 28, 2024
0cec847
chore: tidy mod files
rogercoll Jun 28, 2024
be9e2df
chore: update changelog to reflect maxmind
rogercoll Jun 28, 2024
74bde08
chore: fix imports order
rogercoll Jun 28, 2024
05d0be9
docs: add configuration section
rogercoll Jun 28, 2024
f20bfc4
chore: add comments to factory methods
rogercoll Jun 28, 2024
f7f9902
docs: reference added attributes
rogercoll Jun 28, 2024
21e80e8
Merge branch 'main' into geoipprocessor_provider
rogercoll Jun 28, 2024
f8c2991
chore: remove additional require mod section
rogercoll Jun 28, 2024
bda01e5
rename providers config key variable
rogercoll Jun 28, 2024
cd5293d
Update processor/geoipprocessor/README.md
rogercoll Jul 2, 2024
e196b02
Merge branch 'main' into geoipprocessor_provider
rogercoll Jul 2, 2024
bb6ba71
chore: remove deprecated test helpers
rogercoll Jul 2, 2024
0bec9c9
Merge branch 'main' into geoipprocessor_provider
rogercoll Jul 2, 2024
e392379
chore: tidy indirect deps
rogercoll Jul 2, 2024
e5fb50a
Merge branch 'main' into geoipprocessor_provider
rogercoll Jul 2, 2024
eec083e
Merge branch 'main' into geoipprocessor_provider
rogercoll Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: validate all providers configuration
  • Loading branch information
rogercoll committed Jun 27, 2024
commit 81137cca9c0c0dffa34650715676803f26893a49
7 changes: 7 additions & 0 deletions processor/geoipprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (cfg *Config) Validate() error {
if len(cfg.Providers) == 0 {
return errors.New("must specify at least one geo IP data provider when using the geoip processor")
}

// validate all provider's configuration
for providerID, providerConfig := range cfg.Providers {
if err := providerConfig.Validate(); err != nil {
return fmt.Errorf("error validating provider %s: %w", providerID, err)
}
}
return nil
}

Expand Down
65 changes: 64 additions & 1 deletion processor/geoipprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package geoipprocessor

import (
"errors"
"path/filepath"
"testing"

Expand All @@ -17,6 +18,7 @@ import (
"go.opentelemetry.io/collector/otelcol/otelcoltest"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestLoadConfig(t *testing.T) {
}
}

func TestLoadInvalidConfig_InvalidProviderKey(t *testing.T) {
func TestLoadConfig_InvalidProviderKey(t *testing.T) {
factories, err := otelcoltest.NopFactories()
require.NoError(t, err)

Expand All @@ -74,3 +76,64 @@ func TestLoadInvalidConfig_InvalidProviderKey(t *testing.T) {

require.Contains(t, err.Error(), "error reading configuration for \"geoip\": invalid provider key: invalidProviderKey")
}

func TestLoadConfig_ValidProviderKey(t *testing.T) {
type dbMockConfig struct {
Database string `mapstructure:"database"`
providerConfigMock
}
baseMockFactory.CreateDefaultConfigF = func() provider.Config {
return &dbMockConfig{providerConfigMock: providerConfigMock{func() error { return nil }}}
}
providerFactories["mock"] = &baseMockFactory

factories, err := otelcoltest.NopFactories()
require.NoError(t, err)

factory := NewFactory()
factories.Processors[metadata.Type] = factory
collectorConfig, err := otelcoltest.LoadConfigAndValidateWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config-mockProvider.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
},
},
},
)

require.NoError(t, err)
actualDbMockConfig := collectorConfig.Processors[component.NewID(metadata.Type)].(*Config).Providers["mock"].(*dbMockConfig)
require.Equal(t, "/tmp/geodata.csv", actualDbMockConfig.Database)
}

func TestLoadConfig_ProviderValidateError(t *testing.T) {
baseMockFactory.CreateDefaultConfigF = func() provider.Config {
sampleConfig := struct {
Database string `mapstructure:"database"`
providerConfigMock
}{
"",
providerConfigMock{func() error { return errors.New("error validating mocked config") }},
}
return &sampleConfig
}
providerFactories["mock"] = &baseMockFactory

factories, err := otelcoltest.NopFactories()
require.NoError(t, err)

factory := NewFactory()
factories.Processors[metadata.Type] = factory
_, err = otelcoltest.LoadConfigAndValidateWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config-mockProvider.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
},
},
},
)

require.Contains(t, err.Error(), "error validating provider mock")
}
12 changes: 12 additions & 0 deletions processor/geoipprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package geoipprocessor

import (
"context"
"fmt"
"testing"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
Expand Down Expand Up @@ -50,3 +52,13 @@ func TestCreateProcessor(t *testing.T) {
assert.NotNil(t, lp)
assert.NoError(t, err)
}

func TestCreateProcessor_ProcessorKeyConfigError(t *testing.T) {
const errorKey string = "error"

factory := NewFactory()
cfg := &Config{Providers: map[string]provider.Config{errorKey: &providerConfigMock{}}}

_, err := factory.CreateMetricsProcessor(context.Background(), processortest.NewNopSettings(), cfg, consumertest.NewNop())
assert.EqualError(t, err, fmt.Sprintf("geoIP provider factory not found for key: %q", errorKey))
}
51 changes: 46 additions & 5 deletions processor/geoipprocessor/geoip_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"

Expand All @@ -22,20 +23,60 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/geoipprocessor/internal/provider"
)

type ProviderMock struct {
type providerConfigMock struct {
ValidateF func() error
}

type providerFactoryMock struct {
CreateDefaultConfigF func() provider.Config
CreateGeoIPProviderF func(context.Context, processor.Settings, provider.Config) (provider.GeoIPProvider, error)
}

type providerMock struct {
LocationF func(context.Context, net.IP) (attribute.Set, error)
}

var _ provider.GeoIPProvider = (*ProviderMock)(nil)
var (
_ provider.GeoIPProvider = (*providerMock)(nil)
_ provider.GeoIPProvider = (*providerMock)(nil)
_ provider.GeoIPProviderFactory = (*providerFactoryMock)(nil)
)

func (cm *providerConfigMock) Validate() error {
return cm.ValidateF()
}

func (fm *providerFactoryMock) CreateDefaultConfig() provider.Config {
return fm.CreateDefaultConfigF()
}

func (fm *providerFactoryMock) CreateGeoIPProvider(ctx context.Context, settings processor.Settings, cfg provider.Config) (provider.GeoIPProvider, error) {
return fm.CreateGeoIPProviderF(ctx, settings, cfg)
}

func (pm *providerMock) Location(ctx context.Context, ip net.IP) (attribute.Set, error) {
return pm.LocationF(ctx, ip)
}

var baseProviderMock = ProviderMock{
var baseMockProvider = providerMock{
LocationF: func(context.Context, net.IP) (attribute.Set, error) {
return attribute.Set{}, nil
},
}

func (pm *ProviderMock) Location(ctx context.Context, ip net.IP) (attribute.Set, error) {
return pm.LocationF(ctx, ip)
var baseMockFactory = providerFactoryMock{
CreateDefaultConfigF: func() provider.Config {
return &providerConfigMock{ValidateF: func() error { return nil }}
},
CreateGeoIPProviderF: func(context.Context, processor.Settings, provider.Config) (provider.GeoIPProvider, error) {
return &baseMockProvider, nil
},
}

var baseProviderMock = providerMock{
LocationF: func(context.Context, net.IP) (attribute.Set, error) {
return attribute.Set{}, nil
},
}

type generateResourceFunc func(res pcommon.Resource)
Expand Down
128 changes: 0 additions & 128 deletions processor/geoipprocessor/provider_test.go

This file was deleted.

Loading