Files
zeekay b9f9cdf918 feat: bump o11y fork to latest SigNoz — full re-sync, builds green (§4c retained)
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).
2026-07-02 14:20:32 -07:00

143 lines
4.4 KiB
Go

package sqlmigrator
import (
"context"
"log/slog"
"time"
"github.com/hanzoai/o11y/pkg/errors"
"github.com/uptrace/bun/migrate"
"github.com/hanzoai/o11y/pkg/factory"
"github.com/hanzoai/o11y/pkg/sqlstore"
)
var (
migrationTableName string = "migration"
migrationLockTableName string = "migration_lock"
)
type migrator struct {
settings factory.ScopedProviderSettings
config Config
migrator *migrate.Migrator
dialect string
}
func New(ctx context.Context, providerSettings factory.ProviderSettings, sqlstore sqlstore.SQLStore, migrations *migrate.Migrations, config Config) SQLMigrator {
return &migrator{
migrator: migrate.NewMigrator(
sqlstore.BunDB(),
migrations,
migrate.WithTableName(migrationTableName),
migrate.WithLocksTableName(migrationLockTableName),
// This is to ensure that the migration is marked as applied only on success. If the migration fails, no entry is made in the migration table
// and the migration will be retried.
migrate.WithMarkAppliedOnSuccess(true),
),
settings: factory.NewScopedProviderSettings(providerSettings, "github.com/hanzoai/o11y/pkg/sqlmigrator"),
config: config,
dialect: sqlstore.BunDB().Dialect().Name().String(),
}
}
func (migrator *migrator) Migrate(ctx context.Context) error {
migrator.settings.Logger().InfoContext(ctx, "starting sqlstore migrations", slog.String("dialect", migrator.dialect))
if err := migrator.migrator.Init(ctx); err != nil {
return err
}
if err := migrator.Lock(ctx); err != nil {
return err
}
defer migrator.migrator.Unlock(ctx) //nolint:errcheck
group, err := migrator.migrator.Migrate(ctx)
if err != nil {
return err
}
if group.IsZero() {
migrator.settings.Logger().InfoContext(ctx, "no new migrations to run (database is up to date)", slog.String("dialect", migrator.dialect))
return nil
}
migrator.settings.Logger().InfoContext(ctx, "migrated to", slog.String("group", group.String()), slog.String("dialect", migrator.dialect))
return nil
}
func (migrator *migrator) Check(ctx context.Context) error {
migrator.settings.Logger().InfoContext(ctx, "checking sqlstore migrations", slog.String("dialect", migrator.dialect))
migrations, err := migrator.migrator.MigrationsWithStatus(ctx)
if err != nil {
return err
}
unapplied := migrations.Unapplied()
if len(unapplied) == 0 {
migrator.settings.Logger().InfoContext(ctx, "no pending migrations (database is up to date)", slog.String("dialect", migrator.dialect))
return nil
}
names := make([]string, 0, len(unapplied))
for _, migration := range unapplied {
names = append(names, migration.Name)
}
return errors.Newf(errors.TypeNotFound, ErrCodePendingSQLMigrations, "%d pending migration(s): %v", len(unapplied), names)
}
func (migrator *migrator) Rollback(ctx context.Context) error {
if err := migrator.Lock(ctx); err != nil {
return err
}
defer migrator.migrator.Unlock(ctx) //nolint:errcheck
group, err := migrator.migrator.Rollback(ctx)
if err != nil {
return err
}
if group.IsZero() {
migrator.settings.Logger().InfoContext(ctx, "no groups to roll back", slog.String("dialect", migrator.dialect))
return nil
}
migrator.settings.Logger().InfoContext(ctx, "rolled back", slog.String("group", group.String()), slog.String("dialect", migrator.dialect))
return nil
}
func (migrator *migrator) Lock(ctx context.Context) error {
if err := migrator.migrator.Lock(ctx); err == nil {
migrator.settings.Logger().InfoContext(ctx, "acquired migration lock", slog.String("dialect", migrator.dialect))
return nil
}
timer := time.NewTimer(migrator.config.Lock.Timeout)
defer timer.Stop()
ticker := time.NewTicker(migrator.config.Lock.Interval)
defer ticker.Stop()
for {
select {
case <-timer.C:
err := errors.New(errors.TypeTimeout, errors.CodeTimeout, "timed out waiting for lock")
migrator.settings.Logger().ErrorContext(ctx, "cannot acquire lock", errors.Attr(err), slog.String("lock_timeout", migrator.config.Lock.Timeout.String()), slog.String("dialect", migrator.dialect))
return err
case <-ticker.C:
var err error
if err = migrator.migrator.Lock(ctx); err == nil {
migrator.settings.Logger().InfoContext(ctx, "acquired migration lock", slog.String("dialect", migrator.dialect))
return nil
}
migrator.settings.Logger().ErrorContext(ctx, "attempt to acquire lock failed", errors.Attr(err), slog.String("lock_interval", migrator.config.Lock.Interval.String()), slog.String("dialect", migrator.dialect))
case <-ctx.Done():
return ctx.Err()
}
}
}