Skip to content

Commit 91b1b34

Browse files
add event time to configChangeEventValue (#74)
* add event time to configChangeEventValue * make event time optional * revert making event time field optional
1 parent 32a08ce commit 91b1b34

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

config-service-change-event-api/src/main/proto/org/hypertrace/config/change/event/v1/config_change_event_value.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ message ConfigChangeEventValue {
1414
}
1515
optional string user_id = 4;
1616
optional string user_name = 5;
17+
int64 event_time_millis = 6;
1718
}
1819

1920
message ConfigCreateEvent {

config-service-change-event-generator/src/main/java/org/hypertrace/config/service/change/event/impl/ConfigChangeEventGeneratorFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hypertrace.config.service.change.event.impl;
22

33
import com.typesafe.config.Config;
4+
import java.time.Clock;
45
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
56

67
public class ConfigChangeEventGeneratorFactory {
@@ -17,9 +18,10 @@ public static ConfigChangeEventGeneratorFactory getInstance() {
1718
return instance;
1819
}
1920

20-
public ConfigChangeEventGenerator createConfigChangeEventGenerator(Config appConfig) {
21+
public ConfigChangeEventGenerator createConfigChangeEventGenerator(
22+
Config appConfig, Clock clock) {
2123
if (appConfig.getBoolean(GENERIC_CONFIG_SERVICE_PUBLISH_CHANGE_EVENTS)) {
22-
return new ConfigChangeEventGeneratorImpl(appConfig);
24+
return new ConfigChangeEventGeneratorImpl(appConfig, clock);
2325
} else {
2426
return new NoopConfigChangeEventGenerator();
2527
}

config-service-change-event-generator/src/main/java/org/hypertrace/config/service/change/event/impl/ConfigChangeEventGeneratorImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.annotations.VisibleForTesting;
44
import com.google.protobuf.Value;
55
import com.typesafe.config.Config;
6+
import java.time.Clock;
67
import java.util.Optional;
78
import lombok.extern.slf4j.Slf4j;
89
import org.hypertrace.config.change.event.v1.ConfigChangeEventKey;
@@ -31,9 +32,11 @@ public class ConfigChangeEventGeneratorImpl implements ConfigChangeEventGenerato
3132

3233
private final EventProducer<ConfigChangeEventKey, ConfigChangeEventValue>
3334
configChangeEventProducer;
35+
private Clock clock;
3436

35-
ConfigChangeEventGeneratorImpl(Config appConfig) {
37+
ConfigChangeEventGeneratorImpl(Config appConfig, Clock clock) {
3638
Config config = appConfig.getConfig(EVENT_STORE);
39+
this.clock = clock;
3740
String storeType = config.getString(EVENT_STORE_TYPE_CONFIG);
3841
EventStore eventStore = EventStoreProvider.getEventStore(storeType, config);
3942
configChangeEventProducer =
@@ -45,7 +48,9 @@ public class ConfigChangeEventGeneratorImpl implements ConfigChangeEventGenerato
4548

4649
@VisibleForTesting
4750
ConfigChangeEventGeneratorImpl(
48-
EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> configChangeEventProducer) {
51+
EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> configChangeEventProducer,
52+
Clock clock) {
53+
this.clock = clock;
4954
this.configChangeEventProducer = configChangeEventProducer;
5055
}
5156

@@ -103,6 +108,7 @@ private void produceCreateNotification(
103108
ConfigCreateEvent.newBuilder()
104109
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
105110
.build());
111+
builder.setEventTimeMillis(clock.millis());
106112
populateUserDetails(requestContext, builder);
107113
configChangeEventProducer.send(
108114
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());
@@ -130,6 +136,7 @@ private void produceUpdateNotification(
130136
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))
131137
.setLatestConfigJson(ConfigProtoConverter.convertToJsonString(latestConfig))
132138
.build());
139+
builder.setEventTimeMillis(clock.millis());
133140
populateUserDetails(requestContext, builder);
134141
configChangeEventProducer.send(
135142
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());
@@ -155,6 +162,7 @@ private void produceDeleteNotification(
155162
ConfigDeleteEvent.newBuilder()
156163
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
157164
.build());
165+
builder.setEventTimeMillis(clock.millis());
158166
populateUserDetails(requestContext, builder);
159167
configChangeEventProducer.send(
160168
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());

config-service-change-event-generator/src/test/java/org/hypertrace/config/service/change/event/impl/ConfigChangeEventGeneratorFactoryTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.typesafe.config.Config;
77
import com.typesafe.config.ConfigFactory;
8+
import java.time.Clock;
89
import java.util.Map;
910
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
1011
import org.junit.jupiter.api.Test;
@@ -16,15 +17,17 @@ void createNoopConfigChangeEventGenerator() {
1617
Config config =
1718
ConfigFactory.parseMap(Map.of(GENERIC_CONFIG_SERVICE_PUBLISH_CHANGE_EVENTS, "false"));
1819
ConfigChangeEventGenerator configChangeEventGenerator =
19-
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
20+
ConfigChangeEventGeneratorFactory.getInstance()
21+
.createConfigChangeEventGenerator(config, Clock.systemUTC());
2022
assertTrue(configChangeEventGenerator instanceof NoopConfigChangeEventGenerator);
2123
}
2224

2325
@Test
2426
void createConfigChangeEventGeneratorImpl() {
2527
Config config = getEventStoreConfig();
2628
ConfigChangeEventGenerator configChangeEventGenerator =
27-
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
29+
ConfigChangeEventGeneratorFactory.getInstance()
30+
.createConfigChangeEventGenerator(config, Clock.systemUTC());
2831
assertTrue(configChangeEventGenerator instanceof ConfigChangeEventGeneratorImpl);
2932
}
3033

config-service-change-event-generator/src/test/java/org/hypertrace/config/service/change/event/impl/ConfigChangeEventGeneratorImplTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.hypertrace.config.service.change.event.impl;
22

3+
import static org.mockito.Mockito.mock;
34
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.when;
46

57
import com.google.protobuf.InvalidProtocolBufferException;
68
import com.google.protobuf.Value;
9+
import java.time.Clock;
710
import java.util.Optional;
811
import org.hypertrace.config.change.event.v1.ConfigChangeEventKey;
912
import org.hypertrace.config.change.event.v1.ConfigChangeEventValue;
@@ -29,15 +32,19 @@ class ConfigChangeEventGeneratorImplTest {
2932
private static final String TEST_CONTEXT = "test-context";
3033
private static final String TEST_VALUE = "test-value";
3134
private static final String TEST_NEW_VALUE = "test-new-value";
35+
private static final long CURRENT_TIME_MILLIS = 1000;
3236

3337
@Mock EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> eventProducer;
3438

3539
ConfigChangeEventGeneratorImpl changeEventGenerator;
3640
RequestContext requestContext;
41+
private Clock mockClock;
3742

3843
@BeforeEach
3944
void setup() {
40-
changeEventGenerator = new ConfigChangeEventGeneratorImpl(eventProducer);
45+
mockClock = mock(Clock.class);
46+
when(mockClock.millis()).thenReturn(CURRENT_TIME_MILLIS);
47+
changeEventGenerator = new ConfigChangeEventGeneratorImpl(eventProducer, mockClock);
4148
requestContext = RequestContext.forTenantId(TEST_TENANT_ID_1);
4249
}
4350

@@ -50,6 +57,7 @@ void sendCreateNotification() throws InvalidProtocolBufferException {
5057
.send(
5158
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
5259
ConfigChangeEventValue.newBuilder()
60+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
5361
.setCreateEvent(
5462
ConfigCreateEvent.newBuilder()
5563
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
@@ -65,6 +73,7 @@ void sendCreateNotificationWithNoContext() throws InvalidProtocolBufferException
6573
.send(
6674
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
6775
ConfigChangeEventValue.newBuilder()
76+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
6877
.setCreateEvent(
6978
ConfigCreateEvent.newBuilder()
7079
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
@@ -81,6 +90,7 @@ void sendDeleteNotification() throws InvalidProtocolBufferException {
8190
.send(
8291
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
8392
ConfigChangeEventValue.newBuilder()
93+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
8494
.setDeleteEvent(
8595
ConfigDeleteEvent.newBuilder()
8696
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
@@ -96,6 +106,7 @@ void sendDeleteNotificationWithNoContext() throws InvalidProtocolBufferException
96106
.send(
97107
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
98108
ConfigChangeEventValue.newBuilder()
109+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
99110
.setDeleteEvent(
100111
ConfigDeleteEvent.newBuilder()
101112
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
@@ -114,6 +125,7 @@ void sendChangeNotification() throws InvalidProtocolBufferException {
114125
.send(
115126
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
116127
ConfigChangeEventValue.newBuilder()
128+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
117129
.setUpdateEvent(
118130
ConfigUpdateEvent.newBuilder()
119131
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))
@@ -133,6 +145,7 @@ void sendChangeNotificationWithNoContext() throws InvalidProtocolBufferException
133145
.send(
134146
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
135147
ConfigChangeEventValue.newBuilder()
148+
.setEventTimeMillis(CURRENT_TIME_MILLIS)
136149
.setUpdateEvent(
137150
ConfigUpdateEvent.newBuilder()
138151
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))

config-service/src/main/java/org/hypertrace/config/service/ConfigServicesFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.grpc.BindableService;
55
import io.grpc.ManagedChannel;
66
import io.grpc.ManagedChannelBuilder;
7+
import java.time.Clock;
78
import java.util.List;
89
import org.hypertrace.alerting.config.service.EventConditionConfigServiceImpl;
910
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
@@ -33,7 +34,8 @@ public static List<BindableService> buildAllConfigServices(
3334
lifecycle.shutdownComplete().thenRun(configChannel::shutdown);
3435

3536
ConfigChangeEventGenerator configChangeEventGenerator =
36-
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
37+
ConfigChangeEventGeneratorFactory.getInstance()
38+
.createConfigChangeEventGenerator(config, Clock.systemUTC());
3739

3840
return List.of(
3941
new ConfigServiceGrpcImpl(configStore),

0 commit comments

Comments
 (0)