fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Update go.mod and go.sum

This commit is contained in:
2025-11-06 16:55:12 +00:00
parent 5ae4ea1e25
commit 6261be6e53
+64 -5
View File
@@ -69,26 +69,43 @@ func ensureDefaultLabels(labels *map[string]string, podName string) {
} }
func appendSortedLabels(buf *bytes.Buffer, labels map[string]string) { func appendSortedLabels(buf *bytes.Buffer, labels map[string]string) {
if len(labels) == 0 { // Add defer/recover to prevent panics from crashing the application
defer func() {
if r := recover(); r != nil {
// Log the panic but don't crash
fmt.Fprintf(os.Stderr, "Recovered from panic in appendSortedLabels: %v\n", r)
}
}()
if len(labels) == 0 || buf == nil {
return return
} }
// Create a snapshot to avoid concurrent access issues // Create a snapshot to avoid concurrent access issues
labelsCopy := make(map[string]string, len(labels)) labelsCopy := make(map[string]string, len(labels))
for k, v := range labels { for k, v := range labels {
if k == "" {
continue // Skip empty keys
}
labelsCopy[k] = v labelsCopy[k] = v
} }
if len(labelsCopy) == 0 {
return
}
keys := getSortedKeys(labelsCopy) keys := getSortedKeys(labelsCopy)
for i, k := range keys { for i, k := range keys {
if v, ok := labelsCopy[k]; ok {
if i > 0 { if i > 0 {
buf.WriteByte(',') buf.WriteByte(',')
} }
buf.WriteString(k) buf.WriteString(k)
buf.WriteString(`="`) buf.WriteString(`="`)
buf.WriteString(labelsCopy[k]) buf.WriteString(v)
buf.WriteByte('"') buf.WriteByte('"')
} }
}
} }
func getSortedKeys(labels map[string]string) []string { func getSortedKeys(labels map[string]string) []string {
@@ -117,7 +134,15 @@ func getSortedKeys(labels map[string]string) []string {
} }
func labelsToString(labels map[string]string) string { func labelsToString(labels map[string]string) string {
if labels == nil { // Add defer/recover to prevent panics from crashing the application
defer func() {
if r := recover(); r != nil {
// Log the panic but don't crash
fmt.Fprintf(os.Stderr, "Recovered from panic in labelsToString: %v\n", r)
}
}()
if len(labels) == 0 {
return "" return ""
} }
@@ -126,18 +151,35 @@ func labelsToString(labels map[string]string) string {
values := make(map[string]string, len(labels)) values := make(map[string]string, len(labels))
for k, v := range labels { for k, v := range labels {
if k == "" {
continue // Skip empty keys
}
keys = append(keys, k) keys = append(keys, k)
values[k] = v values[k] = v
} }
if len(keys) == 0 {
return ""
}
sort.Strings(keys) sort.Strings(keys)
// Pre-allocate the builder with estimated capacity to avoid reallocation
var sb strings.Builder var sb strings.Builder
estimatedSize := 0
for _, k := range keys { for _, k := range keys {
estimatedSize += len(k) + len(values[k]) + 2 // key + value + '=' + ';'
}
sb.Grow(estimatedSize)
for _, k := range keys {
if v, ok := values[k]; ok {
sb.WriteString(k) sb.WriteString(k)
sb.WriteByte('=') sb.WriteByte('=')
sb.WriteString(values[k]) sb.WriteString(v)
sb.WriteByte(';') sb.WriteByte(';')
} }
}
return sb.String() return sb.String()
} }
@@ -186,6 +228,14 @@ func is_special_rune(r rune) bool {
} }
func compile_metrics_with_labels(name string, labels map[string]string) string { func compile_metrics_with_labels(name string, labels map[string]string) string {
// Add defer/recover to prevent panics from crashing the application
defer func() {
if r := recover(); r != nil {
// Log the panic but don't crash
fmt.Fprintf(os.Stderr, "Recovered from panic in compile_metrics_with_labels: %v\n", r)
}
}()
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString(name) buf.WriteString(name)
@@ -197,16 +247,25 @@ func compile_metrics_with_labels(name string, labels map[string]string) string {
// Create a snapshot to avoid concurrent access issues // Create a snapshot to avoid concurrent access issues
labelsCopy := make(map[string]string, len(labels)) labelsCopy := make(map[string]string, len(labels))
for k, v := range labels { for k, v := range labels {
if k == "" {
continue // Skip empty keys
}
labelsCopy[k] = v labelsCopy[k] = v
} }
if len(labelsCopy) == 0 {
return buf.String()
}
keys := getSortedKeys(labelsCopy) keys := getSortedKeys(labelsCopy)
for _, k := range keys { for _, k := range keys {
if v, ok := labelsCopy[k]; ok {
buf.WriteByte('_') buf.WriteByte('_')
buf.WriteString(k) buf.WriteString(k)
buf.WriteByte('_') buf.WriteByte('_')
buf.WriteString(labelsCopy[k]) buf.WriteString(v)
}
} }
return buf.String() return buf.String()