mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
Re-sync the entire SigNoz-derived Go tree to a clean fork of latest SigNoz main (upstream 3e6339019), eliminating the version-skew that broke `go build ./...` (mixed-era packages, ~14 imported-but-absent packages, partial signoz->o11y / clickhouse->datastore rebrands, self-import cycle, duplicate package decls). - pkg/ + cmd/ wholesale replaced with latest SigNoz; imports rewritten github.com/SigNoz/signoz/ -> github.com/hanzoai/o11y/. - Adopt SigNoz's CURRENT structure (resourcefilter->telemetryresourcefilter, integrationtypes->cloudintegrationtypes, roletypes/grammar/times folded in upstream); old paths dropped. - Fork deps repathed to hanzoai + bumped: signoz-otel-collector v0.144.6, clickhouse-go-mock v0.14.1, govaluate v0.1.0; expr-lang/expr => hanzoai/expr v1.17.8 (replace). go.mod reconciled from latest SigNoz; go.sum re-tidied. - ee/ + cmd/enterprise stripped — o11y stays MIT-only; community edition imports no ee packages. - Preserved o11y additions: module path github.com/hanzoai/o11y, mount.go (cloud.Register/zip mount adapter, HIP-0106), NOTICE + LICENSE (SigNoz §4c attribution retained; upstream pkg files are headerless per SigNoz convention). - Reverted o11y cosmetic renames (o11y*/datastore*) and experimental originals (authz/iamauthz, billing, zapreceiver, zapmetricreceiver) to SigNoz canonical to kill the skew; snapshots retained in git history for re-layering. go build ./... = exit 0. go vet clean except benign "unreachable code" in generated ANTLR parser files (inherited from upstream).
225 lines
6.2 KiB
Go
225 lines
6.2 KiB
Go
package auditorserver
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/hanzoai/o11y/pkg/factory"
|
|
"github.com/hanzoai/o11y/pkg/instrumentation/instrumentationtest"
|
|
"github.com/hanzoai/o11y/pkg/types/audittypes"
|
|
"github.com/hanzoai/o11y/pkg/types/coretypes"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestSettings() factory.ScopedProviderSettings {
|
|
return factory.NewScopedProviderSettings(instrumentationtest.New().ToProviderSettings(), "auditorserver_test")
|
|
}
|
|
|
|
func newTestEvent(resource coretypes.Resource, action coretypes.Verb) audittypes.AuditEvent {
|
|
return audittypes.AuditEvent{
|
|
Timestamp: time.Now(),
|
|
EventName: audittypes.NewEventName(resource.Kind(), action),
|
|
AuditAttributes: audittypes.AuditAttributes{
|
|
Action: action,
|
|
Outcome: audittypes.OutcomeSuccess,
|
|
},
|
|
ResourceAttributes: audittypes.ResourceAttributes{
|
|
Resource: resource,
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 10, BatchSize: 5, FlushInterval: time.Second}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, _ []audittypes.AuditEvent) error { return nil })
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, server)
|
|
}
|
|
|
|
func TestStart_Stop(t *testing.T) {
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 10, BatchSize: 5, FlushInterval: time.Second}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, _ []audittypes.AuditEvent) error { return nil })
|
|
require.NoError(t, err)
|
|
|
|
done := make(chan error, 1)
|
|
go func() { done <- server.Start(context.Background()) }()
|
|
|
|
require.NoError(t, server.Stop(context.Background()))
|
|
|
|
select {
|
|
case err := <-done:
|
|
assert.NoError(t, err)
|
|
case <-time.After(2 * time.Second):
|
|
assert.Fail(t, "Start did not return after Stop")
|
|
}
|
|
}
|
|
|
|
func TestAdd_FlushesOnBatchSize(t *testing.T) {
|
|
var exported []audittypes.AuditEvent
|
|
var mu sync.Mutex
|
|
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 100, BatchSize: 3, FlushInterval: time.Hour}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, events []audittypes.AuditEvent) error {
|
|
mu.Lock()
|
|
exported = append(exported, events...)
|
|
mu.Unlock()
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() { _ = server.Start(ctx) }()
|
|
|
|
for i := 0; i < 3; i++ {
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceDashboard, coretypes.VerbCreate))
|
|
}
|
|
|
|
assert.Eventually(t, func() bool {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return len(exported) == 3
|
|
}, 2*time.Second, 10*time.Millisecond)
|
|
|
|
require.NoError(t, server.Stop(ctx))
|
|
}
|
|
|
|
func TestAdd_FlushesOnInterval(t *testing.T) {
|
|
var exported atomic.Int64
|
|
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 100, BatchSize: 1000, FlushInterval: 50 * time.Millisecond}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, events []audittypes.AuditEvent) error {
|
|
exported.Add(int64(len(events)))
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() { _ = server.Start(ctx) }()
|
|
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceUser, coretypes.VerbUpdate))
|
|
|
|
assert.Eventually(t, func() bool {
|
|
return exported.Load() == 1
|
|
}, 2*time.Second, 10*time.Millisecond)
|
|
|
|
require.NoError(t, server.Stop(ctx))
|
|
}
|
|
|
|
func TestAdd_DropsWhenBufferFull(t *testing.T) {
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 2, BatchSize: 100, FlushInterval: time.Hour}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, _ []audittypes.AuditEvent) error { return nil })
|
|
require.NoError(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceDashboard, coretypes.VerbCreate))
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceDashboard, coretypes.VerbUpdate))
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceDashboard, coretypes.VerbDelete))
|
|
|
|
assert.Equal(t, 2, server.queueLen())
|
|
}
|
|
|
|
func TestStop_DrainsRemainingEvents(t *testing.T) {
|
|
var exported atomic.Int64
|
|
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 100, BatchSize: 100, FlushInterval: time.Hour}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, events []audittypes.AuditEvent) error {
|
|
exported.Add(int64(len(events)))
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() { _ = server.Start(ctx) }()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceRule, coretypes.VerbCreate))
|
|
}
|
|
|
|
require.NoError(t, server.Stop(ctx))
|
|
|
|
assert.Equal(t, int64(5), exported.Load())
|
|
}
|
|
|
|
func TestAdd_ContinuesAfterExportFailure(t *testing.T) {
|
|
var calls atomic.Int64
|
|
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 100, BatchSize: 2, FlushInterval: time.Hour}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, _ []audittypes.AuditEvent) error {
|
|
calls.Add(1)
|
|
return errors.New(errors.TypeInternal, errors.CodeInternal, "connection refused")
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() { _ = server.Start(ctx) }()
|
|
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceUser, coretypes.VerbDelete))
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceUser, coretypes.VerbDelete))
|
|
|
|
assert.Eventually(t, func() bool {
|
|
return calls.Load() >= 1
|
|
}, 2*time.Second, 10*time.Millisecond)
|
|
|
|
require.NoError(t, server.Stop(ctx))
|
|
}
|
|
|
|
func TestAdd_ConcurrentSafety(t *testing.T) {
|
|
var exported atomic.Int64
|
|
|
|
settings := newTestSettings()
|
|
config := Config{BufferSize: 1000, BatchSize: 10, FlushInterval: 50 * time.Millisecond}
|
|
|
|
server, err := New(settings, config, func(_ context.Context, events []audittypes.AuditEvent) error {
|
|
exported.Add(int64(len(events)))
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go func() { _ = server.Start(ctx) }()
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 100; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
server.Add(ctx, newTestEvent(coretypes.ResourceMetaResourceDashboard, coretypes.VerbCreate))
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
|
|
require.NoError(t, server.Stop(ctx))
|
|
|
|
assert.Equal(t, int64(100), exported.Load())
|
|
}
|