| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/ipfs/kubo/test/cli/harness" |
| 13 | "github.com/ipfs/kubo/test/cli/testutils" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | ) |
| 17 | |
| 18 | var otelCollectorConfigYAML = ` |
| 19 | receivers: |
| 20 | otlp: |
| 21 | protocols: |
| 22 | grpc: |
| 23 | |
| 24 | processors: |
| 25 | batch: |
| 26 | |
| 27 | exporters: |
| 28 | file: |
| 29 | path: /traces/traces.json |
| 30 | |
| 31 | service: |
| 32 | pipelines: |
| 33 | traces: |
| 34 | receivers: [otlp] |
| 35 | processors: [batch] |
| 36 | exporters: [file] |
| 37 | ` |
| 38 | |
| 39 | func TestTracing(t *testing.T) { |
| 40 | testutils.RequiresDocker(t) |
| 41 | t.Parallel() |
| 42 | node := harness.NewT(t).NewNode().Init() |
| 43 | |
| 44 | node.WriteBytes("collector-config.yaml", []byte(otelCollectorConfigYAML)) |
| 45 | |
| 46 | // touch traces.json and give it 777 perms in case Docker runs as a different user |
| 47 | node.WriteBytes("traces.json", nil) |
| 48 | err := os.Chmod(filepath.Join(node.Dir, "traces.json"), 0o777) |
| 49 | require.NoError(t, err) |
| 50 | |
| 51 | dockerBin, err := exec.LookPath("docker") |
| 52 | require.NoError(t, err) |
| 53 | node.Runner.MustRun(harness.RunRequest{ |
| 54 | Path: dockerBin, |
| 55 | Args: []string{ |
| 56 | "run", |
| 57 | "--rm", |
| 58 | "--detach", |
| 59 | "--volume", fmt.Sprintf("%s:/config.yaml", filepath.Join(node.Dir, "collector-config.yaml")), |
| 60 | "--volume", fmt.Sprintf("%s:/traces", node.Dir), |
| 61 | "--net", "host", |
| 62 | "--name", "ipfs-test-otel-collector", |
| 63 | "otel/opentelemetry-collector-contrib:0.52.0", |
| 64 | "--config", "/config.yaml", |
| 65 | }, |
| 66 | }) |
| 67 | |
| 68 | t.Cleanup(func() { |
| 69 | node.Runner.MustRun(harness.RunRequest{ |
| 70 | Path: dockerBin, |
| 71 | Args: []string{"stop", "ipfs-test-otel-collector"}, |
| 72 | }) |
| 73 | }) |
| 74 | |
| 75 | node.Runner.Env["OTEL_TRACES_EXPORTER"] = "otlp" |
| 76 | node.Runner.Env["OTEL_EXPORTER_OTLP_PROTOCOL"] = "grpc" |
| 77 | node.Runner.Env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "http://localhost:4317" |
| 78 | node.StartDaemon() |
| 79 | defer node.StopDaemon() |
| 80 | |
| 81 | assert.Eventually(t, |
| 82 | func() bool { |
| 83 | b, err := os.ReadFile(filepath.Join(node.Dir, "traces.json")) |
| 84 | require.NoError(t, err) |
| 85 | return strings.Contains(string(b), "go-ipfs") |
| 86 | }, |
| 87 | 5*time.Minute, |
| 88 | 10*time.Millisecond, |
| 89 | ) |
| 90 | } |