Skip to content

Commit

Permalink
Updates the user_agent processor to use the EventKey. (#4628)
Browse files Browse the repository at this point in the history
Updates the user_agent processor to use the EventKey.

Signed-off-by: David Venable <[email protected]>
Co-authored-by: Karsten Schnitter <[email protected]>
  • Loading branch information
dlvenable and KarstenSchnitter authored Jul 9, 2024
1 parent 253e592 commit 1d259cf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions data-prepper-plugins/user-agent-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.github.ua-parser:uap-java:1.6.1'
implementation libs.caffeine
testImplementation project(':data-prepper-test-event')
}

jacocoTestCoverageVerification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin;
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.event.EventKey;
import org.opensearch.dataprepper.model.event.EventKeyFactory;
import org.opensearch.dataprepper.model.processor.AbstractProcessor;
import org.opensearch.dataprepper.model.processor.Processor;
import org.opensearch.dataprepper.model.record.Record;
Expand All @@ -30,12 +32,19 @@ public class UserAgentProcessor extends AbstractProcessor<Record<Event>, Record<
private static final Logger LOG = LoggerFactory.getLogger(UserAgentProcessor.class);
private final UserAgentProcessorConfig config;
private final Parser userAgentParser;
private final EventKey sourceKey;
private final EventKey targetKey;

@DataPrepperPluginConstructor
public UserAgentProcessor(final PluginMetrics pluginMetrics, final UserAgentProcessorConfig config) {
public UserAgentProcessor(
final UserAgentProcessorConfig config,
final EventKeyFactory eventKeyFactory,
final PluginMetrics pluginMetrics) {
super(pluginMetrics);
this.config = config;
this.userAgentParser = new CaffeineCachingParser(config.getCacheSize());
this.sourceKey = config.getSource();
this.targetKey = eventKeyFactory.createEventKey(config.getTarget(), EventKeyFactory.EventAction.PUT);
}

@Override
Expand All @@ -44,7 +53,7 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
final Event event = record.getData();

try {
final String userAgentStr = event.get(config.getSource(), String.class);
final String userAgentStr = event.get(sourceKey, String.class);
Objects.requireNonNull(userAgentStr);

final Client clientInfo = this.userAgentParser.parse(userAgentStr);
Expand All @@ -53,10 +62,10 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor
if (!config.getExcludeOriginal()) {
parsedUserAgent.put("original", userAgentStr);
}
event.put(config.getTarget(), parsedUserAgent);
event.put(targetKey, parsedUserAgent);
} catch (Exception e) {
LOG.error(EVENT, "An exception occurred when parsing user agent data from event [{}] with source key [{}]",
event, config.getSource(), e);
event, sourceKey, e);

final List<String> tagsOnParseFailure = config.getTagsOnParseFailure();
if (Objects.nonNull(tagsOnParseFailure) && tagsOnParseFailure.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.opensearch.dataprepper.model.event.EventKey;
import org.opensearch.dataprepper.model.event.EventKeyConfiguration;
import org.opensearch.dataprepper.model.event.EventKeyFactory;

import java.util.List;

Expand All @@ -18,7 +21,8 @@ public class UserAgentProcessorConfig {
@NotEmpty
@NotNull
@JsonProperty("source")
private String source;
@EventKeyConfiguration(EventKeyFactory.EventAction.GET)
private EventKey source;

@NotNull
@JsonProperty("target")
Expand All @@ -34,7 +38,7 @@ public class UserAgentProcessorConfig {
@JsonProperty("tags_on_parse_failure")
private List<String> tagsOnParseFailure;

public String getSource() {
public EventKey getSource() {
return source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.event.TestEventKeyFactory;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.event.Event;
import org.opensearch.dataprepper.model.event.EventKeyFactory;
import org.opensearch.dataprepper.model.event.JacksonEvent;
import org.opensearch.dataprepper.model.record.Record;

Expand All @@ -38,11 +40,13 @@ class UserAgentProcessorTest {
@Mock
private UserAgentProcessorConfig mockConfig;

private final EventKeyFactory eventKeyFactory = TestEventKeyFactory.getTestEventFactory();

@ParameterizedTest
@MethodSource("userAgentStringArguments")
public void testParsingUserAgentStrings(
String uaString, String uaName, String uaVersion, String osName, String osVersion, String osFull, String deviceName) {
when(mockConfig.getSource()).thenReturn("source");
when(mockConfig.getSource()).thenReturn(eventKeyFactory.createEventKey("source"));
when(mockConfig.getTarget()).thenReturn("user_agent");
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);

Expand All @@ -64,7 +68,7 @@ public void testParsingUserAgentStrings(
@MethodSource("userAgentStringArguments")
public void testParsingUserAgentStringsWithCustomTarget(
String uaString, String uaName, String uaVersion, String osName, String osVersion, String osFull, String deviceName) {
when(mockConfig.getSource()).thenReturn("source");
when(mockConfig.getSource()).thenReturn(eventKeyFactory.createEventKey("source"));
when(mockConfig.getTarget()).thenReturn("my_target");
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);

Expand All @@ -86,7 +90,7 @@ public void testParsingUserAgentStringsWithCustomTarget(
@MethodSource("userAgentStringArguments")
public void testParsingUserAgentStringsExcludeOriginal(
String uaString, String uaName, String uaVersion, String osName, String osVersion, String osFull, String deviceName) {
when(mockConfig.getSource()).thenReturn("source");
when(mockConfig.getSource()).thenReturn(eventKeyFactory.createEventKey("source"));
when(mockConfig.getTarget()).thenReturn("user_agent");
when(mockConfig.getExcludeOriginal()).thenReturn(true);
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);
Expand All @@ -107,8 +111,9 @@ public void testParsingUserAgentStringsExcludeOriginal(

@Test
public void testParsingWhenUserAgentStringNotExist() {
when(mockConfig.getSource()).thenReturn("bad_source");
when(mockConfig.getSource()).thenReturn(eventKeyFactory.createEventKey("bad_source"));
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);
when(mockConfig.getTarget()).thenReturn("user_agent");

final UserAgentProcessor processor = createObjectUnderTest();
final Record<Event> testRecord = createTestRecord(UUID.randomUUID().toString());
Expand All @@ -120,8 +125,9 @@ public void testParsingWhenUserAgentStringNotExist() {

@Test
public void testTagsAddedOnParseFailure() {
when(mockConfig.getSource()).thenReturn("bad_source");
when(mockConfig.getSource()).thenReturn(eventKeyFactory.createEventKey("bad_source"));
when(mockConfig.getCacheSize()).thenReturn(TEST_CACHE_SIZE);
when(mockConfig.getTarget()).thenReturn("user_agent");

final String tagOnFailure1 = UUID.randomUUID().toString();
final String tagOnFailure2 = UUID.randomUUID().toString();
Expand All @@ -138,7 +144,7 @@ public void testTagsAddedOnParseFailure() {
}

private UserAgentProcessor createObjectUnderTest() {
return new UserAgentProcessor(pluginMetrics, mockConfig);
return new UserAgentProcessor(mockConfig, eventKeyFactory, pluginMetrics);
}

private Record<Event> createTestRecord(String uaString) {
Expand Down

0 comments on commit 1d259cf

Please sign in to comment.