This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add span attribute key mapping support (#9) Many libraries and services didn't implement consistent naming for their span attribute keys, this feature provides the capability of making the attribute keys consistent for different libraries and services without the need to change their code. * PR Feedback * PR feedback * type on test Fix nil return and avoid lookup if overwritting Better syntax to avoid lookup when overwritting Support same replacement for multiple keys * Simplify handling of attribute related config
- Loading branch information
Paulo Janotti
authored
May 9, 2019
1 parent
5228f66
commit 5fb7397
Showing
9 changed files
with
684 additions
and
12 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
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
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,89 @@ | ||
// Copyright 2019, OpenCensus 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 builder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/census-instrumentation/opencensus-service/processor/attributekeyprocessor" | ||
) | ||
|
||
func TestGlobalProcessorCfg_InitFromViper(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
file string | ||
want *AttributesCfg | ||
}{ | ||
{ | ||
name: "key_mapping", | ||
file: "./testdata/global_attributes_key_mapping.yaml", | ||
want: &AttributesCfg{ | ||
KeyReplacements: []attributekeyprocessor.KeyReplacement{ | ||
{ | ||
Key: "servertracer.http.responsecode", | ||
NewKey: "http.status_code", | ||
}, | ||
{ | ||
Key: "servertracer.http.responsephrase", | ||
NewKey: "http.message", | ||
Overwrite: true, | ||
KeepOriginal: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "all_settings", | ||
file: "./testdata/global_attributes_all.yaml", | ||
want: &AttributesCfg{ | ||
Overwrite: true, | ||
Values: map[string]interface{}{"some_string": "hello world"}, | ||
KeyReplacements: []attributekeyprocessor.KeyReplacement{ | ||
{ | ||
Key: "servertracer.http.responsecode", | ||
NewKey: "http.status_code", | ||
}, | ||
{ | ||
Key: "servertracer.http.responsephrase", | ||
NewKey: "http.message", | ||
Overwrite: true, | ||
KeepOriginal: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
v, err := loadViperFromFile(tt.file) | ||
if err != nil { | ||
t.Fatalf("Failed to load viper from test file: %v", err) | ||
} | ||
|
||
cfg := NewDefaultMultiSpanProcessorCfg().InitFromViper(v) | ||
|
||
got := cfg.Global.Attributes | ||
if got == nil { | ||
t.Fatalf("got nil, want non-nil") | ||
} | ||
|
||
if diff := cmp.Diff(got, tt.want); diff != "" { | ||
t.Errorf("Mismatched global configuration\n-Got +Want:\n\t%s", diff) | ||
} | ||
}) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
cmd/occollector/app/builder/testdata/global_attributes_all.yaml
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,12 @@ | ||
global: | ||
attributes: | ||
overwrite: true | ||
values: | ||
some_string: "hello world" | ||
key-mapping: | ||
- key: servertracer.http.responsecode | ||
replacement: http.status_code | ||
- key: servertracer.http.responsephrase | ||
replacement: http.message | ||
overwrite: true | ||
keep: true |
9 changes: 9 additions & 0 deletions
9
cmd/occollector/app/builder/testdata/global_attributes_key_mapping.yaml
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,9 @@ | ||
global: | ||
attributes: | ||
key-mapping: | ||
- key: servertracer.http.responsecode | ||
replacement: http.status_code | ||
- key: servertracer.http.responsephrase | ||
replacement: http.message | ||
overwrite: true | ||
keep: true |
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
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,106 @@ | ||
// Copyright 2019, OpenCensus 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 collector | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"go.uber.org/zap" | ||
|
||
"github.com/census-instrumentation/opencensus-service/processor/addattributesprocessor" | ||
"github.com/census-instrumentation/opencensus-service/processor/attributekeyprocessor" | ||
"github.com/census-instrumentation/opencensus-service/processor/multiconsumer" | ||
"github.com/census-instrumentation/opencensus-service/processor/processortest" | ||
) | ||
|
||
func Test_startProcessor(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setupViperCfg func() *viper.Viper | ||
wantExamplar func(t *testing.T) interface{} | ||
}{ | ||
{ | ||
name: "incomplete_global_attrib_config", | ||
setupViperCfg: func() *viper.Viper { | ||
v := viper.New() | ||
v.Set("logging-exporter", true) | ||
v.Set("global.attributes.overwrite", true) | ||
return v | ||
}, | ||
wantExamplar: func(t *testing.T) interface{} { | ||
return multiconsumer.NewTraceProcessor(nil) | ||
}, | ||
}, | ||
{ | ||
name: "global_attrib_config_values", | ||
setupViperCfg: func() *viper.Viper { | ||
v := viper.New() | ||
v.Set("logging-exporter", true) | ||
v.Set("global.attributes.values", map[string]interface{}{"foo": "bar"}) | ||
return v | ||
}, | ||
wantExamplar: func(t *testing.T) interface{} { | ||
nopProcessor := processortest.NewNopTraceProcessor(nil) | ||
addAttributesProcessor, err := addattributesprocessor.NewTraceProcessor(nopProcessor) | ||
if err != nil { | ||
t.Fatalf("addattributesprocessor.NewTraceProcessor() = %v", err) | ||
} | ||
return addAttributesProcessor | ||
}, | ||
}, | ||
{ | ||
name: "global_attrib_config_key_mapping", | ||
setupViperCfg: func() *viper.Viper { | ||
v := viper.New() | ||
v.Set("logging-exporter", true) | ||
v.Set("global.attributes.key-mapping", | ||
[]map[string]interface{}{ | ||
{ | ||
"key": "foo", | ||
"replacement": "bar", | ||
}, | ||
}) | ||
return v | ||
}, | ||
wantExamplar: func(t *testing.T) interface{} { | ||
nopProcessor := processortest.NewNopTraceProcessor(nil) | ||
attributeKeyProcessor, err := attributekeyprocessor.NewTraceProcessor(nopProcessor) | ||
if err != nil { | ||
t.Fatalf("attributekeyprocessor.NewTraceProcessor() = %v", err) | ||
} | ||
return attributeKeyProcessor | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
consumer, closeFns := startProcessor(tt.setupViperCfg(), zap.NewNop()) | ||
if consumer == nil { | ||
t.Errorf("startProcessor() got nil consumer") | ||
} | ||
consumerExamplar := tt.wantExamplar(t) | ||
if reflect.TypeOf(consumer) != reflect.TypeOf(consumerExamplar) { | ||
t.Errorf("startProcessor() got consumer type %q want %q", | ||
reflect.TypeOf(consumer), | ||
reflect.TypeOf(consumerExamplar)) | ||
} | ||
for _, closeFn := range closeFns { | ||
closeFn() | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.