Skip to content

Commit

Permalink
Add factory and config for attributes processor (#152)
Browse files Browse the repository at this point in the history
* Add factory and config for attributes processor

* Format: remove KeyReplacement

* Refactore: config format

* Refactore:attribute-key and Keys
  • Loading branch information
MohamedElqdusy authored and Paulo Janotti committed Jul 17, 2019
1 parent 8905c13 commit 83e99c2
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
39 changes: 39 additions & 0 deletions processor/attributekeyprocessor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2019, 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 attributekeyprocessor

import (
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
)

// Config defines configuration for Attribute Key processor.
type Config struct {
configmodels.ProcessorSettings `mapstructure:",squash"`
// map key is the attribute key to be replaced.
Keys map[string]NewKeyProperties `mapstructure:"keys"`
}

// NewKeyProperties defines the key's replacments properties.
type NewKeyProperties struct {
// NewKey is the value that will be used as the new key for the attribute.
NewKey string `mapstructure:"replacement"`
// Overwrite is set to true to indicate that the replacement should be
// performed even if the new key already exists on the attributes.
// In this case the original value associated with the new key is lost.
Overwrite bool `mapstructure:"overwrite"`
// KeepOriginal is set to true to indicate that the original key
// should not be removed from the attributes.
KeepOriginal bool `mapstructure:"keep"`
}
79 changes: 79 additions & 0 deletions processor/attributekeyprocessor/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2019, 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 attributekeyprocessor

import (
"path"
"testing"

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

"github.com/open-telemetry/opentelemetry-service/config"
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
"github.com/open-telemetry/opentelemetry-service/processor"
)

var _ = config.RegisterTestFactories()

func TestLoadConfig(t *testing.T) {

config, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"))

require.Nil(t, err)
require.NotNil(t, config)

p0 := config.Processors["attribute-key"]
assert.Equal(t, p0,
&Config{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: "attribute-key",
NameVal: "attribute-key",
},
Keys: map[string]NewKeyProperties{
"foo": {
NewKey: "boo",
Overwrite: true,
KeepOriginal: true,
},
"kie": {
NewKey: "goo",
Overwrite: true,
KeepOriginal: false,
},
"ddd": {
NewKey: "vss",
Overwrite: false,
KeepOriginal: true,
},
"dot.test": {
NewKey: "bot",
Overwrite: false,
KeepOriginal: false,
},
},
})
}

func TestLoadConfigEmpty(t *testing.T) {
factory := processor.GetFactory(typeStr)

config, err := config.LoadConfigFile(t, path.Join(".", "testdata", "empty.yaml"))

require.Nil(t, err)
require.NotNil(t, config)
p0 := config.Processors["attribute-key"]
assert.Equal(t, p0, factory.CreateDefaultConfig())
}
79 changes: 79 additions & 0 deletions processor/attributekeyprocessor/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2019, 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 attributekeyprocessor

import (
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-service/config/configerror"
"github.com/open-telemetry/opentelemetry-service/config/configmodels"
"github.com/open-telemetry/opentelemetry-service/consumer"
"github.com/open-telemetry/opentelemetry-service/processor"
)

var _ = processor.RegisterFactory(&factory{})

const (
// The value of "type" Attribute Key in configuration.
typeStr = "attribute-key"
)

// factory is the factory for Attribute Key processor.
type factory struct {
}

// Type gets the type of the config created by this factory.
func (f *factory) Type() string {
return typeStr
}

// CreateDefaultConfig creates the default configuration for processor.
func (f *factory) CreateDefaultConfig() configmodels.Processor {
return &Config{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
Keys: make(map[string]NewKeyProperties, 0),
}
}

// CreateTraceProcessor creates a trace processor based on this config.
func (f *factory) CreateTraceProcessor(
logger *zap.Logger,
nextConsumer consumer.TraceConsumer,
cfg configmodels.Processor,
) (processor.TraceProcessor, error) {
oCfg := cfg.(*Config)
return NewTraceProcessor(nextConsumer, convertToKeyReplacements(&oCfg.Keys)...)
}

// CreateMetricsProcessor creates a metrics processor based on this config.
func (f *factory) CreateMetricsProcessor(
logger *zap.Logger,
nextConsumer consumer.MetricsConsumer,
cfg configmodels.Processor,
) (processor.MetricsProcessor, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}

// convert keys' "map" to KeyReplacement
func convertToKeyReplacements(keyMap *map[string]NewKeyProperties) []KeyReplacement {
var replacements []KeyReplacement
for key, val := range *keyMap {
replacements = append(replacements, KeyReplacement{Key: key, NewKey: val.NewKey, Overwrite: val.Overwrite, KeepOriginal: val.KeepOriginal})
}
return replacements
}
50 changes: 50 additions & 0 deletions processor/attributekeyprocessor/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019, 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 attributekeyprocessor

import (
"testing"

"go.uber.org/zap"

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

"github.com/open-telemetry/opentelemetry-service/exporter/exportertest"
"github.com/open-telemetry/opentelemetry-service/processor"
)

func TestCreateDefaultConfig(t *testing.T) {
factory := processor.GetFactory(typeStr)
require.NotNil(t, factory)

cfg := factory.CreateDefaultConfig()
assert.NotNil(t, cfg, "failed to create default config")
}

func TestCreateProcessor(t *testing.T) {
factory := processor.GetFactory(typeStr)
require.NotNil(t, factory)

cfg := factory.CreateDefaultConfig()

tp, err := factory.CreateTraceProcessor(zap.NewNop(), exportertest.NewNopTraceExporter(), cfg)
assert.NotNil(t, tp)
assert.NoError(t, err, "cannot create trace processor")

mp, err := factory.CreateMetricsProcessor(zap.NewNop(), nil, cfg)
assert.Nil(t, mp)
assert.Error(t, err, "should not be able to create metric processor")
}
30 changes: 30 additions & 0 deletions processor/attributekeyprocessor/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
receivers:
examplereceiver:

processors:
attribute-key:
keys:
foo:
replacement: "boo"
overwrite: true
keep: true
kie:
replacement: "goo"
overwrite: true
keep: false
ddd:
replacement: "vss"
overwrite: false
keep: true
dot.test:
replacement: "bot"
overwrite: false
keep: false
exporters:
exampleexporter:

pipelines:
traces:
receivers: [examplereceiver]
processors: [attribute-key]
exporters: [exampleexporter]
15 changes: 15 additions & 0 deletions processor/attributekeyprocessor/testdata/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
receivers:
examplereceiver:

processors:
attribute-key:


exporters:
exampleexporter:

pipelines:
traces:
receivers: [examplereceiver]
processors: [attribute-key]
exporters: [exampleexporter]

0 comments on commit 83e99c2

Please sign in to comment.