-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add factory and config for attributes processor (#152)
* Add factory and config for attributes processor * Format: remove KeyReplacement * Refactore: config format * Refactore:attribute-key and Keys
- Loading branch information
1 parent
8905c13
commit 83e99c2
Showing
6 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |