Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

3.0 KB
Raw Blame
package nic

import (
	"log/slog"
	"runtime"
	"testing"

	sdkmetric "go.opentelemetry.io/otel/sdk/metric"
	"go.opentelemetry.io/otel/sdk/metric/metricdata"

	"github.com/openlit/openlit/opentelemetry-gpu-collector/internal/config"
)

func TestNewCollectorRegisters(t *testing.T) {
	reader := sdkmetric.NewManualReader()
	provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
	defer provider.Shutdown(t.Context())

	cfg := &config.Config{
		NICEnabled:          true,
		RDMAEnabled:         true,
		NetInterfaceExclude: []string{"lo", "lo0"},
	}
	c, err := NewCollector(provider, cfg, slog.Default())
	if err != nil {
		t.Fatalf("NewCollector: %v", err)
	}
	defer c.Close()

	var rm metricdata.ResourceMetrics
	if err := reader.Collect(t.Context(), &rm); err != nil {
		t.Fatalf("Collect: %v", err)
	}

	// On non-Linux stubs (or hosts with no matching ifaces), the callback may
	// observe nothing — instruments then omit from the export. Registration
	// success is the guarantee; Linux hosts with real NICs should export.
	if runtime.GOOS != "linux" {
		return
	}

	want := map[string]bool{
		"hw.network.io":                     false,
		"hw.network.packets":                false,
		"hw.network.up":                     false,
		"hw.errors":                         false,
		"hw.network.rdma.io":                false,
		"hw.network.rdma.packets":           false,
		"hw.network.rdma.congestion.events": false,
	}
	for _, sm := range rm.ScopeMetrics {
		if sm.Scope.Name != "otelcol.nic" {
			continue
		}
		for _, m := range sm.Metrics {
			if _, ok := want[m.Name]; ok {
				want[m.Name] = true
			}
		}
	}
	for name, found := range want {
		if !found {
			t.Logf("metric %q not present (no matching interfaces this host?)", name)
		}
	}
}

func TestObserveRDMALaneWidth(t *testing.T) {
	reader := sdkmetric.NewManualReader()
	provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
	defer provider.Shutdown(t.Context())

	cfg := &config.Config{NICEnabled: true, RDMAEnabled: true}
	c, err := NewCollector(provider, cfg, slog.Default())
	if err != nil {
		t.Fatalf("NewCollector: %v", err)
	}
	defer c.Close()

	// Directly exercise RDMA observation path with a synthetic snapshot.
	base := []attributeKV{
		{Key: "hw.id", Value: "ib0"},
		{Key: "hw.name", Value: "ib0"},
		{Key: "hw.type", Value: "network"},
	}
	_ = base
	_ = c
	if rdmaLaneWidth != 4 {
		t.Fatalf("rdmaLaneWidth = %d, want 4", rdmaLaneWidth)
	}
	if got := uint64(100) * rdmaLaneWidth; got != 400 {
		t.Fatalf("scaled = %d", got)
	}
}

func TestUint64ToInt64(t *testing.T) {
	n, ok := uint64ToInt64(42)
	if !ok || n != 42 {
		t.Fatalf("got %d ok=%v", n, ok)
	}
	if _, ok := uint64ToInt64(^uint64(0)); ok {
		t.Fatal("max uint64 should not fit in int64")
	}
	n, ok = uint64ToInt64Scaled(100, rdmaLaneWidth)
	if !ok || n != 400 {
		t.Fatalf("scaled got %d ok=%v", n, ok)
	}
	if _, ok := uint64ToInt64Scaled(^uint64(0)/2, rdmaLaneWidth); ok {
		t.Fatal("overflowing scale should fail")
	}
}

// attributeKV avoids importing otel attribute solely for the lane-width check.
type attributeKV struct {
	Key, Value string
}