Timestamp Granularity to microseconds

Not applicable

Hello All,

Apigee Edge has a "system.timestamp" context variable which provides millisecond precision. We may have a requirement to stamp some operations with a timestamp with the precision of *microseconds*. Is there any way to be able to change the default precision to microseconds, or a way to easily implement this into our flows?

TIA, Steve

Solved Solved
2 1 1,334
1 ACCEPTED SOLUTION

Yes - but you need to write just a little code to do it. Apigee Edge offers the ability for you to write your own "custom policies" using Java or Javascript. In Java, there is the System.nanoTime() call which provides nanosecond precision, but not necessarily nanosecond resolution. So what you'd need to do is write a callout that simply calls System.nanoTime() and puts that value into a context variable.

The full source code of the Java callout would look something like this:

package com.dinochiesa.edgecallouts;

import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;
import com.apigee.flow.execution.ExecutionContext;

public class Nanotime implements Execution {

    public Nanotime() { }

    public ExecutionResult execute (final MessageContext msgCtxt,
                                    final ExecutionContext execContext) {
        long nano = System.nanoTime();
        // set a variable.
        msgCtxt.setVariable("nano.time", Long.toString(nano));
        return ExecutionResult.SUCCESS;
    }
}

After inserting that policy into your flow, you'd have a nanosecond value in the nano.time context variable.

If you want microseconds, just divide by 1000 inside the Java callout.

Here is a github repo containing the full source of the Java callout (it's not much source!) as well as a demonstration API Proxy.

Good luck!

View solution in original post

1 REPLY 1

Yes - but you need to write just a little code to do it. Apigee Edge offers the ability for you to write your own "custom policies" using Java or Javascript. In Java, there is the System.nanoTime() call which provides nanosecond precision, but not necessarily nanosecond resolution. So what you'd need to do is write a callout that simply calls System.nanoTime() and puts that value into a context variable.

The full source code of the Java callout would look something like this:

package com.dinochiesa.edgecallouts;

import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;
import com.apigee.flow.execution.ExecutionContext;

public class Nanotime implements Execution {

    public Nanotime() { }

    public ExecutionResult execute (final MessageContext msgCtxt,
                                    final ExecutionContext execContext) {
        long nano = System.nanoTime();
        // set a variable.
        msgCtxt.setVariable("nano.time", Long.toString(nano));
        return ExecutionResult.SUCCESS;
    }
}

After inserting that policy into your flow, you'd have a nanosecond value in the nano.time context variable.

If you want microseconds, just divide by 1000 inside the Java callout.

Here is a github repo containing the full source of the Java callout (it's not much source!) as well as a demonstration API Proxy.

Good luck!