Skip to content

Commit b5fed49

Browse files
committed
wiring and test cases of iot-metrics
1 parent f8e30e1 commit b5fed49

15 files changed

Lines changed: 558 additions & 100 deletions

File tree

src/main/java/software/amazon/awssdk/crt/internal/IoTMetricEncoder.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import software.amazon.awssdk.crt.io.TlsContext;
1010
import software.amazon.awssdk.crt.mqtt5.Mqtt5ClientOptions;
1111
import software.amazon.awssdk.crt.mqtt5.TopicAliasingOptions;
12+
import software.amazon.awssdk.crt.io.ExponentialBackoffRetryOptions.JitterMode;
1213
import software.amazon.awssdk.crt.utils.PackageInfo;
1314

1415
import java.util.ArrayList;
@@ -65,10 +66,13 @@ private static String protocolVersionValue(boolean isMqtt5) {
6566
* Mapping: Windows (WINSOCK)->B, all other platforms (POSIX)->A.
6667
*
6768
* @return the single-character metrics value for the socket implementation
68-
* @throws CRT.UnknownPlatformException if the running platform cannot be identified
6969
*/
70-
private static String socketImplementationValue() throws CRT.UnknownPlatformException {
71-
return "windows".equals(CRT.getOSIdentifier()) ? "B" : "A";
70+
private static String socketImplementationValue() {
71+
try {
72+
return "windows".equals(CRT.getOSIdentifier()) ? "B" : "A";
73+
} catch (Exception e) {
74+
return "A";
75+
}
7276
}
7377

7478
/**
@@ -85,16 +89,14 @@ private static String httpProxyTypeValue(boolean proxyUsesTls){
8589
/**
8690
* Maps ExponentialBackoffJitterMode value to its metrics character.
8791
*
88-
* @param value the int value from JitterMode.getValue()
92+
* @param mode the JitterMode enum value
8993
* @return single-character metrics value, or null if DEFAULT (should be omitted)
9094
*/
91-
private static String retryJitterModeValue(int value) {
92-
switch (value) {
93-
case 1: return "A"; // None
94-
case 2: return "B"; // Full
95-
case 3: return "C"; // Decorrelated
96-
default: return null;
97-
}
95+
private static String retryJitterModeValue(JitterMode mode) {
96+
if (mode == JitterMode.None) return "A";
97+
if (mode == JitterMode.Full) return "B";
98+
if (mode == JitterMode.Decorrelated) return "C";
99+
return null;
98100
}
99101

100102
/**
@@ -216,7 +218,7 @@ public static String getEncodedFeatureListMqtt5(Mqtt5ClientOptions clientOptions
216218

217219
// A: retry_jitter_mode
218220
if (clientOptions.getRetryJitterMode() != null) {
219-
String val = retryJitterModeValue(clientOptions.getRetryJitterMode().getValue());
221+
String val = retryJitterModeValue(clientOptions.getRetryJitterMode());
220222
if (val != null) features.add(RETRY_JITTER_MODE + "/" + val);
221223
}
222224

src/main/java/software/amazon/awssdk/crt/mqtt/MqttClientConnection.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import software.amazon.awssdk.crt.mqtt5.Mqtt5ClientOptions;
1919
import software.amazon.awssdk.crt.mqtt5.packets.ConnectPacket;
2020
import software.amazon.awssdk.crt.internal.IoTDeviceSDKMetrics;
21+
import software.amazon.awssdk.crt.internal.IoTMetricEncoder;
2122

2223
import java.util.concurrent.CompletableFuture;
2324
import java.util.function.Consumer;
@@ -78,7 +79,7 @@ private static MqttConnectionConfig s_toMqtt3ConnectionConfig(Mqtt5ClientOptions
7879
options.setProtocolOperationTimeoutMs(mqtt5options.getAckTimeoutSeconds() != null
7980
? Math.toIntExact(mqtt5options.getAckTimeoutSeconds()) * 1000
8081
: 0);
81-
options.setMetricsEnabled(mqtt5options.getMetricsEnabled());
82+
options.setDisableMetrics(mqtt5options.getDisableMetrics());
8283
return options;
8384
}
8485

@@ -164,8 +165,15 @@ private void SetupConfig(MqttConnectionConfig config) throws MqttException {
164165
mqttClientConnectionSetLogin(getNativeHandle(), config.getUsername(), config.getPassword());
165166
}
166167

167-
if (config.getMetricsEnabled()) {
168-
mqttClientConnectionSetMetrics(getNativeHandle(), new IoTDeviceSDKMetrics());
168+
if (!config.getDisableMetrics()) {
169+
TlsContext tls = null;
170+
if (config.getMqttClient()!=null){
171+
tls = config.getMqttClient().getTlsContext();
172+
} else if (config.getMqtt5Client()!=null){
173+
tls = config.getMqtt5Client().getClientOptions().getTlsContext();
174+
}
175+
IoTDeviceSDKMetrics metrics = IoTMetricEncoder.createMetricsMqtt3(config.getMetrics(), config.getHttpProxyOptions(), tls);
176+
mqttClientConnectionSetMetrics(getNativeHandle(), metrics);
169177
}
170178

171179
if (config.getMinReconnectTimeoutSecs() != 0L && config.getMaxReconnectTimeoutSecs() != 0L) {

src/main/java/software/amazon/awssdk/crt/mqtt/MqttConnectionConfig.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import software.amazon.awssdk.crt.CrtResource;
1111
import software.amazon.awssdk.crt.http.HttpProxyOptions;
12+
import software.amazon.awssdk.crt.internal.IoTDeviceSDKMetrics;
1213
import software.amazon.awssdk.crt.io.ClientTlsContext;
1314
import software.amazon.awssdk.crt.io.SocketOptions;
1415
import software.amazon.awssdk.crt.mqtt5.Mqtt5Client;
@@ -47,7 +48,8 @@ public final class MqttConnectionConfig extends CrtResource {
4748
private Consumer<WebsocketHandshakeTransformArgs> websocketHandshakeTransform;
4849

4950
/* metrics */
50-
private boolean metricsEnabled = true;
51+
private boolean disableMetrics = false;
52+
private IoTDeviceSDKMetrics metrics = null;
5153

5254
public MqttConnectionConfig() {}
5355

@@ -542,23 +544,44 @@ public Consumer<WebsocketHandshakeTransformArgs> getWebsocketHandshakeTransform(
542544
}
543545

544546
/**
545-
* Enables or disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
547+
* Disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
548+
* Default is false (metrics enabled).
546549
*
547-
* @param enabled true to enable metrics, false to disable
550+
* @param disable true to disable metrics, false to enable (default)
548551
*/
549-
public void setMetricsEnabled(boolean enabled) {
550-
this.metricsEnabled = enabled;
552+
public void setDisableMetrics(boolean disable) {
553+
this.disableMetrics = disable;
551554
}
552555

553556
/**
554-
* Queries whether IoT Device SDK metrics collection is enabled
557+
* Returns whether IoT Device SDK metrics collection is disabled.
555558
*
556-
* @return true if metrics are enabled, false if disabled
559+
* @return true if metrics are disabled, false if metrics are enabled (default)
557560
*/
558-
public boolean getMetricsEnabled() {
559-
return metricsEnabled;
561+
public boolean getDisableMetrics() {
562+
return disableMetrics;
560563
}
561564

565+
/**
566+
* Sets the IoT SDK metrics configuration. If provided, the CRT will merge
567+
* these metrics with CRT-level metrics. If null, default CRT metrics are used.
568+
*
569+
* @param metrics metrics configuration from the IoT SDK layer
570+
*/
571+
public void setMetrics(IoTDeviceSDKMetrics metrics) {
572+
this.metrics = metrics;
573+
}
574+
575+
/**
576+
* Returns the IoT SDK metrics configuration.
577+
*
578+
* @return metrics configuration, or null if not set
579+
*/
580+
public IoTDeviceSDKMetrics getMetrics() {
581+
return metrics;
582+
}
583+
584+
562585
/**
563586
* Creates a (shallow) clone of this config object
564587
*
@@ -588,7 +611,7 @@ public MqttConnectionConfig clone() {
588611
clone.setWebsocketHandshakeTransform(getWebsocketHandshakeTransform());
589612

590613
clone.setReconnectTimeoutSecs(getMinReconnectTimeoutSecs(), getMaxReconnectTimeoutSecs());
591-
clone.setMetricsEnabled(getMetricsEnabled());
614+
clone.setDisableMetrics(getDisableMetrics());
592615

593616
// success, bump up the ref count so we can escape the try-with-resources block
594617
clone.addRef();

src/main/java/software/amazon/awssdk/crt/mqtt5/Mqtt5ClientOptions.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import software.amazon.awssdk.crt.mqtt5.packets.ConnectPacket;
1414
import software.amazon.awssdk.crt.mqtt.MqttConnectionConfig;
1515
import software.amazon.awssdk.crt.internal.IoTDeviceSDKMetrics;
16+
import software.amazon.awssdk.crt.internal.IoTMetricEncoder;
1617

1718
import java.util.Map;
1819
import java.util.function.Function;
@@ -46,12 +47,11 @@ public class Mqtt5ClientOptions {
4647
private Consumer<Mqtt5WebsocketHandshakeTransformArgs> websocketHandshakeTransform;
4748
private PublishEvents publishEvents;
4849
private TopicAliasingOptions topicAliasingOptions;
49-
// Indicates whether AWS IoT Metrics are enabled for this client, default to true.
50-
// We don't expose iotDeviceSDKMetrics in the builder, and only allow setting
51-
// metricsEnabled for now.
52-
private boolean metricsEnabled = true;
50+
// Opt-out flag for AWS IoT Metrics. When true, metrics are disabled.
51+
// Default is false (metrics enabled).
52+
private boolean disableMetrics = false;
5353
private IoTDeviceSDKMetrics iotDeviceSDKMetrics;
54-
54+
5555

5656
/**
5757
* Returns the host name of the MQTT server to connect to.
@@ -271,21 +271,22 @@ public TopicAliasingOptions getTopicAliasingOptions() {
271271
}
272272

273273
/**
274-
* Returns whether AWS IoT Device SDK metrics collection is enabled
274+
* Returns whether AWS IoT Device SDK metrics collection is disabled.
275275
*
276-
* @return true if metrics are enabled, false otherwise
276+
* @return true if metrics are disabled, false if metrics are enabled (default)
277277
*/
278-
public boolean getMetricsEnabled() {
279-
return this.metricsEnabled;
278+
public boolean getDisableMetrics() {
279+
return this.disableMetrics;
280280
}
281281

282282
/**
283-
* Enables or disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
283+
* Disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
284+
* Default is false (metrics enabled).
284285
*
285-
* @param enabled true to enable metrics, false to disable
286+
* @param disable true to disable metrics, false to enable (default)
286287
*/
287-
public void setMetricsEnabled(boolean enabled) {
288-
this.metricsEnabled = enabled;
288+
public void setDisableMetrics(boolean disable) {
289+
this.disableMetrics = disable;
289290
}
290291

291292
/**
@@ -314,8 +315,12 @@ public Mqtt5ClientOptions(Mqtt5ClientOptionsBuilder builder) {
314315
this.websocketHandshakeTransform = builder.websocketHandshakeTransform;
315316
this.publishEvents = builder.publishEvents;
316317
this.topicAliasingOptions = builder.topicAliasingOptions;
317-
this.metricsEnabled = builder.metricsEnabled;
318-
this.iotDeviceSDKMetrics = new IoTDeviceSDKMetrics();
318+
this.disableMetrics = builder.disableMetrics;
319+
if (this.disableMetrics) {
320+
this.iotDeviceSDKMetrics = null;
321+
} else {
322+
this.iotDeviceSDKMetrics = IoTMetricEncoder.createMetricsMqtt5(this, builder.metrics);
323+
}
319324
}
320325

321326
/*******************************************************************************
@@ -379,7 +384,7 @@ public interface PublishEvents {
379384
* the client will NOT automatically send the publish acknowledgement. You are responsible for calling
380385
* {@link Mqtt5Client#invokePublishAcknowledgement(Mqtt5PublishAcknowledgementControlHandle)} later.</p>
381386
*
382-
* <p>If you do not call {@code acquirePublishAcknowledgementControl()} within the callback ,
387+
* <p>If you do not call {@code acquirePublishAcknowledgementControl()} within the callback ,
383388
* the client will automatically send the publish acknowledgement after this callback returns.</p>
384389
*
385390
* @param client The client that has received the message
@@ -618,7 +623,8 @@ static final public class Mqtt5ClientOptionsBuilder {
618623
private Consumer<Mqtt5WebsocketHandshakeTransformArgs> websocketHandshakeTransform;
619624
private PublishEvents publishEvents;
620625
private TopicAliasingOptions topicAliasingOptions;
621-
private boolean metricsEnabled = true;
626+
private boolean disableMetrics = false;
627+
private IoTDeviceSDKMetrics metrics = null;
622628

623629
/**
624630
* Sets the host name of the MQTT server to connect to.
@@ -887,13 +893,26 @@ public Mqtt5ClientOptionsBuilder withTopicAliasingOptions(TopicAliasingOptions o
887893
}
888894

889895
/**
890-
* Enables or disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
896+
* Disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
897+
* Default is false (metrics enabled).
891898
*
892-
* @param enabled true to enable metrics, false to disable.
899+
* @param disable true to disable metrics, false to enable (default)
893900
* @return The Mqtt5ClientOptionsBuilder after setting the metrics option
894901
*/
895-
public Mqtt5ClientOptionsBuilder withMetricsEnabled(boolean enabled) {
896-
this.metricsEnabled = enabled;
902+
public Mqtt5ClientOptionsBuilder withDisableMetrics(boolean disable) {
903+
this.disableMetrics = disable;
904+
return this;
905+
}
906+
907+
/**
908+
* Sets the IoT SDK metrics configuration. If provided, the CRT will merge
909+
* these metrics with CRT-level metrics. If null, default CRT metrics are used.
910+
*
911+
* @param metrics metrics configuration from the IoT SDK layer
912+
* @return The Mqtt5ClientOptionsBuilder after setting the metrics
913+
*/
914+
public Mqtt5ClientOptionsBuilder withMetrics(IoTDeviceSDKMetrics metrics) {
915+
this.metrics = metrics;
897916
return this;
898917
}
899918

src/main/resources/META-INF/native-image/software.amazon.awssdk/crt/aws-crt/jni-config.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,20 @@
12721272
"fields": [
12731273
{
12741274
"name": "libraryName"
1275+
},
1276+
{
1277+
"name": "metadataEntries"
1278+
}
1279+
]
1280+
},
1281+
{
1282+
"name": "software.amazon.awssdk.crt.internal.IoTMetricsMetadata",
1283+
"fields": [
1284+
{
1285+
"name": "key"
1286+
},
1287+
{
1288+
"name": "value"
12751289
}
12761290
]
12771291
},
@@ -1330,7 +1344,7 @@
13301344
"name": "iotDeviceSDKMetrics"
13311345
},
13321346
{
1333-
"name": "metricsEnabled"
1347+
"name": "disableMetrics"
13341348
}
13351349
],
13361350
"methods": [

0 commit comments

Comments
 (0)