-
Notifications
You must be signed in to change notification settings - Fork 67
fix(traceloop-sdk): Add csv and json support to experiment #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { Polly, setupMocha as setupPolly } from "@pollyjs/core"; | ||
| import NodeHttpAdapter from "@pollyjs/adapter-node-http"; | ||
| import FetchAdapter from "@pollyjs/adapter-fetch"; | ||
| import FSPersister from "@pollyjs/persister-fs"; | ||
| import * as traceloop from "../src"; | ||
| import * as assert from "assert"; | ||
|
|
||
| // Register adapters and persisters | ||
| Polly.register(NodeHttpAdapter); | ||
| Polly.register(FetchAdapter); | ||
| Polly.register(FSPersister); | ||
|
|
||
| describe("Experiment Export Tests", () => { | ||
| let polly: Polly; | ||
| let client: traceloop.TraceloopClient; | ||
| let experimentSlug: string; | ||
| let runId: string; | ||
|
|
||
| setupPolly({ | ||
| adapters: ["node-http", "fetch"], | ||
| persister: "fs", | ||
| recordIfMissing: process.env.RECORD_MODE === "NEW", | ||
| recordFailedRequests: true, | ||
| mode: process.env.RECORD_MODE === "NEW" ? "record" : "replay", | ||
| matchRequestsBy: { | ||
| headers: false, | ||
| url: { | ||
| protocol: true, | ||
| hostname: true, | ||
| pathname: true, | ||
| query: false, | ||
| }, | ||
| }, | ||
| logging: true, | ||
| }); | ||
|
|
||
| before(async function () { | ||
| const apiKey = | ||
| process.env.RECORD_MODE === "NEW" | ||
| ? process.env.TRACELOOP_API_KEY! | ||
| : "test-key"; | ||
| const baseUrl = | ||
| process.env.RECORD_MODE === "NEW" | ||
| ? process.env.TRACELOOP_BASE_URL! | ||
| : "https://api-staging.traceloop.com"; | ||
|
|
||
| client = new traceloop.TraceloopClient({ | ||
| appName: "experiment_export_test", | ||
| apiKey, | ||
| baseUrl, | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(function () { | ||
| const { server } = this.polly as Polly; | ||
| server.any().on("beforePersist", (_req, recording) => { | ||
| recording.request.headers = recording.request.headers.filter( | ||
| ({ name }: { name: string }) => | ||
| !["authorization"].includes(name.toLowerCase()), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Export Methods", () => { | ||
| it("should export experiment results as CSV with explicit parameters", async function () { | ||
| // Skip this test unless valid Polly recordings exist | ||
| if (process.env.RECORD_MODE !== "NEW") { | ||
| this.skip(); | ||
| return; | ||
| } | ||
|
|
||
| // Use known experiment slug and run ID for testing | ||
| experimentSlug = "test-experiment-slug"; | ||
| runId = "test-run-id"; | ||
|
|
||
| const csvData = await client.experiment.toCsvString( | ||
| experimentSlug, | ||
| runId, | ||
| ); | ||
|
|
||
| assert.ok(csvData); | ||
| assert.strictEqual(typeof csvData, "string"); | ||
| console.log(`✓ Exported CSV data: ${csvData.length} characters`); | ||
| }); | ||
|
|
||
| it("should export experiment results as JSON with explicit parameters", async function () { | ||
| // Skip this test unless valid Polly recordings exist | ||
| if (process.env.RECORD_MODE !== "NEW") { | ||
| this.skip(); | ||
| return; | ||
| } | ||
|
|
||
| experimentSlug = "test-experiment-slug"; | ||
| runId = "test-run-id"; | ||
|
|
||
| const jsonData = await client.experiment.toJsonString( | ||
| experimentSlug, | ||
| runId, | ||
| ); | ||
|
|
||
| assert.ok(jsonData); | ||
| assert.strictEqual(typeof jsonData, "string"); | ||
| // Verify it's valid JSON | ||
| JSON.parse(jsonData); | ||
| console.log(`✓ Exported JSON data: ${jsonData.length} characters`); | ||
| }); | ||
|
nina-kollman marked this conversation as resolved.
|
||
|
|
||
| it("should throw error when exporting CSV without experiment slug", async function () { | ||
| try { | ||
| await client.experiment.toCsvString(); | ||
| assert.fail("Should have thrown an error"); | ||
| } catch (error) { | ||
| assert.ok(error instanceof Error); | ||
| assert.ok(error.message.includes("experiment_slug is required")); | ||
| console.log("✓ Correctly threw error for missing experiment slug"); | ||
| } | ||
| }); | ||
|
|
||
| it("should throw error when exporting JSON without run ID", async function () { | ||
| try { | ||
| await client.experiment.toJsonString("some-slug"); | ||
| assert.fail("Should have thrown an error"); | ||
| } catch (error) { | ||
| assert.ok(error instanceof Error); | ||
| assert.ok(error.message.includes("run_id is required")); | ||
| console.log("✓ Correctly threw error for missing run ID"); | ||
| } | ||
| }); | ||
|
|
||
| it("should use last run values when not provided", async function () { | ||
| // This test would require running an actual experiment first | ||
| // For now, we'll just verify the error handling | ||
| try { | ||
| await client.experiment.toCsvString(); | ||
| assert.fail("Should have thrown an error"); | ||
| } catch (error) { | ||
| assert.ok(error instanceof Error); | ||
| // Should fail because no last run exists | ||
| assert.ok( | ||
| error.message.includes("experiment_slug is required") || | ||
| error.message.includes("run_id is required"), | ||
| ); | ||
| console.log("✓ Correctly handled missing last run values"); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.