Skip to content

Commit

Permalink
Added timestamp and timestamp unit information on the event
Browse files Browse the repository at this point in the history
fixes gh-4032
  • Loading branch information
marcingrzejszczak committed Aug 17, 2023
1 parent c959eb3 commit 3bad57e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -85,7 +86,12 @@ void observeWithHandlers() {

Observation.Event event = Observation.Event.of("testEvent", "event for testing");
observation.event(event);
Observation.Event event2 = Observation.Event.of("testEvent", "event for testing",
System.currentTimeMillis(), TimeUnit.MILLISECONDS);
observation.event(event2);

inOrder.verify(handler).onEvent(same(event), isA(Observation.Context.class));
inOrder.verify(handler).onEvent(same(event2), isA(Observation.Context.class));

Throwable exception = new IOException("simulated");
observation.error(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1272,6 +1273,8 @@ interface Event {
*/
static Event of(String name, String contextualName) {
return new Event() {
private final long timestamp = System.currentTimeMillis();

@Override
public String getName() {
return name;
Expand All @@ -1282,9 +1285,62 @@ public String getContextualName() {
return contextualName;
}

@Override
public long getTimestamp() {
return this.timestamp;
}

@Override
public TimeUnit getTimestampTimeUnit() {
return TimeUnit.MILLISECONDS;
}

@Override
public String toString() {
return "event.name='" + getName() + "', event.contextualName='" + getContextualName() + '\'';
return "event.name='" + getName() + "', event.contextualName='" + getContextualName()
+ "', event.timestamp=" + getTimestamp() + ", event.timestampTimeUnit="
+ getTimestampTimeUnit();
}
};
}

/**
* Creates an {@link Event} for the given names and timestamp.
* @param name The name of the event (should have low cardinality).
* @param contextualName The contextual name of the event (can have high
* cardinality).
* @param time Value of time
* @param timeUnit Time unit in which value of time was provided
* @return event
*/
static Event of(String name, String contextualName, long time, TimeUnit timeUnit) {
return new Event() {

@Override
public String getName() {
return name;
}

@Override
public String getContextualName() {
return contextualName;
}

@Override
public long getTimestamp() {
return time;
}

@Override
public TimeUnit getTimestampTimeUnit() {
return timeUnit;
}

@Override
public String toString() {
return "event.name='" + getName() + "', event.contextualName='" + getContextualName()
+ "', event.timestamp=" + getTimestamp() + ", event.timestampTimeUnit="
+ getTimestampTimeUnit();
}
};
}
Expand All @@ -1304,6 +1360,22 @@ static Event of(String name) {
*/
String getName();

/**
* Returns the timestamp of the event.
* @return the timestamp of the event
*/
default long getTimestamp() {
return 0L;
}

/**
* Returns the timestamp unit.
* @return the timestamp unit
*/
default TimeUnit getTimestampTimeUnit() {
return TimeUnit.MILLISECONDS;
}

/**
* Returns the contextual name of the event. You can use {@code %s} to represent
* dynamic entries that should be resolved at runtime via
Expand Down

0 comments on commit 3bad57e

Please sign in to comment.