-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to template executer. Signed-off-by: Serge Logvinov <[email protected]>
- Loading branch information
1 parent
0e8728c
commit 386958d
Showing
7 changed files
with
223 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package metrics | ||
|
||
import ( | ||
"time" | ||
|
||
"k8s.io/component-base/metrics" | ||
"k8s.io/component-base/metrics/legacyregistry" | ||
) | ||
|
||
// TransformerMetrics contains the metrics for transformer. | ||
type TransformerMetrics struct { | ||
Duration *metrics.HistogramVec | ||
Errors *metrics.CounterVec | ||
} | ||
|
||
var transformerMetrics = registerTransformerMetrics() | ||
|
||
// ObserveTransformer records the transformer latency and counts the errors. | ||
func (mc *MetricContext) ObserveTransformer(err error) error { | ||
transformerMetrics.Duration.WithLabelValues(mc.attributes...).Observe( | ||
time.Since(mc.start).Seconds()) | ||
|
||
if err != nil { | ||
transformerMetrics.Errors.WithLabelValues(mc.attributes...).Inc() | ||
} | ||
|
||
return err | ||
} | ||
|
||
func registerTransformerMetrics() *TransformerMetrics { | ||
metrics := &TransformerMetrics{ | ||
Duration: metrics.NewHistogramVec( | ||
&metrics.HistogramOpts{ | ||
Name: "talosccm_transformer_duration_seconds", | ||
Help: "Latency of an Transformer call", | ||
Buckets: []float64{.001, .01, .05, .1}, | ||
}, []string{"type"}), | ||
Errors: metrics.NewCounterVec( | ||
&metrics.CounterOpts{ | ||
Name: "talosccm_transformer_errors_total", | ||
Help: "Total number of errors for an Transformer call", | ||
}, []string{"type"}), | ||
} | ||
|
||
legacyregistry.MustRegister( | ||
metrics.Duration, | ||
metrics.Errors, | ||
) | ||
|
||
return metrics | ||
} |
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,85 @@ | ||
package transformer | ||
|
||
import ( | ||
"encoding/base64" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var genericMap = map[string]interface{}{ | ||
// String functions: | ||
"upper": strings.ToUpper, | ||
"lower": strings.ToLower, | ||
"trim": strings.TrimSpace, | ||
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) }, | ||
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) }, | ||
|
||
"replace": func(o, n, s string) string { return strings.ReplaceAll(s, o, n) }, | ||
"regexFind": regexFind, | ||
"regexFindString": regexFindString, | ||
"regexReplaceAll": regexReplaceAll, | ||
|
||
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) }, | ||
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) }, | ||
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) }, | ||
|
||
// Encoding functions: | ||
"b64enc": base64encode, | ||
"b64dec": base64decode, | ||
} | ||
|
||
// GenericFuncMap returns a copy of the basic function map as a map[string]interface{}. | ||
func GenericFuncMap() map[string]interface{} { | ||
gfm := make(map[string]interface{}, len(genericMap)) | ||
for k, v := range genericMap { | ||
gfm[k] = v | ||
} | ||
|
||
return gfm | ||
} | ||
|
||
func regexFindString(regex string, s string, n int) (string, error) { | ||
r, err := regexp.Compile(regex) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
matches := r.FindStringSubmatch(s) | ||
|
||
if len(matches) < n+1 { | ||
return "", nil | ||
} | ||
|
||
return matches[n], nil | ||
} | ||
|
||
func regexReplaceAll(regex string, s string, repl string) (string, error) { | ||
r, err := regexp.Compile(regex) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return r.ReplaceAllString(s, repl), nil | ||
} | ||
|
||
func regexFind(regex string, s string) (string, error) { | ||
r, err := regexp.Compile(regex) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return r.FindString(s), nil | ||
} | ||
|
||
func base64encode(v string) string { | ||
return base64.StdEncoding.EncodeToString([]byte(v)) | ||
} | ||
|
||
func base64decode(v string) (string, error) { | ||
data, err := base64.StdEncoding.DecodeString(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(data), 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
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