|
| 1 | +# Histogram Generator |
| 2 | + |
| 3 | +A Go package for generating realistic histogram data for OpenTelemetry metrics testing. This package provides statistical distribution functions and can optionally publish generated metrics to OTLP endpoints. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +The package is organized into focused modules for maintainability and scalability: |
| 8 | + |
| 9 | +``` |
| 10 | +cmd/generator/ |
| 11 | +├── generator.go # Package documentation and overview |
| 12 | +├── types.go # Core data structures and types |
| 13 | +├── histogram_generator.go # Main histogram generation logic |
| 14 | +├── distributions.go # Statistical distribution functions |
| 15 | +├── otlp_publisher.go # OTLP endpoint publishing functionality |
| 16 | +└── example_test.go # Usage examples |
| 17 | +``` |
| 18 | + |
| 19 | +### Key Components |
| 20 | + |
| 21 | +- **HistogramGenerator**: Main generator that creates histogram data from statistical distributions |
| 22 | +- **OTLPPublisher**: Handles publishing metrics to OpenTelemetry Protocol endpoints using both telemetrygen and custom OTLP |
| 23 | +- **Distribution Functions**: Various statistical distributions (Normal, Exponential, Gamma, etc.) |
| 24 | +- **Types**: Shared data structures for histogram inputs, outputs, and configuration |
| 25 | + |
| 26 | +### Publishing Approach |
| 27 | + |
| 28 | +The package uses the official OpenTelemetry telemetrygen package for all metric publishing, ensuring: |
| 29 | + |
| 30 | +- **Standard Compliance**: Full compatibility with OTLP endpoints |
| 31 | +- **Reliability**: Uses the same code as the official telemetrygen tool |
| 32 | +- **Simplicity**: Clean API with `metrics.Start(cfg)` under the hood |
| 33 | +- **Flexibility**: Supports Gauge, Sum, and Histogram metric types |
| 34 | + |
| 35 | +The histogram generator creates realistic data distributions, while telemetrygen handles the actual OTLP publishing. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### Basic Generation |
| 40 | + |
| 41 | +```go |
| 42 | +generator := NewHistogramGenerator(GenerationOptions{ |
| 43 | + Seed: 12345, // For reproducible results |
| 44 | +}) |
| 45 | + |
| 46 | +input := HistogramInput{ |
| 47 | + Count: 1000, |
| 48 | + Min: ptr(10.0), |
| 49 | + Max: ptr(200.0), |
| 50 | + Boundaries: []float64{25, 50, 75, 100, 150}, |
| 51 | + Attributes: map[string]string{"service.name": "test-service"}, |
| 52 | +} |
| 53 | + |
| 54 | +result, err := generator.GenerateHistogram(input, func(rnd *rand.Rand, t time.Time) float64 { |
| 55 | + return NormalRandom(rnd, 75, 25) // mean=75, stddev=25 |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +### Generation with Publishing |
| 60 | + |
| 61 | +```go |
| 62 | +generator := NewHistogramGenerator(GenerationOptions{ |
| 63 | + Seed: time.Now().UnixNano(), |
| 64 | + Endpoint: "localhost:4318", // OTLP HTTP endpoint |
| 65 | +}) |
| 66 | + |
| 67 | +result, err := generator.GenerateAndPublishHistograms(input, valueFunc) |
| 68 | +``` |
| 69 | + |
| 70 | +### Direct Publishing with Telemetrygen |
| 71 | + |
| 72 | +You can also use telemetrygen directly for different metric types: |
| 73 | + |
| 74 | +```go |
| 75 | +publisher := NewOTLPPublisher("localhost:4318") |
| 76 | + |
| 77 | +// Send different types of metrics using telemetrygen |
| 78 | +err := publisher.SendSumMetric("requests_total", 100) |
| 79 | +err = publisher.SendGaugeMetric("cpu_usage", 75.5) |
| 80 | +err = publisher.SendHistogramMetricSimple("response_time") |
| 81 | +``` |
| 82 | + |
| 83 | +This approach uses the official OpenTelemetry telemetrygen package under the hood, ensuring compatibility with standard OTLP endpoints. |
| 84 | + |
| 85 | +## Available Distributions |
| 86 | + |
| 87 | +- **NormalRandom**: Normal (Gaussian) distribution |
| 88 | +- **ExponentialRandom**: Exponential distribution |
| 89 | +- **GammaRandom**: Gamma distribution |
| 90 | +- **LogNormalRandom**: Log-normal distribution |
| 91 | +- **WeibullRandom**: Weibull distribution |
| 92 | +- **BetaRandom**: Beta distribution |
| 93 | + |
| 94 | +### Time-based Functions |
| 95 | + |
| 96 | +- **SinusoidalValue**: Sinusoidal patterns with noise |
| 97 | +- **SpikyValue**: Baseline with occasional spikes |
| 98 | +- **TrendingValue**: Linear trend with noise |
| 99 | + |
| 100 | +## Design Principles |
| 101 | + |
| 102 | +### Single Responsibility |
| 103 | +Each file has a focused purpose: |
| 104 | +- `types.go`: Data structures only |
| 105 | +- `distributions.go`: Statistical functions only |
| 106 | +- `histogram_generator.go`: Core generation logic only |
| 107 | +- `otlp_publisher.go`: Publishing logic only |
| 108 | + |
| 109 | +### Dependency Injection |
| 110 | +The generator accepts value functions, allowing for flexible distribution selection and custom patterns. |
| 111 | + |
| 112 | +### Testability |
| 113 | +All components are designed for easy unit testing with deterministic seeds and dependency injection. |
| 114 | + |
| 115 | +### Extensibility |
| 116 | +New distributions can be added to `distributions.go` without affecting other components. |
| 117 | + |
| 118 | +## Features |
| 119 | + |
| 120 | +- ✅ **Statistical Distributions**: Multiple distribution functions for realistic data |
| 121 | +- ✅ **OTLP Publishing**: Direct integration with OpenTelemetry Protocol endpoints |
| 122 | +- ✅ **Flexible Generation**: Custom value functions and deterministic seeds |
| 123 | +- ✅ **Multiple Metric Types**: Support for Gauge, Sum, and Histogram metrics |
| 124 | + |
| 125 | +## Future Enhancements |
| 126 | + |
| 127 | +1. **Additional Publishers**: Support for Prometheus, StatsD, etc. |
| 128 | +2. **More Distributions**: Poisson, Binomial, etc. |
| 129 | +3. **Validation**: Input validation for histogram consistency |
| 130 | +4. **Batch Generation**: Generate multiple histograms efficiently |
| 131 | +5. **Configuration Files**: YAML/JSON configuration support |
| 132 | + |
| 133 | +## Testing |
| 134 | + |
| 135 | +The package includes comprehensive examples in `example_test.go` and integrates with the test cases in `share/testdata/histograms/`. |
| 136 | + |
| 137 | +Run tests: |
| 138 | +```bash |
| 139 | +go test ./cmd/generator/... |
| 140 | +``` |
| 141 | + |
| 142 | +## Integration |
| 143 | + |
| 144 | +This generator is used by: |
| 145 | +- `share/testdata/histograms/histograms.go`: Test case generation |
| 146 | +- Various metric exporters for testing realistic data patterns |
| 147 | +- Performance testing tools for load generation |
0 commit comments