Skip to content

Commit ba66fdc

Browse files
committed
port decode aws to new code structure
1 parent caf33c2 commit ba66fdc

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ Flags:
3636
-h, --help help for flowlogs2metrics
3737
--log-level string Log level: debug, info, warning, error (default "error")
3838
--pipeLine.ingest.collector string Ingest collector API
39+
--pipeline.decode.aws string aws fields
3940
--pipeline.decode.type string Decode type: aws, json, none (default "none")
4041
--pipeline.encode.prom string Prometheus encode API
4142
--pipeline.encode.type string Encode type: prom, none (default "none")
4243
--pipeline.extract.aggregates string Aggregates (see docs)
4344
--pipeline.extract.type string Extract type: aggregates, none (default "none")
44-
--pipeline.ingest.aws.fields strings aws fields
4545
--pipeline.ingest.file.filename string Ingest filename (file)
4646
--pipeline.ingest.type string Ingest type: file, collector,file_loop (required)
4747
--pipeline.transform string Transforms (list) API (default "[{"type": "none"}]")

cmd/flowlogs2metrics/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func initFlags() {
128128
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Ingest.Type, "pipeline.ingest.type", "", "Ingest type: file, collector,file_loop (required)")
129129
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Ingest.File.Filename, "pipeline.ingest.file.filename", "", "Ingest filename (file)")
130130
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Ingest.Collector, "pipeLine.ingest.collector", "", "Ingest collector API")
131-
rootCmd.PersistentFlags().StringSliceVar(&config.Opt.PipeLine.Ingest.Aws.Fields, "pipeline.ingest.aws.fields", nil, "aws fields")
131+
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Decode.Aws, "pipeline.decode.aws", "", "aws fields")
132132
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Decode.Type, "pipeline.decode.type", "none", "Decode type: aws, json, none")
133133
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Transform, "pipeline.transform", "[{\"type\": \"none\"}]", "Transforms (list) API")
134134
rootCmd.PersistentFlags().StringVar(&config.Opt.PipeLine.Extract.Type, "pipeline.extract.type", "none", "Extract type: aggregates, none")

pkg/api/decode_aws.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2022 IBM, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package api
19+
20+
type EncodeAwsStruct struct {
21+
Fields []string `yaml:"fields" doc:"list of aws flow log fields"`
22+
}

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ type Ingest struct {
4040
Type string
4141
File File
4242
Collector string
43-
Aws Aws
4443
}
4544

4645
type File struct {
@@ -53,6 +52,7 @@ type Aws struct {
5352

5453
type Decode struct {
5554
Type string
55+
Aws string
5656
}
5757

5858
type Extract struct {

pkg/pipeline/decode/decode_aws.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy ofthe License at
6+
* You may obtain a copy of the License at
77
*
88
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specificlanguage governing permissions and
13+
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
1616
*/
1717

1818
package decode
1919

2020
import (
21+
"encoding/json"
22+
"github.com/netobserv/flowlogs2metrics/pkg/api"
2123
"github.com/netobserv/flowlogs2metrics/pkg/config"
2224
log "github.com/sirupsen/logrus"
2325
"strings"
@@ -51,11 +53,12 @@ func (c *decodeAws) Decode(in []interface{}) []config.GenericMap {
5153
out := make([]config.GenericMap, 0)
5254
nItems := len(in)
5355
log.Debugf("nItems = %d", nItems)
54-
for _, line := range in {
56+
for i, line := range in {
5557
lineSlice := strings.Fields(line.(string))
5658
nFields := len(lineSlice)
57-
if nFields > len(c.keyTags) {
58-
nFields = len(c.keyTags)
59+
if nFields != len(c.keyTags) {
60+
log.Errorf("decodeAws Decode: wrong number of fields in line %d", i+1)
61+
continue
5962
}
6063
record := make(config.GenericMap)
6164
for i := 0; i < nFields; i++ {
@@ -71,12 +74,23 @@ func (c *decodeAws) Decode(in []interface{}) []config.GenericMap {
7174
// NewDecodeAws create a new decode
7275
func NewDecodeAws() (Decoder, error) {
7376
log.Debugf("entering NewDecodeAws")
74-
RecordKeys := config.Opt.PipeLine.Ingest.Aws.Fields
75-
if RecordKeys == nil {
76-
RecordKeys = defaultKeys
77+
var recordKeys []string
78+
fieldsString := config.Opt.PipeLine.Decode.Aws
79+
log.Debugf("fieldsString = %v", fieldsString)
80+
if fieldsString != "" {
81+
var awsFields api.EncodeAwsStruct
82+
err := json.Unmarshal([]byte(fieldsString), &awsFields)
83+
if err != nil {
84+
log.Errorf("NewDecodeAws: error in unmarshalling fields: %v", err)
85+
return nil, err
86+
}
87+
recordKeys = awsFields.Fields
88+
} else {
89+
recordKeys = defaultKeys
7790
}
78-
log.Debugf("RecordKeys = %v", RecordKeys)
91+
92+
log.Debugf("recordKeys = %v", recordKeys)
7993
return &decodeAws{
80-
keyTags: RecordKeys,
94+
keyTags: recordKeys,
8195
}, nil
8296
}

0 commit comments

Comments
 (0)