Skip to content

Commit

Permalink
Merge pull request #5 from solo-io/rolds/test-metrics
Browse files Browse the repository at this point in the history
Metrics for test cases
  • Loading branch information
ryanrolds authored Dec 19, 2024
2 parents 80a05ac + 24d6bfc commit 5d5e141
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
pull_request:
branches:
- main
- '*'

jobs:
build:
Expand Down
228 changes: 143 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,146 @@

[![Tests](https://github.com/mdelapenya/junit2otlp/actions/workflows/tests.yml/badge.svg)](https://github.com/mdelapenya/junit2otlp/actions/workflows/tests.yml)

This simple CLI, written in Go, is sending jUnit metrics to a back-end using [Open Telemetry](https://opentelemetry.io).
This simple CLI, written in Go, is sending jUnit metrics to a back-end using [OpenTelemetry](https://opentelemetry.io).

> Inspired by https://github.com/axw/test2otlp, which sends traces and spans for `go test` JSON events as they occur.
## Background
As jUnit represents a de-facto standard for test results in every programming language, this tool consumes the XML files produced by the test runner (or a tool converting to xUnit format), sending metrics to one or more open-source or commercial back-ends with Open Telemetry.

As jUnit represents a de-facto standard for test results in every programming language, this tool consumes the XML files produced by the test runner (or a tool converting to xUnit format), sending metrics to one or more open-source or commercial back-ends with OpenTelemetry.

## Configuration

This tool is able to override the following attributes:

| Attribute | Flag | Default value | Description |
| --------- | ---- | ------------- | ----------- |
| Repository Path | --repository-path | `.` | Path to the SCM repository to be read. |
| Service Name | --service-name | `junit2otlp` | Overrides OpenTelemetry's service name. If the `OTEL_SERVICE_NAME` environment variable is set, it will take precedence over any other value. |
| Service Version | --service-version | Empty | Overrides OpenTelemetry's service version. If the `OTEL_SERVICE_VERSION` environment variable is set, it will take precedence over any other value. |
| Trace Name | --trace-name | `junit2otlp` | Overrides OpenTelemetry's trace name. |
| Properties Allowed | --properties-allowed | All | Comma separated list of properties to be allowed in the jUnit report. |
| Skip Sending Traces | --skip-traces | `false` | Skip sending traces to the OpenTelemetry collector. |
| Skip Sending Metrics | --skip-metrics | `false` | Skip sending metrics to the OpenTelemetry collector. |

For using this tool in a distributed tracing scenario, where there is a parent trace in which the test reports traces should be attached, it's important to set the `TRACEPARENT` environment variable, so that the traces and spans generated by this tool are located under the right parent trace. Please read more on this [here](https://github.com/open-telemetry/opentelemetry-specification/issues/740).

For further reference on environment variables in the OpenTelemetry SDK, please read the [official specification](https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/)

## Traces

[Traces](https://opentelemetry.io/docs/concepts/signals/traces/) are sent to the OpenTelemetry collector, representing the test execution. Each run of the tool will create a root trace that contains spans for each suite. Each suite will contain spans for each test case.

## Metrics

[Metrics](https://opentelemetry.io/docs/concepts/signals/metrics/) are sent to the OpenTelemetry collector, representing the test execution. Each run of the tool will create a set of metrics for the test execution, including the number of failed, errored, skipped, and passed tests, the total number of tests, and the duration of the test execution.

| Metric | Description |
| --------- | ----------- |
| `tests.suite.failed` | Number of failed tests in the test execution |
| `tests.suite.error` | Number of errored tests in the test execution |
| `tests.suite.skipped` | Number of skipped tests in the test execution |
| `tests.suite.passed` | Number of passed tests in the test execution |
| `tests.suite.total` | Total number of tests in the test execution |
| `tests.suite.duration` | Duration of the test execution |
| `tests.suite.duration.histogram` | Histogram of the test execution duration |
| `tests.case.failed` | The test failed |
| `tests.case.error` | The test errored |
| `tests.case.skipped` | The test errored |
| `tests.case.passed` | The test passed |
| `tests.case.duration` | Duration of the test execution |
| `tests.case.duration.histogram` | Histogram of the test execution duration |

## OpenTelemetry Attributes

[Attributes](https://opentelemetry.io/docs/specs/otel/common/#attribute) are added to the traces, spans and metrics sent to the OpenTelemetry collector. They are used to provide context to the test execution, and to correlate the test execution with the authors of the test suite and test cases.

The attributes are divided into five categories:

- Runtime attributes
- Ownership attributes
- Report properties
- Test suite attributes
- Test case attributes

### Runtime attributes

Runtime attributes are added to the root trace, spans, and metrics sent by the tool.

| Attribute | Description |
| --------- | ----------- |
| `host.arch` | Architecture of the host where the test execution is processed |
| `os.name` | Name of the OS where the test execution is processed |
| `service.name` | Name of the service where the test execution is processed |
| `service.version` | Version of the service where the test execution is processed |

### Ownership attributes

These attributes are added to the traces, spans and metrics, identifying the owner (or owners) of the test suite, trying to correlate a test failure with an author or authors. To identify the owner, the tool will inspect the SCM repository for the project.

#### SCM attributes

Because the XML test report is evaluated for a project **in a SCM repository**, the tool will add the following attributes to each trace and span:

| Attribute | Description |
| --------- | ----------- |
| `scm.authors` | Array of unique Email addresses for the authors of the commits |
| `scm.baseRef` | Name of the target branch (Only for change requests) |
| `scm.branch` | Name of the branch where the test execution is processed |
| `scm.committers` | Array of unique Email addresses for the committers of the commits |
| `scm.provider` | Optional. If present, will include the name of the SCM provider, such as Github, Gitlab, Bitbucket, etc. |
| `scm.repository` | Array of unique URLs representing the repository (i.e. https://github.com/mdelapenya/junit2otlp) |
| `scm.type` | Type of the SCM (i.e. git, svn, mercurial) At this moment the tool only supports Git repositories. |

#### Change request attributes

The tool will add the following attributes to each trace, span, and metric if and only if the XML test report is evaluated in the context of a change requests **for a Git repository**:

| Attribute | Description |
| --------- | ----------- |
| `scm.git.additions` | Number of added lines in the changeset |
| `scm.git.deletions` | Number of deleted lines in the changeset |
| `scm.git.clone.depth` | Depth of the git clone |
| `scm.git.clone.shallow` | Whethere the git clone was shallow or not |
| `scm.git.files.modified` | Number of modified files in the changeset |

A changeset is calculated based on the HEAD commit and the first ancestor between HEAD and the branch where the changeset is submitted against.

### Report properties

The jUnit XML report can contain properties at different levels. The tool will add the properties to the testsuite and testcase spans automatically. If the `--properties-allowed` flag is set, only the properties listed in the flag will be added to the spans.

### Test suite attributes

For each test suite in the test execution, the tool will add the following attributes to the span document representing the test suite:

| Attribute | Spans | Metrics | Description |
| --------- | ----- | ------- | ----------- |
| `code.namespace` | x | x | Class/module of the test suite |
| `tests.suite.suitename` | x | x | Name of the test suite |
| `tests.suite.duration` | x | | Duration of the test suite |
| `tests.suite.systemerr` | x | | Log produced by Systemerr |
| `tests.suite.systemout` | x | | Log produced by Systemout |

### Test case attributes

For each test case in the test execution, the tool will add the following attributes to the span document representing the test case:

| Attribute | Spans | Metrics | Description |
| --------- | ----- | ------- | ----------- |
| `code.namespace` | x | x | Class/module of the test suite |
| `code.function` | x | x | Function or method of the test case |
| `tests.suite.suitename` | x | x | Name of the test suite |
| `tests.case.classname` | x | x | Classname or file for the test case |
| `tests.case.duration` | x | | Duration of the test case |
| `tests.case.error` | x | | Error message of the test case |
| `tests.case.message` | x | | Message of the test case |
| `tests.case.status` | x | | Status of the test case |
| `tests.case.systemerr` | x | | Log produced by Systemerr |
| `tests.case.systemout` | x | | Log produced by Systemout |

## Supported CI runners

This tool will work in the context of a CI runner, such as a Github action, a Jenkins job, a Gitlab runner, or even a local execution. This is important because it will use the context of the CI execution to infer the attributes to be added to the OpenTelemetry traces and spans.

In particular the order of evaluation to detect the right execution context is the following:
Expand All @@ -19,6 +151,7 @@ In particular the order of evaluation to detect the right execution context is t
```

### Local execution

It reads the environment variables that are avaible in the context of a local execution, representing the fallback if no context is discovered:

```golang
Expand Down Expand Up @@ -73,6 +206,7 @@ func FromGithub() *ScmContext {
```

### Jenkins multibranch pipelines

It reads the environment variables that are avaible in the context of a Jenkins multibranch pipeline execution:

```golang
Expand Down Expand Up @@ -109,6 +243,7 @@ func FromJenkins() *ScmContext {
```

### Gitlab Runners

It reads the environment variables that are avaible in the context of a Gitlab runner execution:

```golang
Expand Down Expand Up @@ -136,90 +271,8 @@ func FromGitlab() *ScmContext {
}
```

## OpenTelemetry configuration
This tool is able to override the following attributes:

| Attribute | Flag | Default value | Description |
| --------- | ---- | ------------- | ----------- |
| Repository Path | --repository-path | `.` | Path to the SCM repository to be read. |
| Service Name | --service-name | `junit2otlp` | Overrides OpenTelemetry's service name. If the `OTEL_SERVICE_NAME` environment variable is set, it will take precedence over any other value. |
| Service Version | --service-version | Empty | Overrides OpenTelemetry's service version. If the `OTEL_SERVICE_VERSION` environment variable is set, it will take precedence over any other value. |
| Trace Name | --trace-name | `junit2otlp` | Overrides OpenTelemetry's trace name. |
| Properties Allowed | --properties-allowed | All | Comma separated list of properties to be allowed in the jUnit report. |
| Skip Sending Traces | --skip-traces | `false` | Skip sending traces to the OpenTelemetry collector. |
| Skip Sending Metrics | --skip-metrics | `false` | Skip sending metrics to the OpenTelemetry collector. |

For using this tool in a distributed tracing scenario, where there is a parent trace in which the test reports traces should be attached, it's important to set the `TRACEPARENT` environment variable, so that the traces and spans generated by this tool are located under the right parent trace. Please read more on this [here](https://github.com/open-telemetry/opentelemetry-specification/issues/740).

For further reference on environment variables in the OpenTelemetry SDK, please read the [official specification](https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/)

## OpenTelemetry Attributes
This tool is going to parse the XML report produced by jUnit, or any other tool converting to that format, adding different attributes, separated by different categories:

- Test metrics attributes
- Ownership attributes

### Metrics and Traces
The following attributes are added as metrics and/or traces.

#### Test execution attributes
For each test execution, represented by a test report file, the tool will add the following attributes to the metric document, including them in the trace representing the test execution.

| Attribute | Description |
| --------- | ----------- |
| `tests.suite.failed` | Number of failed tests in the test execution |
| `tests.suite.error` | Number of errored tests in the test execution |
| `tests.suite.passed` | Number of passed tests in the test execution |
| `tests.suite.skipped` | Number of skipped tests in the test execution |
| `tests.suite.duration` | Duration of the test execution |
| `tests.suite.suitename` | Name of the test execution |
| `tests.suite.systemerr` | Log produced by Systemerr |
| `tests.suite.systemout` | Log produced by Systemout |
| `tests.suite.total` | Total number of tests in the test execution |

#### Test case attributes
For each test case in the test execution, the tool will add the following attributes to the span document representing the test case:

| Attribute | Description |
| --------- | ----------- |
| `tests.case.classname` | Classname or file for the test case |
| `tests.case.duration` | Duration of the test case |
| `tests.case.error` | Error message of the test case |
| `tests.case.message` | Message of the test case |
| `tests.case.status` | Status of the test case |
| `tests.case.systemerr` | Log produced by Systemerr |
| `tests.case.systemout` | Log produced by Systemout |

### Ownership attributes
These attributes are added to the traces and spans sent by the tool, identifying the owner (or owners) of the test suite, trying to correlate a test failure with an author or authors. To identify the owner, the tool will inspect the SCM repository for the project.

#### SCM attributes
Because the XML test report is evaluated for a project **in a SCM repository**, the tool will add the following attributes to each trace and span:

| Attribute | Description |
| --------- | ----------- |
| `scm.authors` | Array of unique Email addresses for the authors of the commits |
| `scm.baseRef` | Name of the target branch (Only for change requests) |
| `scm.branch` | Name of the branch where the test execution is processed |
| `scm.committers` | Array of unique Email addresses for the committers of the commits |
| `scm.provider` | Optional. If present, will include the name of the SCM provider, such as Github, Gitlab, Bitbucket, etc. |
| `scm.repository` | Array of unique URLs representing the repository (i.e. https://github.com/mdelapenya/junit2otlp) |
| `scm.type` | Type of the SCM (i.e. git, svn, mercurial) At this moment the tool only supports Git repositories. |

#### Change request attributes
The tool will add the following attributes to each trace and span if and only if the XML test report is evaluated in the context of a change requests **for a Git repository**:

| Attribute | Description |
| --------- | ----------- |
| `scm.git.additions` | Number of added lines in the changeset |
| `scm.git.deletions` | Number of deleted lines in the changeset |
| `scm.git.clone.depth` | Depth of the git clone |
| `scm.git.clone.shallow` | Whethere the git clone was shallow or not |
| `scm.git.files.modified` | Number of modified files in the changeset |

A changeset is calculated based on the HEAD commit and the first ancestor between HEAD and the branch where the changeset is submitted against.

## Docker image

It's possible to run the binary as a Docker image. To build and use the image

1. First build the Docker image using this Make goal:
Expand All @@ -242,6 +295,7 @@ cat TEST-sample3.xml | docker run --rm -i --network elastic_junit2otlp --volume
- We are passing command line flags to the container, setting the service name (_DOCKERFOO_) and the trace name (_TRACEBAR_).

## Demos

To demonstrate how traces and metrics are sent to different back-ends, we are provising the following demos:

- Elastic
Expand All @@ -250,6 +304,7 @@ To demonstrate how traces and metrics are sent to different back-ends, we are pr
- Zipkin

### Elastic

It will use the Elastic Stack as back-end, sending the traces, spans and metrics through the APM Server, storing them in Elasticsearch and finally using Kibana as visualisation layer.

```shell
Expand All @@ -262,6 +317,7 @@ open http://localhost:5601/app/apm/services?rangeFrom=now-15m&rangeTo=now&compar
```

### Jaeger

It will use Jaeger as back-end, sending the traces, spans and metrics through the OpenTelemetry collector, storing them in memory.

```shell
Expand All @@ -274,6 +330,7 @@ open http://localhost:16686
```

### Prometheus

It will use Prometheus as back-end, sending the traces, spans and metrics through the OpenTelemetry collector, storing them in memory.

```shell
Expand All @@ -286,6 +343,7 @@ open http://localhost:9090
```

### Zipkin

It will use Prometheus as back-end, sending the traces, spans and metrics through the OpenTelemetry collector, storing them in memory.

```shell
Expand Down
Loading

0 comments on commit 5d5e141

Please sign in to comment.