Skip to content

Commit a20a6f4

Browse files
authored
Change fields type and add sourceFormat(s) (#1756)
1 parent 1a9bf92 commit a20a6f4

File tree

9 files changed

+338
-75
lines changed

9 files changed

+338
-75
lines changed

exporter/sumologicexporter/exporter.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
)
2626

2727
type sumologicexporter struct {
28-
config *Config
28+
config *Config
29+
sources sourceFormats
2930
}
3031

3132
func initExporter(cfg *Config) (*sumologicexporter, error) {
@@ -56,8 +57,14 @@ func initExporter(cfg *Config) (*sumologicexporter, error) {
5657
return nil, errors.New("endpoint is not set")
5758
}
5859

60+
sfs, err := newSourceFormats(cfg)
61+
if err != nil {
62+
return nil, err
63+
}
64+
5965
se := &sumologicexporter{
60-
config: cfg,
66+
config: cfg,
67+
sources: sfs,
6168
}
6269

6370
return se, nil

exporter/sumologicexporter/fields.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2020 OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sumologicexporter
16+
17+
import (
18+
"fmt"
19+
"sort"
20+
"strings"
21+
)
22+
23+
// fields represents metadata
24+
type fields map[string]string
25+
26+
// string returns fields as ordered key=value string with `, ` as separator
27+
func (f fields) string() string {
28+
rv := make([]string, 0, len(f))
29+
for k, v := range f {
30+
rv = append(rv, fmt.Sprintf("%s=%s", k, v))
31+
}
32+
sort.Strings(rv)
33+
34+
return strings.Join(rv, ", ")
35+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sumologicexporter
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestFieldsAsString(t *testing.T) {
24+
expected := "key1=value1, key2=value2, key3=value3"
25+
flds := fields{
26+
"key1": "value1",
27+
"key3": "value3",
28+
"key2": "value2",
29+
}
30+
31+
assert.Equal(t, expected, flds.string())
32+
}

exporter/sumologicexporter/filter.go

+6-25
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
package sumologicexporter
1616

1717
import (
18-
"fmt"
1918
"regexp"
20-
"sort"
21-
"strings"
2219

2320
"go.opentelemetry.io/collector/consumer/pdata"
2421
tracetranslator "go.opentelemetry.io/collector/translator/trace"
@@ -28,9 +25,6 @@ type filter struct {
2825
regexes []*regexp.Regexp
2926
}
3027

31-
// fields represents concatenated metadata
32-
type fields string
33-
3428
func newFilter(flds []string) (filter, error) {
3529
metadataRegexes := make([]*regexp.Regexp, len(flds))
3630

@@ -48,9 +42,9 @@ func newFilter(flds []string) (filter, error) {
4842
}, nil
4943
}
5044

51-
// filterIn returns map of strings which matches at least one of the filter regexes
52-
func (f *filter) filterIn(attributes pdata.AttributeMap) map[string]string {
53-
returnValue := make(map[string]string)
45+
// filterIn returns fields which match at least one of the filter regexes
46+
func (f *filter) filterIn(attributes pdata.AttributeMap) fields {
47+
returnValue := make(fields)
5448

5549
attributes.ForEach(func(k string, v pdata.AttributeValue) {
5650
for _, regex := range f.regexes {
@@ -63,9 +57,9 @@ func (f *filter) filterIn(attributes pdata.AttributeMap) map[string]string {
6357
return returnValue
6458
}
6559

66-
// filterOut returns map of strings which doesn't match any of the filter regexes
67-
func (f *filter) filterOut(attributes pdata.AttributeMap) map[string]string {
68-
returnValue := make(map[string]string)
60+
// filterOut returns fields which don't match any of the filter regexes
61+
func (f *filter) filterOut(attributes pdata.AttributeMap) fields {
62+
returnValue := make(fields)
6963

7064
attributes.ForEach(func(k string, v pdata.AttributeValue) {
7165
for _, regex := range f.regexes {
@@ -77,16 +71,3 @@ func (f *filter) filterOut(attributes pdata.AttributeMap) map[string]string {
7771
})
7872
return returnValue
7973
}
80-
81-
// getMetadata builds string which represents metadata in alphabetical order
82-
func (f *filter) getMetadata(attributes pdata.AttributeMap) fields {
83-
attrs := f.filterIn(attributes)
84-
metadata := make([]string, 0, len(attrs))
85-
86-
for k, v := range attrs {
87-
metadata = append(metadata, fmt.Sprintf("%s=%s", k, v))
88-
}
89-
sort.Strings(metadata)
90-
91-
return fields(strings.Join(metadata, ", "))
92-
}

exporter/sumologicexporter/filter_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func TestGetMetadata(t *testing.T) {
3434
f, err := newFilter(regexes)
3535
require.NoError(t, err)
3636

37-
metadata := f.getMetadata(attributes)
38-
const expected fields = "key1=value1, key2=value2, key3=value3"
37+
metadata := f.filterIn(attributes)
38+
expected := fields{"key1": "value1", "key2": "value2", "key3": "value3"}
3939
assert.Equal(t, expected, metadata)
4040
}
4141

@@ -52,7 +52,7 @@ func TestFilterOutMetadata(t *testing.T) {
5252
require.NoError(t, err)
5353

5454
data := f.filterOut(attributes)
55-
expected := map[string]string{
55+
expected := fields{
5656
"additional_key2": "value2",
5757
"additional_key3": "value3",
5858
}

exporter/sumologicexporter/sender.go

+26-17
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ type appendResponse struct {
3636
}
3737

3838
type sender struct {
39-
buffer []pdata.LogRecord
40-
config *Config
41-
client *http.Client
42-
filter filter
43-
ctx context.Context
39+
buffer []pdata.LogRecord
40+
config *Config
41+
client *http.Client
42+
filter filter
43+
ctx context.Context
44+
sources sourceFormats
4445
}
4546

4647
const (
@@ -55,17 +56,25 @@ func newAppendResponse() appendResponse {
5556
}
5657
}
5758

58-
func newSender(ctx context.Context, cfg *Config, cl *http.Client, f filter) *sender {
59+
func newSender(
60+
ctx context.Context,
61+
cfg *Config,
62+
cl *http.Client,
63+
f filter,
64+
s sourceFormats,
65+
) *sender {
5966
return &sender{
60-
config: cfg,
61-
client: cl,
62-
filter: f,
63-
ctx: ctx,
67+
config: cfg,
68+
client: cl,
69+
filter: f,
70+
ctx: ctx,
71+
sources: s,
6472
}
6573
}
6674

6775
// send sends data to sumologic
6876
func (s *sender) send(pipeline PipelineType, body io.Reader, flds fields) error {
77+
6978
// Add headers
7079
req, err := http.NewRequestWithContext(s.ctx, http.MethodPost, s.config.HTTPClientSettings.Endpoint, body)
7180
if err != nil {
@@ -74,22 +83,22 @@ func (s *sender) send(pipeline PipelineType, body io.Reader, flds fields) error
7483

7584
req.Header.Add("X-Sumo-Client", s.config.Client)
7685

77-
if len(s.config.SourceHost) > 0 {
78-
req.Header.Add("X-Sumo-Host", s.config.SourceHost)
86+
if s.sources.host.isSet() {
87+
req.Header.Add("X-Sumo-Host", s.sources.host.format(flds))
7988
}
8089

81-
if len(s.config.SourceName) > 0 {
82-
req.Header.Add("X-Sumo-Name", s.config.SourceName)
90+
if s.sources.name.isSet() {
91+
req.Header.Add("X-Sumo-Name", s.sources.name.format(flds))
8392
}
8493

85-
if len(s.config.SourceCategory) > 0 {
86-
req.Header.Add("X-Sumo-Category", s.config.SourceCategory)
94+
if s.sources.category.isSet() {
95+
req.Header.Add("X-Sumo-Category", s.sources.category.format(flds))
8796
}
8897

8998
switch pipeline {
9099
case LogsPipeline:
91100
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
92-
req.Header.Add("X-Sumo-Fields", string(flds))
101+
req.Header.Add("X-Sumo-Fields", flds.string())
93102
case MetricsPipeline:
94103
// ToDo: Implement metrics pipeline
95104
return errors.New("current sender version doesn't support metrics")

0 commit comments

Comments
 (0)