get.porter.sh/porter@v1.3.0/tests/integration/telemetry_test.go (about) 1 //go:build integration 2 3 package integration 4 5 import ( 6 "context" 7 "fmt" 8 "os" 9 "path/filepath" 10 "testing" 11 "time" 12 13 "get.porter.sh/porter/pkg/porter" 14 "get.porter.sh/porter/tests" 15 "get.porter.sh/porter/tests/tester" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 "github.com/uwu-tools/magex/shx" 19 ) 20 21 // Validate that we can configure a live connection to a telemetry endpoint 22 func TestTelemetrySetup(t *testing.T) { 23 test, err := tester.NewTest(t) 24 defer test.Close() 25 require.NoError(t, err, "test setup failed") 26 27 ctx := context.Background() 28 _, _, err = test.RunPorter("install", "otel-jaeger", "-r=ghcr.io/getporter/examples/otel-jaeger:v0.1.1", "--allow-docker-host-access") 29 require.NoError(t, err) 30 defer test.RunPorter("uninstall", "otel-jaeger", "--allow-docker-host-access") 31 32 // Wait until the collection should be up 33 time.Sleep(10 * time.Second) 34 35 // Try to run porter with telemetry enabled 36 p := porter.New() 37 38 defer p.Close() 39 os.Setenv("PORTER_EXPERIMENTAL", "structured-logs") 40 os.Setenv("PORTER_TELEMETRY_ENABLED", "true") 41 os.Setenv("PORTER_TELEMETRY_PROTOCOL", "grpc") 42 os.Setenv("PORTER_TELEMETRY_INSECURE", "true") 43 defer func() { 44 os.Unsetenv("PORTER_EXPERIMENTAL") 45 os.Unsetenv("PORTER_TELEMETRY_ENABLED") 46 os.Unsetenv("PORTER_TELEMETRY_PROTOCOL") 47 os.Unsetenv("PORTER_TELEMETRY_INSECURE") 48 }() 49 ctx, err = p.Connect(ctx) 50 require.NoError(t, err, "error initializing porter") 51 52 ctx, log := p.StartRootSpan(ctx, t.Name()) 53 defer log.Close() 54 assert.True(t, log.IsTracingEnabled()) 55 } 56 57 // Test that telemetry data is being exported both from porter and the plugins 58 func TestTelemetry_IncludesPluginLogs(t *testing.T) { 59 // I am always using require, so that we stop immediately upon an error 60 // A long test is hard to debug when it fails in the middle and keeps going 61 test, err := tester.NewTestWithConfig(t, "tests/testdata/config/config-with-telemetry.yaml") 62 defer test.Close() 63 require.NoError(t, err, "test setup failed") 64 65 // Enable telemetry 66 err = shx.Copy(filepath.Join(test.RepoRoot, "tests/testdata/config/config-with-telemetry.yaml"), filepath.Join(test.PorterHomeDir, "config.yaml")) 67 require.NoError(t, err, "error copying config file into PORTER_HOME") 68 69 // Make a call that will call a plugin 70 _, output, err := test.RunPorter("list") 71 fmt.Println(output) 72 require.NoError(t, err, "porter list failed") 73 74 // Read the traces generated for that call 75 tracesDir := filepath.Join(test.PorterHomeDir, "traces") 76 traces, err := os.ReadDir(tracesDir) 77 require.NoError(t, err, "error getting a list of the traces directory in PORTER_HOME") 78 require.Len(t, traces, 2, "expected 2 trace files to be exported") 79 80 // Validate we have trace data for porter (files are returned in descending order, which is why we know which to read first) 81 porterTraceName := filepath.Join(tracesDir, traces[1].Name()) 82 porterTrace, err := os.ReadFile(porterTraceName) 83 require.NoError(t, err, "error reading porter's trace file %s", porterTraceName) 84 tests.RequireOutputContains(t, string(porterTrace), `{"Key":"service.name","Value":{"Type":"STRING","Value":"porter"}}`, "no spans for porter were exported") 85 86 // Validate we have trace data for porter 87 pluginTraceName := filepath.Join(tracesDir, traces[0].Name()) 88 require.Contains(t, pluginTraceName, "storage.porter.mongodb", "expected the plugin trace to be for the mongodb plugin") 89 pluginTrace, err := os.ReadFile(pluginTraceName) 90 require.NoError(t, err, "error reading the plugin's trace file %s", pluginTraceName) 91 tests.RequireOutputContains(t, string(pluginTrace), `{"Key":"service.name","Value":{"Type":"STRING","Value":"storage.porter.mongodb"}}`, "no spans for the plugins were exported") 92 }