Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-241 Support changing metrics-level for KclMessageDrivenChannelAdapter #242

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,8 @@
import software.amazon.kinesis.lifecycle.events.ProcessRecordsInput;
import software.amazon.kinesis.lifecycle.events.ShardEndedInput;
import software.amazon.kinesis.lifecycle.events.ShutdownRequestedInput;
import software.amazon.kinesis.metrics.MetricsConfig;
import software.amazon.kinesis.metrics.MetricsLevel;
import software.amazon.kinesis.processor.FormerStreamsLeasesDeletionStrategy;
import software.amazon.kinesis.processor.MultiStreamTracker;
import software.amazon.kinesis.processor.RecordProcessorCheckpointer;
Expand Down Expand Up @@ -92,6 +94,7 @@
* @author Artem Bilan
* @author Dirk Bonhomme
* @author Siddharth Jain
* @author Minkyu Moon
*
* @since 2.2.0
*/
Expand Down Expand Up @@ -145,6 +148,8 @@ public class KclMessageDrivenChannelAdapter extends MessageProducerSupport

private volatile Scheduler scheduler;

private MetricsLevel metricsLevel = MetricsLevel.DETAILED;

public KclMessageDrivenChannelAdapter(String... streams) {
this(KinesisAsyncClient.create(), CloudWatchAsyncClient.create(), DynamoDbAsyncClient.create(), streams);
}
Expand Down Expand Up @@ -267,6 +272,16 @@ public void setFanOut(boolean fanOut) {
this.fanOut = fanOut;
}

/**
* Specify a metrics level to emit.
* Defaults to {@link MetricsLevel#DETAILED}.
artembilan marked this conversation as resolved.
Show resolved Hide resolved
* @param metricsLevel the {@link MetricsLevel} for emitting (or not) metrics into Cloud Watch.
*/
public void setMetricsLevel(MetricsLevel metricsLevel) {
Assert.notNull(metricsLevel, "'metricsLevel' must not be null");
this.metricsLevel = metricsLevel;
artembilan marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected void onInit() {
super.onInit();
Expand Down Expand Up @@ -321,13 +336,16 @@ protected void doStart() {
.glueSchemaRegistryDeserializer(this.glueSchemaRegistryDeserializer)
.retrievalSpecificConfig(retrievalSpecificConfig);

MetricsConfig metricsConfig = this.config.metricsConfig();
metricsConfig.metricsLevel(this.metricsLevel);

this.scheduler =
new Scheduler(
this.config.checkpointConfig(),
this.config.coordinatorConfig(),
this.config.leaseManagementConfig(),
lifecycleConfig,
this.config.metricsConfig(),
metricsConfig,
this.config.processorConfig(),
retrievalConfig);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.integration.test.util.TestUtils;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
Expand All @@ -42,6 +43,7 @@
import org.springframework.messaging.PollableChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import software.amazon.kinesis.metrics.MetricsLevel;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -66,6 +68,9 @@ public class KclMessageDrivenChannelAdapterTests implements LocalstackContainerT
@Autowired
private PollableChannel kinesisReceiveChannel;

@Autowired
private KclMessageDrivenChannelAdapter kclMessageDrivenChannelAdapter;

@BeforeAll
static void setup() {
AMAZON_KINESIS = LocalstackContainerTest.kinesisClient();
Expand Down Expand Up @@ -116,6 +121,21 @@ void kclChannelAdapterReceivesRecords() {
assertThat(streamConsumers).hasSize(0);
}

@Test
public void metricsLevelOfMetricsFactoryShouldBeSetToMetricsLevelOfAdapter() {
MetricsLevel metricsLevel = TestUtils.getPropertyValue(
kclMessageDrivenChannelAdapter,
"scheduler.metricsFactory.metricsLevel",
MetricsLevel.class
);
MetricsLevel expectedMetricsLevel = TestUtils.getPropertyValue(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do this. There is just enough to compare with the MetricsLevel.NONE.

kclMessageDrivenChannelAdapter,
"metricsLevel",
MetricsLevel.class
);
assertThat(metricsLevel).isEqualTo(expectedMetricsLevel);
}

@Configuration
@EnableIntegration
public static class TestConfiguration {
Expand All @@ -130,6 +150,7 @@ public KclMessageDrivenChannelAdapter kclMessageDrivenChannelAdapter() {
adapter.setConverter(String::new);
adapter.setConsumerGroup("single_stream_group");
adapter.setFanOut(false);
adapter.setMetricsLevel(MetricsLevel.fromName("NONE"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just enough to use MetricsLevel.NONE.

adapter.setBindSourceRecord(true);
return adapter;
}
Expand Down