I’m sending a JSON payload to the Datadog DORA metrics API, which requires the timestamp to be in nanoseconds. What is the best way to format or convert time value to nanoseconds within this JSON structure (started_at & finished_at)?
{
"data": {
"attributes": {
"team": "retails",
"service": "portal",
"env": "production",
"started_at": {{issue.customfield_12138.epochMilli}},
"finished_at": {{issue.customfield_12140.epochMilli}},
"severity": "{{issue.priority.name}}",
"name": "{{issue.key}} : {{issue.summary}}"
}
}
}
Required format: 1693491974000000000
(Human-readable date: Thursday, August 31, 2023 2:26:14 PM (UTC))
Hi @mike_atrache -- Welcome to the Atlassian Community!
Are you trying this in an automation rule? If so...
When I have tried this in the past, the format() function with nano-of-second did not work in a rule. A workaround is to create a variable for the start of epoch time (i.e., start of day 1 Jan 1970) and then perform a diff() to millis format, multiplying as needed for nanoseconds.
An example for the start of epoch time would be this:
{{now.withYear(1970).withDayOfYear(1).toDateTimeAtStartOfDay}}
Then an example of the diff() would be this, assuming the variable is named varStartOfTime:
{{varStartOfTime.toDate.diff(issue.customfield_12138).millis}}
Kind regards,
Bill
Please try to stay with one thread when responding. That will help people reading the posts in the future know when there are multiple solution approaches. Thanks!
For what you tried, please note there is a missing, closing curly bracket for the started_at smart value expression, which leads to a null value and causes the error. Try adding the bracket and re-testing.
If it still does not work, there could be a racetrack timing problem building the JSON fast enough (which can occur with the Send Web Request action). The mitigation for that is to pre-build the entire JSON and store that in another variable, perhaps named varJson. Then use just {{varJson}} in the action for the custom data. This technique also helps debugging when the variable is written to the audit log to confirm the contents with the Log action.
Thanks,
Bill
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologies Bill. Will make sure to use the same thread. The issue was to include trailing 0s
This worked:
"started_at": {{varStartOfTime.toDate.diff(issue.customfield_12138).millis}}000000,
"finished_at":{{varStartOfTime.toDate.diff(issue.customfield_12140).millis}}000000,
I appreciate your help! Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I should add the variables appeared empty during testing because Jira evaluates smart values dynamically at the moment of execution. Consequently, the "Validate" feature within the Web Request configuration cannot process these variables, as they haven't been initialized in a live run.
To bypass this limitation and verify the payload, I have implemented the following:
Live Testing: Instead of the Validate button, I am triggering the rule manually to see the variables resolve in real-time.
Audit Logging: I’ve added a Log Action immediately after the Web Request. This allows us to inspect the final, rendered values in the Audit Log to ensure the nanosecond timestamps and JSON body are formatted correctly before they reach the API.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @mike_atrache and well done!
Adding the zeros is what I noted by multiplying the milliseconds to get your correct units...although directly adding the zeros as text reduces the risk of a numeric conversion switching to scientific notation :^)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Bill, thanks for responding. I may have attempted your instructions but I am not getting any values back.
These are the steps I did:
1. I created a variable varStartOfTime with value = {{now.withYear(1970).withDayOfYear(1).toDateTimeAtStartOfDay}}
2. Custom data body:
{
"data": {
"attributes": {
"team": "portals",
"service": "portal",
"env": "production",
"started_at": {{varStartOfTime.toDate.diff(issue.customfield_12138).millis},
"finished_at": {{varStartOfTime.toDate.diff(issue.customfield_12140).millis}},
"severity": "{{issue.priority.name}}",
"name": "{{issue.key}} : {{issue.summary}}"
}
}
when validating, this is the payload outcome:
{ "data": { "attributes": { "team": "portals", "service": "portal", "env": "production", "started_at": , "finished_at": , "severity": "Medium", "name": "EIM-47804 : Test123" } } }
"errors": [
{
"status": "400",
"title": "Bad Request",
"detail": "invalid character ',' looking for beginning of
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.