Skip to content

Commit 2dfe504

Browse files
author
Andrea Spacca
authored
Fix aws az from region (#71)
* share funcMap on calculating tot events * fix timestamp in aws.billing * fix test * proper fix
1 parent 7f82bca commit 2dfe504

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

assets/templates/aws.billing/schema-b/gotext.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
{{- $period := generate "metricset.period" }}
44
{{- $cloudId := generate "cloud.account.id" }}
55
{{- $cloudRegion := generate "cloud.region" }}
6+
{{- $timestamp := generate "timestamp" }}
67
{
7-
"@timestamp": "{{generate "timestamp"}}",
8+
"@timestamp": "{{$timestamp.Format "2006-01-02T15:04:05.999999Z07:00"}}",
89
"cloud": {
910
"provider": "aws",
1011
"region": "{{$cloudRegion}}",

pkg/genlib/generator_with_text_template.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"bytes"
99
"errors"
1010
"io"
11-
"math/rand"
11+
"math/rand"
1212
"text/template"
1313
"time"
1414

@@ -50,22 +50,20 @@ var awsAZs map[string][]string = map[string][]string{
5050
"us-west-2": {"us-west-2a", "us-west-2b", "us-west-2c", "us-west-2d"},
5151
}
5252

53-
func calculateTotEventsWithTextTemplate(totSize uint64, fieldMap map[string]any, errChan chan error, tpl []byte) (uint64, error) {
53+
func calculateTotEventsWithTextTemplate(totSize uint64, fieldMap map[string]any, errChan chan error, tpl []byte, templateFns template.FuncMap) (uint64, error) {
5454
if totSize == 0 {
5555
return 0, nil
5656
}
5757

5858
// Generate a single event to calculate the total number of events based on its size
5959
t := template.New("estimate_tot_events")
6060
t = t.Option("missingkey=error")
61-
62-
templateFns := sprig.TxtFuncMap()
63-
64-
templateFns["timeDuration"] = func(duration int64) time.Duration {
65-
return time.Duration(duration)
61+
tempTemplateFns := template.FuncMap{}
62+
for k, v := range templateFns {
63+
tempTemplateFns[k] = v
6664
}
6765

68-
templateFns["generate"] = func(field string) any {
66+
tempTemplateFns["generate"] = func(field string) any {
6967
state := NewGenState()
7068
state.prevCacheForDup[field] = make(map[any]struct{})
7169
state.prevCacheCardinality[field] = make([]any, 0)
@@ -127,21 +125,13 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields, totSize
127125

128126
errChan := make(chan error)
129127

130-
totEvents, err := calculateTotEventsWithTextTemplate(totSize, fieldMap, errChan, tpl)
131-
if err != nil {
132-
return nil, err
133-
}
134-
135-
t := template.New("generator")
136-
t = t.Option("missingkey=error")
137-
138128
templateFns := sprig.TxtFuncMap()
139129

140130
templateFns["timeDuration"] = func(duration int64) time.Duration {
141131
return time.Duration(duration)
142132
}
143-
144-
templateFns["awsAZFromRegion"] = func(region string) string {
133+
134+
templateFns["awsAZFromRegion"] = func(region string) string {
145135
azs, ok := awsAZs[region]
146136
if !ok {
147137
return "NoAZ"
@@ -160,6 +150,14 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields, totSize
160150
return bindF(state)
161151
}
162152

153+
totEvents, err := calculateTotEventsWithTextTemplate(totSize, fieldMap, errChan, tpl, templateFns)
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
t := template.New("generator")
159+
t = t.Option("missingkey=error")
160+
163161
parsedTpl, err := t.Funcs(templateFns).Parse(string(tpl))
164162
if err != nil {
165163
return nil, err

0 commit comments

Comments
 (0)