mirror of
https://github.com/hanzoai/traces.git
synced 2026-10-30 18:20:41 +00:00
* [Generator] Add DRAIN span_name sanitizer * Fix linter * Fix formatting * Update default manifest * Fix build error in update-fixtures * Remove bad test * Remove misleading comment * Add per-tenant override for drain mode * Do a machines job * Address PR feedback * Address PR review: rename drain_mode config and improve sanitizer - Rename `drain_mode` to `span_name_sanitization` across all config, legacy config, interfaces, and overrides for better user-facing clarity - Add explicit default value "disabled" in RegisterFlagsAndApplyDefaults - Add ValidateSpanNameSanitization function in validation/fields.go - Rename metrics to remove DRAIN reference: `drain_spans_compacted_total` -> `spans_sanitized_total` `post_drain_demand_estimate` -> `post_sanitization_demand_estimate` - Pass configured staleDuration to NewDrainSanitizer instead of hardcoded 15min, consistent with other registry components - Extract "span_name" magic string to labelSpanName constant - Update all tests with new names and signatures * Update manifest * Use empty string as disabled * Update CHANGELOG.md Co-authored-by: Suraj Nath <[email protected]> * Update manifest --------- Co-authored-by: Suraj Nath <[email protected]>
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/grafana/tempo/pkg/drain"
|
|
)
|
|
|
|
type TestData struct {
|
|
OriginalCount int
|
|
FinalCount int
|
|
ReductionPct float64
|
|
PatternMapping map[string]string
|
|
}
|
|
|
|
// This is a tool to generate test fixtures for the drain package.
|
|
func main() {
|
|
// For each .txt file in the testdata directory, read the file and run DRAIN on it.
|
|
// The output should be written to a new file in the testdata directory with the same name but with the suffix .drain
|
|
|
|
files, err := os.ReadDir("pkg/drain/testdata")
|
|
if err != nil {
|
|
log.Fatalf("failed to read testdata directory: %v", err)
|
|
}
|
|
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
continue
|
|
}
|
|
if !strings.HasSuffix(file.Name(), ".json") {
|
|
continue
|
|
}
|
|
linesJSON, err := os.ReadFile(filepath.Join("pkg/drain/testdata", file.Name()))
|
|
if err != nil {
|
|
log.Fatalf("failed to read file: %v", err)
|
|
}
|
|
var lines []string
|
|
err = json.Unmarshal(linesJSON, &lines)
|
|
if err != nil {
|
|
log.Fatalf("failed to unmarshal file: %v", err)
|
|
}
|
|
patternMapping := make(map[string]string)
|
|
drain := drain.New("test-tenant", drain.DefaultConfig())
|
|
for _, line := range lines {
|
|
cluster := drain.Train(line)
|
|
if cluster == nil {
|
|
patternMapping[line] = "<nil> (possibly too many tokens)"
|
|
} else {
|
|
patternMapping[line] = cluster.String()
|
|
}
|
|
}
|
|
clusters := drain.Clusters()
|
|
testData := TestData{
|
|
OriginalCount: len(lines),
|
|
FinalCount: len(clusters),
|
|
ReductionPct: 100 * float64(len(lines)-len(clusters)) / float64(len(lines)),
|
|
PatternMapping: patternMapping,
|
|
}
|
|
|
|
fmt.Printf("finished %s\n reduction: %f%%\n", file.Name(), testData.ReductionPct) //nolint:forbidigo
|
|
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(testData); err != nil {
|
|
log.Fatalf("failed to marshal test data: %v", err)
|
|
}
|
|
err = os.WriteFile(filepath.Join("pkg/drain/testdata", strings.TrimSuffix(file.Name(), ".json")+".drain"), buf.Bytes(), 0o600)
|
|
if err != nil {
|
|
log.Fatalf("failed to write file: %v", err)
|
|
}
|
|
}
|
|
}
|