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

Add exprfilterprocessor skeleton #1844

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions processor/exprfilterprocessor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"go.opentelemetry.io/collector/config/configmodels"
)

type Config struct {
configmodels.ProcessorSettings `mapstructure:",squash"`
}
50 changes: 50 additions & 0 deletions processor/exprfilterprocessor/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/configtest"
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.ExampleComponents()
require.NoError(t, err)

factory := NewFactory()
cfgType := configmodels.Type("exprfilter")
factories.Processors[cfgType] = factory

cfgPath := path.Join("testdata", "config.yaml")
cfg, err := configtest.LoadConfigFile(t, cfgPath, factories)
require.NoError(t, err)
require.NotNil(t, cfg)

procCfg := cfg.Processors["exprfilter"]
require.NotNil(t, procCfg)

flProcCfg := procCfg.(*Config)
require.NotNil(t, flProcCfg)

assert.Equal(t, cfgType, flProcCfg.TypeVal)
assert.Equal(t, "exprfilter", flProcCfg.NameVal)
}
62 changes: 62 additions & 0 deletions processor/exprfilterprocessor/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor/processorhelper"
)

const (
typeStr = "exprfilter"
)

func NewFactory() component.ProcessorFactory {
return processorhelper.NewFactory(
typeStr,
createDefaultConfig,
processorhelper.WithMetrics(createProcessor),
)
}

func createDefaultConfig() configmodels.Processor {
return &Config{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
}
}

func createProcessor(
_ context.Context,
_ component.ProcessorCreateParams,
cfg configmodels.Processor,
nextConsumer consumer.MetricsConsumer,
) (component.MetricsProcessor, error) {
proc := newProcessor(cfg.(*Config))
return processorhelper.NewMetricsProcessor(
cfg,
nextConsumer,
proc,
processorhelper.WithCapabilities(
component.ProcessorCapabilities{MutatesConsumedData: false},
),
)
}
70 changes: 70 additions & 0 deletions processor/exprfilterprocessor/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcheck"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/exporter/exportertest"
)

func TestType(t *testing.T) {
factory := NewFactory()
typ := factory.Type()
require.Equal(t, typ, configmodels.Type("exprfilter"))
}

func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.Equal(t, cfg, &Config{
ProcessorSettings: configmodels.ProcessorSettings{
NameVal: typeStr,
TypeVal: typeStr,
},
})
assert.NoError(t, configcheck.ValidateConfig(cfg))
}

func TestCreateProcessor(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
ctx := context.Background()
params := component.ProcessorCreateParams{Logger: zap.NewNop()}
tp, err := factory.CreateTraceProcessor(
ctx,
params,
cfg,
exportertest.NewNopTraceExporter(),
)
assert.Nil(t, tp)
assert.Error(t, err)

lp, err := factory.CreateLogsProcessor(ctx, params, cfg, exportertest.NewNopLogsExporter())
assert.Nil(t, lp)
assert.Error(t, err)

mp, err := factory.CreateMetricsProcessor(ctx, params, cfg, exportertest.NewNopMetricsExporter())
assert.NotNil(t, mp)
assert.NoError(t, err)
}
36 changes: 36 additions & 0 deletions processor/exprfilterprocessor/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"context"

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/processor/processorhelper"
)

type processor struct {
cfg *Config
}

var _ processorhelper.MProcessor = (*processor)(nil)

func newProcessor(cfg *Config) *processor {
return &processor{cfg: cfg}
}

func (p *processor) ProcessMetrics(ctx context.Context, metrics pdata.Metrics) (pdata.Metrics, error) {
return metrics, nil
}
31 changes: 31 additions & 0 deletions processor/exprfilterprocessor/processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exprfilterprocessor

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/consumer/pdata"
)

func TestProcessor(t *testing.T) {
proc := newProcessor(&Config{})
in := pdata.NewMetrics()
out, _ := proc.ProcessMetrics(context.Background(), in)
require.Equal(t, in, out)
}
15 changes: 15 additions & 0 deletions processor/exprfilterprocessor/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
receivers:
examplereceiver:

processors:
exprfilter:

exporters:
exampleexporter:

service:
pipelines:
metrics:
receivers: [examplereceiver]
processors: [exprfilter]
exporters: [exampleexporter]