-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* auth with client cert in kafka plugin (#607) * auth with client cert in kafka plugin * because you can set content of cert or key * e2e test for kafka auth by client cert * dont skip event on error parse meta * when template for meta is simple * fix metadata fillment after review * rename metadata registry to templater * add meta in configs for e2e test * dont reuse buffer in templater for avoid race * convertToResultMaps returns strings * Revert "dont reuse buffer in templater for avoid race" This reverts commit 60db00c. * poolBuffer in meta templater * use sync.pool in template if we have templates * fix flaky TestServeChunks
- Loading branch information
1 parent
a9fd975
commit e4ace70
Showing
22 changed files
with
432 additions
and
42 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,3 @@ | ||
package cfg | ||
|
||
type MetaTemplates map[string]string |
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,76 @@ | ||
package metadata | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
"text/template" | ||
|
||
"github.com/ozontech/file.d/cfg" | ||
) | ||
|
||
type MetaData map[string]string | ||
|
||
type MetaTemplater struct { | ||
templates map[string]*template.Template | ||
singleValues map[string]string | ||
poolBuffer sync.Pool | ||
} | ||
|
||
func NewMetaTemplater(templates cfg.MetaTemplates) *MetaTemplater { | ||
compiledTemplates := make(map[string]*template.Template) | ||
singleValues := make(map[string]string) | ||
singleValueRegex := regexp.MustCompile(`^\{\{\ +\.(\w+)\ +\}\}$`) | ||
|
||
for k, v := range templates { | ||
vals := singleValueRegex.FindStringSubmatch(v) | ||
if len(vals) > 1 { | ||
singleValues[k] = vals[1] | ||
} else { | ||
compiledTemplates[k] = template.Must(template.New("").Parse(v)) | ||
} | ||
} | ||
|
||
meta := MetaTemplater{ | ||
templates: compiledTemplates, | ||
singleValues: singleValues, | ||
poolBuffer: sync.Pool{ | ||
New: func() interface{} { return new(bytes.Buffer) }, | ||
}, | ||
} | ||
|
||
return &meta | ||
} | ||
|
||
type Data interface { | ||
GetData() map[string]any | ||
} | ||
|
||
func (m *MetaTemplater) Render(data Data) (MetaData, error) { | ||
values := data.GetData() | ||
meta := MetaData{} | ||
|
||
if len(m.templates) > 0 { | ||
tplOutput := m.poolBuffer.Get().(*bytes.Buffer) | ||
defer m.poolBuffer.Put(tplOutput) | ||
|
||
for k, tmpl := range m.templates { | ||
tplOutput.Reset() | ||
err := tmpl.Execute(tplOutput, values) | ||
if err != nil { | ||
return meta, err | ||
} else { | ||
meta[k] = tplOutput.String() | ||
} | ||
} | ||
} | ||
|
||
for k, tmpl := range m.singleValues { | ||
if val, ok := values[tmpl]; ok { | ||
meta[k] = fmt.Sprintf("%v", val) | ||
} | ||
} | ||
|
||
return meta, nil | ||
} |
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,43 @@ | ||
package metadata | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ozontech/file.d/cfg" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTemplaterRender(t *testing.T) { | ||
templater := NewMetaTemplater( | ||
cfg.MetaTemplates{ | ||
"partition": "partition_{{ .partition }}", | ||
"partition_describe": "{{ .partition }} partition", | ||
"topic": "{{ .topic }}", | ||
"broker": "{{ .broker }}", | ||
}, | ||
) | ||
|
||
data := testMetadata{} | ||
result, err := templater.Render(data) | ||
assert.Nil(t, err) | ||
assert.Equal( | ||
t, | ||
fmt.Sprint(map[string]any{ | ||
"topic": "topic", | ||
"partition": "partition_1", | ||
"partition_describe": "1 partition", | ||
}), | ||
fmt.Sprint(result), | ||
) | ||
} | ||
|
||
type testMetadata struct{} | ||
|
||
func (f testMetadata) GetData() map[string]any { | ||
return map[string]any{ | ||
"topic": "topic", | ||
"partition": 1, | ||
"offset": 1000, | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.