Logo

dev-resources.site

for different kinds of informations.

Tracetest Tip: Testing Span Order with Assertions

Published at
10/14/2024
Categories
tracetest
opentelemetry
testing
Author
Daniel Baptista Dias
Categories
3 categories in total
tracetest
open
opentelemetry
open
testing
open
Tracetest Tip: Testing Span Order with Assertions

šŸ’” Are you not sure how OpenTelemetry instrumentation or Trace-based testing works? Click here to see more details.

When you are instrumenting services with OpenTelemetry, you want to see traces propagated from service A to service B, and check if their communication is working as expected. For instance, in the example below, a user sends data to Service A and Service A calls Service B to augment it.

You can validate the communication flow with Tracetest using a selector in the following format.

  specs:
  - selector: span[service.name="service-a"] span[service.name="service-b"]
    assertions:
    - attr:tracetest.selected_spans.count >= 1
  • Declaring the selector in this order means that it will only select spans from service-b that come after spans from service-a.
  • The assertion attr:tracetest.selected_spans.count >= 1 validates that at least one span exists with that criteria. For further details, visit the selector documentation.

Going back to the example above, you can write a test with a specific assertion to validate it.

type: Test
spec:
  id: FMqdxukHg
  name: Test if service B is called after service A
  trigger:
    type: http
    httpRequest:
      method: POST
      url: http://service-a:8800/sendData
      body: "{\n  \"some\": \"test\" \n}"
      headers:
      - key: Content-Type
        value: application/json
  specs:
  - selector: span[tracetest.span.type="http" service.name="service-a" name="POST /sendData"] 
              span[tracetest.span.type="http" service.name="service-b" name="POST /augmentData"]
    name: Service B was called after Service A
    assertions:
    - attr:tracetest.selected_spans.count >= 1

When running this test with the CLI, you should have the following result.

tracetest run test -f ./tracetest/test.yaml

# It should output something like this:

# āœ” RunGroup: #uv8yYXkNg (https://app.tracetest.io/organizations/ttorg_1cbdabae7b8fd1c6/environments/ttenv_6e983cd1e9edbecf/run/uv8yYXkNg)
#  Summary: 1 passed, 0 failed, 0 pending
#   āœ” Test if service B is called after service A (https://app.tracetest.io/organizations/ttorg_1cbdabae7b8fd1c6/environments/ttenv_6e983cd1e9edbecf/test/FMqdxukHg/run/9/test) - trace id: 008075c573faf4583f42e67c9bdb4f83
#         āœ” Service B was called after Service A

By doing this type of assertion you can validate if the dependencies are organized as intended, and even use it to validate if a trace is being propagated between services.

Hereā€™s what it looks like in the Tracetest UI.

image.png

TheĀ example sourcesĀ used in this article andĀ setup instructionsĀ are available in the Tracetest GitHub repository.

Would you like to learn more about Tracetest and what it brings to the table? Visit theĀ Tracetest docsĀ and try it out by signing up today!

Also, please feel free to join ourĀ Slack Community, giveĀ Tracetest a star on GitHub, or schedule aĀ time to chat 1:1.

Featured ones: