Skip to content

Commit

Permalink
Add debug utility function (#225)
Browse files Browse the repository at this point in the history
* Add debug utility

* Add some documentation into contributing.md
  • Loading branch information
samiwelthomasHO committed Sep 23, 2022
1 parent 06d8a1a commit 43f4c95
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,37 @@ To edit the payload just change the template, if you think there is anything mis
config or SLI specs that you need for the payload then raise the issue with us, since that would
be a breaking change.

# Debugging

While developing the jsonnet code it can be helpful to write out a trace to inspect the value of
objects along the way.

We have provided a convenience utility function `debug` that will write the TRACE output using the
`std.trace` function from the standard library but also returns the traced object so that the
debug can be added in place.

It is important to bare in mind that the `debug` function will only be run if the expression is evaluated
due to the lazy-loading nature of jsonnet.

Example usage:

This **will** work:

```
{
local test = [1,2,3];
value: debug(test),
}
```

This **will not** work since test is never used/evaluated.

```
{
local test = debug({});
}
```

# Testing

TBC
10 changes: 10 additions & 0 deletions monitoring-as-code/src/util/debug.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// A function that writes a TRACE output of the object and returns the object itself.
// @param obj Any serialisable object
// @returns obj the passed in object
local debug(obj) = (
std.trace(std.toString(obj), obj)
);

{
debug(obj): debug(obj)
}

0 comments on commit 43f4c95

Please sign in to comment.