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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
package telemetry
import (
"context"
"sync"
"testing"
"go.opentelemetry.io/otel/metric"
)
// Mock Int64Counter implementation
type MockInt64Counter struct {
metric.Int64Counter
addCalled bool
}
func (m *MockInt64Counter) Add(ctx context.Context, value int64, opts ...metric.AddOption) {
m.addCalled = true
}
// Mock Float64Histogram implementation
type MockFloat64Histogram struct {
metric.Float64Histogram
recordCalled bool
}
func (m *MockFloat64Histogram) Record(ctx context.Context, value float64, opts ...metric.RecordOption) {
m.recordCalled = true
}
// Mock Meter implementation
type MockMeter struct {
metric.Meter
counters map[string]metric.Int64Counter
histograms map[string]metric.Float64Histogram
}
func (m *MockMeter) Int64Counter(name string, opts ...metric.Int64CounterOption) (metric.Int64Counter, error) {
if counter, exists := m.counters[name]; exists {
return counter, nil
}
counter := &MockInt64Counter{}
m.counters[name] = counter
return counter, nil
}
func (m *MockMeter) Float64Histogram(name string, opts ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
if histogram, exists := m.histograms[name]; exists {
return histogram, nil
}
histogram := &MockFloat64Histogram{}
m.histograms[name] = histogram
return histogram, nil
}
func TestGetCounter(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Counters: make(map[string]metric.Int64Counter),
}
counter, err := metrics.GetCounter("test_counter", "A test counter")
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if counter == nil {
t.Fatalf("Expected a non-nil counter, but got nil")
}
counter2, err := metrics.GetCounter("test_counter", "A test counter")
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if counter != counter2 {
t.Fatalf("Expected the same counter instance to be returned")
}
}
// Run this test with the "-race" flag.
//
// go test -race -run ^TestGetCounterRace github.com/openfga/go-sdk/telemetry
func TestGetCounterRace(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Counters: make(map[string]metric.Int64Counter),
}
t.Parallel()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := metrics.GetCounter("test_counter", "A test counter")
if err != nil {
t.Errorf("Expected no error, but got %v", err)
return
}
Get(TelemetryFactoryParameters{})
}()
}
wg.Wait()
}
func TestGetHistogram(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Histograms: make(map[string]metric.Float64Histogram),
}
histogram, err := metrics.GetHistogram("test_histogram", "A test histogram", "ms")
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if histogram == nil {
t.Fatalf("Expected a non-nil histogram, but got nil")
}
histogram2, err := metrics.GetHistogram("test_histogram", "A test histogram", "ms")
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if histogram != histogram2 {
t.Fatalf("Expected the same histogram instance to be returned")
}
}
// go test -race -run ^TestGetHistogramRace github.com/openfga/go-sdk/telemetry
func TestGetHistogramRace(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Histograms: make(map[string]metric.Float64Histogram),
}
t.Parallel()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := metrics.GetHistogram("test_histogram", "A test histogram", "ms")
if err != nil {
t.Errorf("Expected no error, but got %v", err)
return
}
}()
}
wg.Wait()
}
func TestCredentialsRequest(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Counters: make(map[string]metric.Int64Counter),
}
attrs := make(map[*Attribute]string)
counter, err := metrics.CredentialsRequest(1, attrs)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if counter == nil {
t.Fatalf("Expected a non-nil counter, but got nil")
}
mockCounter, ok := counter.(*MockInt64Counter)
if !ok || !mockCounter.addCalled {
t.Fatalf("Expected Add method to be called on counter")
}
}
func TestRequestDuration(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Histograms: make(map[string]metric.Float64Histogram),
}
attrs := make(map[*Attribute]string)
histogram, err := metrics.RequestDuration(1.0, attrs)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if histogram == nil {
t.Fatalf("Expected a non-nil histogram, but got nil")
}
mockHistogram, ok := histogram.(*MockFloat64Histogram)
if !ok || !mockHistogram.recordCalled {
t.Fatalf("Expected Record method to be called on histogram")
}
}
func TestQueryDuration(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Histograms: make(map[string]metric.Float64Histogram),
}
attrs := make(map[*Attribute]string)
histogram, err := metrics.QueryDuration(1.0, attrs)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if histogram == nil {
t.Fatalf("Expected a non-nil histogram, but got nil")
}
mockHistogram, ok := histogram.(*MockFloat64Histogram)
if !ok || !mockHistogram.recordCalled {
t.Fatalf("Expected Record method to be called on histogram")
}
}
|