Skip to main content

Tutorial

In this guide we're going to use this repo's example app in the java directory to capture, transform and replay traffic with mocks.

Prerequisites​

  1. speedctl CLI is installed
  2. proxymock CLI is installed
  3. After initializing proxymock, SPEEDSCALE_HOME should be set correctly. If it is missing, run proxymock init again and allow it to update your rcfile.
  4. Clone the repo: https://github.com/speedscale/demo
  5. Java is present and JAVA_HOME is set correctly (on MacOS you can run /usr/libexec/java_home to find the correct JAVA_HOME)
  6. Install jq and make if not already on your desktop (brew install jq and brew install make on MacOS for example)

The App​

The app is a Java Spring Boot web server with authenticated endpoints that makes requests out to a few external services. The project README (at java/README.md) shows ways to run the app locally, in Docker and in Kubernetes. Speedscale can be configured to be compatible with whichever way you choose to deploy the app. Note that for this test run we'll disable DLP but you can find instructions on how to enable it near the end.

Capture​

Make sure you navigate to the java subdirectory within the demo repository.

  1. Install the operator
  2. Recommended: deploy the demo app with make kube, then follow Kubernetes capture with eBPF and Java Agent to add both capture annotations to the java-server Deployment in the default namespace and verify the agent in new pods.
    • Fallback: if eBPF is not suitable in your cluster, deploy the demo app with the Speedscale sidecar by running:
make kube-capture
  1. Validate the app was deployed properly with:
kubectl -n default get pods

Depending on your chosen capture method you may see either 1/1 (eBPF collector) or 2/2 (application + sidecar) for the java-server pod.

NAME READY STATUS RESTARTS AGE
java-client-54ccddd5cd-tthld 1/1 Running 0 37s
java-server-597577977-bthvl 2/2 Running 0 37s

This should start generating traffic that you can see in the Speedscale UI within a couple of minutes.

Analyze​

After a few minutes you should be able to see your traffic in the Speedscale dashboard. Open Traffic and select the service name that you entered in speedctl install under Service. You should see the inbound and outbound calls for this app as shown below.

The Traffic viewer filtered to java-server, listing inbound requests and outbound calls to the Treasury API and a demo API

You can also drill down into specific request-response pairs (RRPairs). For example, here is the request we make to our app to get the max interest rate for treasuries.

An inbound GET /treasury/max_interest request with its JSON response body

And we can also see the outbound request our app makes to the Treasury API to fulfill this request.

The outbound GET request to api.fiscaldata.treasury.gov, with its filter query parameter

You can and inspect it further or you can skip ahead to running a replay which will also create a snapshot as a side effect.

Replay​

We're now going to run a replay for this captured traffic.

Click Replay on the top right and walk through the wizard. All the default settings are ok here. You can also do this outside of the UI with more instructions here.

Making sense of a Replay​

If you are already viewing the snapshot you recorded, you can see your replay appear in the Replays tab. Alternatively, you can find a report for your Replay in the dashboard. It should look something like this.

The Overview of a java-server replay report, where 274 of 315 assertions passed, 98 of 98 outbound calls matched a mock and 1 of 2 goals was met

The Mocks check shows that every call our app made to third-party APIs was answered by a mock. Open Mocked Responses to see each one, such as the calls to the Treasury API. Mocks isolate your service during tests, and you can use them in other parts of your development cycle as detailed here.

The Mocked Responses tab with Mock Accuracy 98/98, listing matched calls to the SpaceX and Treasury APIs

Not every check passed. Open the Accuracy tab to see which requests failed their checks.

The Accuracy tab, where 274 of 315 checks passed, with failed rows for /spacex/ship requests and /spacex/launches

Transform​

Recorded credentials are a common reason replayed requests fail. If we were replaying in a different environment, we might need different credentials entirely for the login request. Another pattern might be a set of traffic where we don't have a login request and instead we need to resign the JWT from our captured traffic. This is where Transforms come into play and we can edit our captured data to parameterize parts of it. As an example, we can transform our snapshot to edit the password.

Find the request on the snapshot's Traffic tab and open Request, then click the pencil icon next to the field you want to edit, which in our case is the password. Choose Modify.

The pencil menu on the password field of a login request, with Modify password

This opens the transform chain on the Prepare tab. Select Add step and pick a transform.

The transform picker in the chain, with Constant selected

For now we'll just replace it with a constant but there are all sorts of options that can be chained together. Replay uses shows what the replay will send. Select Save and reanalyze to keep the change.

The finished chain, where the password becomes a constant and Replay uses shows the new request body

We just did a transform for the traffic coming into our app during a replay. We can also use transforms for mocks in case we have to parameterize fields like session ids, dates, etc. Check out this guide for a deep dive.

Assertions​

Select a failed row to see which checks failed. Here a /spacex/ship/{id} request failed the HTTP Response Content Type check, while its status code and headers matched the recording. When this traffic was recorded, the SpaceX API answered some of these requests with an error page, error code: 522, instead of JSON. The response is labeled JSON but holds plain text, so the check fails on every replay. The cause is in the recorded traffic, and our app behaves the same as when it was recorded.

A failed /spacex/ship request, where 1 of 3 checks failed: HTTP Response Content Type

There are two ways to handle failures like this. You can remove the recorded error responses from the snapshot, or you can change what the replay checks. Assertions live in the test config. Open the report's test config and select the Assertions tab.

The Assertions tab of a test config, listing the HTTP Status Code, HTTP Headers, HTTP Response Schema and HTTP Response Content Type checks for all traffic

Each assertion applies to a subset of traffic. To stop checking the content type of the SpaceX error responses, edit the HTTP Response Content Type assertion or move it to a subset that leaves out /spacex/ship/.*.

This test config has no HTTP Response Body assertion, so values that change on every request, such as the ship_id our app generates, do not fail the replay. If you add a body assertion, enter those fields under ignore so it compares only the data that should stay the same.

The Assertion dialog for HTTP Response Body, with ship_id entered under ignore

Save the test config, then reanalyze the report. When every check passes, the report looks like this.

A passed java-server replay report, where 52 of 52 assertions passed and both goals were met

Overview​

In this demo we:

  1. Captured some traffic
  2. Analyzed it in the Traffic Viewer
  3. Optionally configured DLP
  4. Ran a replay and created a snapshot
  5. Used auto generated mocks during the replay
  6. Transformed some snapshot traffic
  7. Found why assertions failed and changed the test config
  8. Reanalyzed the report for a higher accuracy

Next Steps

This is just a small subset of things you can do with Speedscale, other things to try out could be:

  1. Capture traffic from one of your own apps
  2. Replay traffic from one cluster into another
  3. Run a load test
  4. Integrate with CI/CD

Uninstall​

If you'd like to remove the demo from your environment follow these instructions:

make kube-clean

(Optional) Data loss Protection​

If we drill down into a request, we can also see data we may not want to leave our environment.

The Request Headers tab of an inbound java-server request, where the Authorization header holds a Bearer JWT, shown truncated here

You can enable DLP to redact certain fields from an RRPair at capture time. Note that this will cause your replays to have low accuracies because necessary information will be masked. Check out the dlp section for more information on DLP configuration. As a starter, you cna follow the instructions below.

Run:

speedctl infra dlp enable

Now we see the authorization header is redacted and never makes it to Speedscale.

The Request Headers tab of a request captured with DLP on, where the Authorization header value is Bearer REDACTED-UNRECOGNIZED followed by a hash

For more complex DLP configuration you can use this guide.

Loading search...