From b9c8d9af2659223bbb87483ceb6382f13f45c498 Mon Sep 17 00:00:00 2001 From: Rodrigo Villar Date: Fri, 22 Nov 2024 13:14:30 -0500 Subject: [PATCH] remove internal trace --- go.mod | 8 ++-- internal/mempool/mempool_test.go | 14 +++--- internal/trace/exporter.go | 62 ------------------------- internal/trace/exporter_type.go | 45 ------------------ internal/trace/noop.go | 20 -------- internal/trace/tracer.go | 80 -------------------------------- state/tstate/tstate_test.go | 4 +- vm/config.go | 2 +- vm/vm.go | 7 ++- 9 files changed, 17 insertions(+), 225 deletions(-) delete mode 100644 internal/trace/exporter.go delete mode 100644 internal/trace/exporter_type.go delete mode 100644 internal/trace/noop.go delete mode 100644 internal/trace/tracer.go diff --git a/go.mod b/go.mod index ba00544370..196de67c6d 100644 --- a/go.mod +++ b/go.mod @@ -20,10 +20,6 @@ require ( github.com/spf13/viper v1.12.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/otel v1.22.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 - go.opentelemetry.io/otel/sdk v1.22.0 go.opentelemetry.io/otel/trace v1.22.0 go.uber.org/atomic v1.11.0 go.uber.org/zap v1.26.0 @@ -136,7 +132,11 @@ require ( github.com/urfave/cli/v2 v2.25.7 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 // indirect go.opentelemetry.io/otel/metric v1.22.0 // indirect + go.opentelemetry.io/otel/sdk v1.22.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect go.uber.org/mock v0.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/internal/mempool/mempool_test.go b/internal/mempool/mempool_test.go index 1732a8d46f..122af95248 100644 --- a/internal/mempool/mempool_test.go +++ b/internal/mempool/mempool_test.go @@ -8,10 +8,10 @@ import ( "testing" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/trace" "github.com/stretchr/testify/require" "github.com/ava-labs/hypersdk/codec" - "github.com/ava-labs/hypersdk/internal/trace" ) var testSponsor = codec.CreateAddress(1, ids.GenerateTestID()) @@ -51,7 +51,7 @@ func TestMempool(t *testing.T) { require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) txm := New[*TestItem](tracer, 3, 16) for _, i := range []int64{100, 200, 300, 400} { @@ -69,7 +69,7 @@ func TestMempool(t *testing.T) { func TestMempoolAddDuplicates(t *testing.T) { require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) txm := New[*TestItem](tracer, 3, 16) // Generate item item := GenerateTestItem(testSponsor, 300) @@ -89,7 +89,7 @@ func TestMempoolAddExceedMaxSponsorSize(t *testing.T) { // Sponsor2 is exempt from max size require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) sponsor := codec.CreateAddress(4, ids.GenerateTestID()) // Non exempt sponsors max of 4 txm := New[*TestItem](tracer, 20, 4) @@ -105,7 +105,7 @@ func TestMempoolAddExceedMaxSponsorSize(t *testing.T) { func TestMempoolAddExceedMaxSize(t *testing.T) { require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) txm := New[*TestItem](tracer, 3, 20) // Add more tx's than txm.maxSize @@ -133,7 +133,7 @@ func TestMempoolAddExceedMaxSize(t *testing.T) { func TestMempoolRemoveTxs(t *testing.T) { require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) txm := New[*TestItem](tracer, 3, 20) // Add @@ -151,7 +151,7 @@ func TestMempoolRemoveTxs(t *testing.T) { func TestMempoolSetMinTimestamp(t *testing.T) { require := require.New(t) ctx := context.TODO() - tracer, _ := trace.New(&trace.Config{Enabled: false}) + tracer, _ := trace.New(trace.Config{Enabled: false}) txm := New[*TestItem](tracer, 20, 20) // Add more tx's than txm.maxSize diff --git a/internal/trace/exporter.go b/internal/trace/exporter.go deleted file mode 100644 index f0b3800b59..0000000000 --- a/internal/trace/exporter.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package trace - -import ( - "context" - "time" - - "go.opentelemetry.io/otel/exporters/otlp/otlptrace" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" - - sdktrace "go.opentelemetry.io/otel/sdk/trace" -) - -const tracerProviderExportCreationTimeout = 5 * time.Second - -type ExporterConfig struct { - Type ExporterType `json:"type"` - - // Endpoint to send metrics to - Endpoint string `json:"endpoint"` - - // Headers to send with metrics - Headers map[string]string `json:"headers"` - - // If true, don't use TLS - Insecure bool `json:"insecure"` -} - -func newExporter(config ExporterConfig) (sdktrace.SpanExporter, error) { - var client otlptrace.Client - switch config.Type { - case GRPC: - opts := []otlptracegrpc.Option{ - otlptracegrpc.WithEndpoint(config.Endpoint), - otlptracegrpc.WithHeaders(config.Headers), - otlptracegrpc.WithTimeout(tracerExportTimeout), - } - if config.Insecure { - opts = append(opts, otlptracegrpc.WithInsecure()) - } - client = otlptracegrpc.NewClient(opts...) - case HTTP: - opts := []otlptracehttp.Option{ - otlptracehttp.WithEndpoint(config.Endpoint), - otlptracehttp.WithHeaders(config.Headers), - otlptracehttp.WithTimeout(tracerExportTimeout), - } - if config.Insecure { - opts = append(opts, otlptracehttp.WithInsecure()) - } - client = otlptracehttp.NewClient(opts...) - default: - return nil, errUnknownExporterType - } - - ctx, cancel := context.WithTimeout(context.Background(), tracerProviderExportCreationTimeout) - defer cancel() - return otlptrace.New(ctx, client) -} diff --git a/internal/trace/exporter_type.go b/internal/trace/exporter_type.go deleted file mode 100644 index 8c80e6e7d7..0000000000 --- a/internal/trace/exporter_type.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package trace - -import ( - "errors" - "fmt" - "strings" -) - -const ( - GRPC ExporterType = iota + 1 - HTTP -) - -var errUnknownExporterType = errors.New("unknown exporter type") - -func ExporterTypeFromString(exporterTypeStr string) (ExporterType, error) { - switch strings.ToLower(exporterTypeStr) { - case GRPC.String(): - return GRPC, nil - case HTTP.String(): - return HTTP, nil - default: - return 0, fmt.Errorf("%w: %q", errUnknownExporterType, exporterTypeStr) - } -} - -type ExporterType byte - -func (t ExporterType) MarshalJSON() ([]byte, error) { - return []byte(`"` + t.String() + `"`), nil -} - -func (t ExporterType) String() string { - switch t { - case GRPC: - return "grpc" - case HTTP: - return "http" - default: - return "unknown" - } -} diff --git a/internal/trace/noop.go b/internal/trace/noop.go deleted file mode 100644 index 066eb79591..0000000000 --- a/internal/trace/noop.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package trace - -import ( - "github.com/ava-labs/avalanchego/trace" - "go.opentelemetry.io/otel/trace/noop" -) - -var Noop trace.Tracer = noOpTracer{} - -// noOpTracer is an implementation of trace.Tracer that does nothing. -type noOpTracer struct { - noop.Tracer -} - -func (noOpTracer) Close() error { - return nil -} diff --git a/internal/trace/tracer.go b/internal/trace/tracer.go deleted file mode 100644 index 3bc4d10c8f..0000000000 --- a/internal/trace/tracer.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package trace - -import ( - "context" - "time" - - "github.com/ava-labs/avalanchego/trace" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/sdk/resource" - - sdktrace "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.4.0" - oteltrace "go.opentelemetry.io/otel/trace" -) - -const ( - tracerExportTimeout = 10 * time.Second - // [tracerProviderShutdownTimeout] is longer than [tracerExportTimeout] so - // in-flight exports can finish before the tracer provider shuts down. - tracerProviderShutdownTimeout = 15 * time.Second -) - -type Config struct { - ExporterConfig - // Used to flag if tracing should be performed - Enabled bool `json:"enabled"` - - // The fraction of traces to sample. - // If >= 1 always samples. - // If <= 0 never samples. - TraceSampleRate float64 `json:"traceSampleRate"` - - AppName string `json:"appName"` - Agent string `json:"agent"` - Version string `json:"version"` -} - -type tracer struct { - oteltrace.Tracer - - tp *sdktrace.TracerProvider -} - -func (t *tracer) Close() error { - ctx, cancel := context.WithTimeout(context.Background(), tracerProviderShutdownTimeout) - defer cancel() - return t.tp.Shutdown(ctx) -} - -func New(config *Config) (trace.Tracer, error) { - if !config.Enabled { - return &noOpTracer{}, nil - } - - exporter, err := newExporter(config.ExporterConfig) - if err != nil { - return nil, err - } - - tracerProviderOpts := []sdktrace.TracerProviderOption{ - sdktrace.WithBatcher(exporter, sdktrace.WithExportTimeout(tracerExportTimeout)), - sdktrace.WithResource( - resource.NewWithAttributes( - semconv.SchemaURL, - attribute.String("version", config.Version), - semconv.ServiceNameKey.String(config.Agent), - ), - ), - sdktrace.WithSampler(sdktrace.TraceIDRatioBased(config.TraceSampleRate)), - } - - tracerProvider := sdktrace.NewTracerProvider(tracerProviderOpts...) - return &tracer{ - Tracer: tracerProvider.Tracer(config.AppName), - tp: tracerProvider, - }, nil -} diff --git a/state/tstate/tstate_test.go b/state/tstate/tstate_test.go index 5c03a55da3..4fa064d8a2 100644 --- a/state/tstate/tstate_test.go +++ b/state/tstate/tstate_test.go @@ -10,12 +10,12 @@ import ( "github.com/ava-labs/avalanchego/database" "github.com/ava-labs/avalanchego/database/memdb" + "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/utils/maybe" "github.com/ava-labs/avalanchego/utils/units" "github.com/ava-labs/avalanchego/x/merkledb" "github.com/stretchr/testify/require" - "github.com/ava-labs/hypersdk/internal/trace" "github.com/ava-labs/hypersdk/keys" "github.com/ava-labs/hypersdk/state" ) @@ -540,7 +540,7 @@ func TestCreateView(t *testing.T) { ctx := context.TODO() ts := New(10) - tracer, err := trace.New(&trace.Config{Enabled: false}) + tracer, err := trace.New(trace.Config{Enabled: false}) require.NoError(err) db, err := merkledb.New(ctx, memdb.New(), merkledb.Config{ BranchFactor: merkledb.BranchFactor16, diff --git a/vm/config.go b/vm/config.go index 8f12195227..a69a0d1d7b 100644 --- a/vm/config.go +++ b/vm/config.go @@ -7,11 +7,11 @@ import ( "encoding/json" "time" + "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/utils/profiler" "github.com/ava-labs/avalanchego/utils/units" "github.com/ava-labs/hypersdk/chain" - "github.com/ava-labs/hypersdk/internal/trace" ) type Config struct { diff --git a/vm/vm.go b/vm/vm.go index 01874d4bf4..70a7b0e6d0 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -19,6 +19,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/consensus/snowman" "github.com/ava-labs/avalanchego/snow/engine/common" + "github.com/ava-labs/avalanchego/trace" "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/profiler" "github.com/ava-labs/avalanchego/utils/set" @@ -38,7 +39,6 @@ import ( "github.com/ava-labs/hypersdk/internal/gossiper" "github.com/ava-labs/hypersdk/internal/mempool" "github.com/ava-labs/hypersdk/internal/pebble" - "github.com/ava-labs/hypersdk/internal/trace" "github.com/ava-labs/hypersdk/internal/validators" "github.com/ava-labs/hypersdk/internal/validitywindow" "github.com/ava-labs/hypersdk/internal/workers" @@ -48,7 +48,6 @@ import ( "github.com/ava-labs/hypersdk/utils" avacache "github.com/ava-labs/avalanchego/cache" - avatrace "github.com/ava-labs/avalanchego/trace" avautils "github.com/ava-labs/avalanchego/utils" internalfees "github.com/ava-labs/hypersdk/internal/fees" ) @@ -104,7 +103,7 @@ type VM struct { authEngine map[uint8]AuthEngine network *p2p.Network - tracer avatrace.Tracer + tracer trace.Tracer mempool *mempool.Mempool[*chain.Transaction] // We cannot use a map here because we may parse blocks up in the ancestry @@ -247,7 +246,7 @@ func (vm *VM) Initialize( snowCtx.Log.Info("initialized hypersdk config", zap.Any("config", vm.config)) // Setup tracer - vm.tracer, err = trace.New(&vm.config.TraceConfig) + vm.tracer, err = trace.New(vm.config.TraceConfig) if err != nil { return err }