-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability.go
More file actions
100 lines (86 loc) · 1.9 KB
/
Copy pathobservability.go
File metadata and controls
100 lines (86 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package confkit
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
type LoadMetrics struct {
TotalTime time.Duration
SourceTimes map[string]time.Duration
ValidationTime time.Duration
ErrorCount int
}
func DumpConfig(cfg any, fields []FieldInfo) ([]byte, error) {
dump := make(map[string]any)
val := reflect.ValueOf(cfg)
if val.Kind() == reflect.Pointer {
val = val.Elem()
}
for _, field := range fields {
value := getFieldValue(val, field.Path)
if field.IsSecret {
dump[field.Path] = "***REDACTED***"
} else {
dump[field.Path] = value
}
}
return json.MarshalIndent(dump, "", " ")
}
func getFieldValue(val reflect.Value, path string) any {
parts := splitPath(path)
current := val
for _, part := range parts {
field := current.FieldByName(part)
if !field.IsValid() {
return nil
}
if field.Kind() == reflect.Pointer {
if field.IsNil() {
return nil
}
current = field.Elem()
} else {
current = field
}
}
if current.IsValid() {
return current.Interface()
}
return nil
}
func LogLoadStart(sources []string) string {
payload := map[string]any{
"event": "config_load_start",
"sources": sources,
"timestamp": time.Now().Format(time.RFC3339),
}
return toJSON(payload)
}
func LogLoadComplete(duration time.Duration, fieldCount int, errorCount int) string {
return fmt.Sprintf(`{"event":"config_load_complete","duration_ms":%d,"fields_loaded":%d,"validation_errors":%d}`,
duration.Milliseconds(), fieldCount, errorCount)
}
func toJSON(data any) string {
b, _ := json.Marshal(data)
return string(b)
}
func splitPath(path string) []string {
var parts []string
var cur strings.Builder
for _, ch := range path {
if ch == '.' {
if cur.Len() > 0 {
parts = append(parts, cur.String())
cur.Reset()
}
} else {
cur.WriteRune(ch)
}
}
if cur.Len() > 0 {
parts = append(parts, cur.String())
}
return parts
}