|
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.List; |
10 | 10 |
|
11 | | -import software.amazon.awssdk.crt.internal.IoTDeviceSDKMetrics; |
12 | | -import software.amazon.awssdk.crt.internal.IoTMetricsMetadata; |
13 | | -import software.amazon.awssdk.crt.internal.IoTMetricEncoder; |
| 11 | +import software.amazon.awssdk.crt.iot.IoTDeviceSDKMetrics; |
| 12 | +import software.amazon.awssdk.crt.iot.IoTMetricsMetadata; |
14 | 13 |
|
15 | 14 | /** |
16 | | - * Builds SDK-layer metrics for embedding in the MQTT CONNECT packet username field. |
17 | | - * Collects SDK-layer feature usage (e.g., CERTIFICATE_SOURCE) and packages it |
18 | | - * into an IoTDeviceSDKMetrics object for the CRT layer to merge with CRT features. |
| 15 | + * Provides SDK-level metadata (version info) to the CRT layer. |
| 16 | + * The CRT handles all feature detection (certificate source, TLS settings, etc.) |
| 17 | + * and embeds the combined metrics in the MQTT CONNECT packet username field. |
19 | 18 | */ |
20 | | -public class IoTSdkMetrics { |
| 19 | +class IoTSdkMetrics { |
21 | 20 |
|
22 | | - private static final String CERTIFICATE_SOURCE = "I"; |
| 21 | + /** SDK library name reported in the metrics string. */ |
| 22 | + private String sdkLibraryName = "IoTDeviceSDK/JAVA"; |
| 23 | + |
| 24 | + /** |
| 25 | + * The current version of the IoT SDK metrics format. |
| 26 | + * This must match the version expected by the CRT layer. |
| 27 | + */ |
| 28 | + private String iotSdkMetricsVersion = "1"; |
23 | 29 |
|
24 | 30 | /** |
25 | 31 | * Returns the installed SDK version string. |
| 32 | + * |
| 33 | + * <p>Attempts to read the specification version from the package manifest first, |
| 34 | + * falling back to the implementation version. Returns {@code "dev"} if the package |
| 35 | + * metadata is unavailable (e.g. when running from a source checkout without installing). |
| 36 | + * |
| 37 | + * @return a version string such as {@code "1.32.0"} or {@code "dev"} |
26 | 38 | */ |
27 | | - private static String getSdkVersion(){ |
28 | | - try{ |
| 39 | + private static String getSdkVersion() { |
| 40 | + try { |
29 | 41 | Package pkg = IoTSdkMetrics.class.getPackage(); |
30 | 42 | String version = pkg.getSpecificationVersion(); |
31 | | - if(version == null){ |
| 43 | + if (version == null) { |
32 | 44 | version = pkg.getImplementationVersion(); |
33 | 45 | } |
34 | | - if(version == null){ |
| 46 | + if (version == null) { |
35 | 47 | version = "dev"; |
36 | 48 | } |
37 | 49 | return version; |
38 | | - }catch (Exception e){ |
| 50 | + } catch (Exception e) { |
39 | 51 | return "dev"; |
40 | 52 | } |
41 | 53 | } |
42 | 54 |
|
43 | 55 | /** |
44 | | - * Encodes SDK features into "ID/Value" format. |
45 | | - * @param certificateSource the certificate method in use or null if none |
46 | | - * @return encoded feature string (e.g., "I/A"), or empty string if no feature. |
47 | | - */ |
48 | | - private static String encodedFeatureList(CertificateSource certificateSource){ |
49 | | - if(certificateSource!=null){ |
50 | | - return CERTIFICATE_SOURCE + "/" + certificateSource.getValue(); |
51 | | - } |
52 | | - return ""; |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * Builds an IoTDeviceSDKMetrics instance for CRT layer. |
57 | | - * Always include IoTSDKVersion. When a certificate source is provided, |
58 | | - * also includes IoTSDKFeature and IoTSDKMetricsVersion. |
| 56 | + * Builds the SDK-level {@link IoTDeviceSDKMetrics} payload that is passed down to the CRT layer. |
| 57 | + * |
| 58 | + * <p>The returned object carries SDK identity and the metrics format version via two metadata entries: |
| 59 | + * <ul> |
| 60 | + * <li>{@code IoTSDKVersion} — the installed SDK package version, used to identify the |
| 61 | + * SDK release on the server side.</li> |
| 62 | + * <li>{@code IoTSDKMetricsVersion} — the metrics format version this SDK supports. |
| 63 | + * The CRT only merges SDK-supplied features when this value matches the version it expects, |
| 64 | + * so bumping {@link #iotSdkMetricsVersion} should be done in lockstep with CRT changes.</li> |
| 65 | + * </ul> |
| 66 | + * |
| 67 | + * <p>The CRT layer is responsible for detecting connection-level features (protocol version, |
| 68 | + * certificate source, TLS settings, proxy type, etc.) and appending them to the metadata |
| 69 | + * before embedding the result in the MQTT CONNECT packet username field. |
59 | 70 | * |
60 | | - * @param certificateSource the certificate method used, or null for connections |
61 | | - * without client certs (websocket, custom auth) |
62 | | - * @return metrics object ready to pass to CRT via withMetrics() or setMetrics() |
| 71 | + * @return a populated {@link IoTDeviceSDKMetrics} object ready to attach to an |
| 72 | + * MQTT5 client or MQTT3 connection configuration |
63 | 73 | */ |
64 | | - public static IoTDeviceSDKMetrics buildSdkMetrics(CertificateSource certificateSource) { |
| 74 | + IoTDeviceSDKMetrics buildSdkMetrics() { |
65 | 75 | List<IoTMetricsMetadata> metadata = new ArrayList<>(); |
66 | | - |
67 | 76 | metadata.add(new IoTMetricsMetadata("IoTSDKVersion", getSdkVersion())); |
68 | | - |
69 | | - String featureList = encodedFeatureList(certificateSource); |
70 | | - if (!featureList.isEmpty()) { |
71 | | - metadata.add(new IoTMetricsMetadata("IoTSDKFeature", featureList)); |
72 | | - metadata.add(new IoTMetricsMetadata("IoTSDKMetricsVersion", |
73 | | - String.valueOf(IoTMetricEncoder.IOT_SDK_METRICS_FEATURE_VERSION))); |
74 | | - } |
75 | | - return new IoTDeviceSDKMetrics("IoTDeviceSDK/Java", metadata); |
| 77 | + metadata.add(new IoTMetricsMetadata("IoTSDKMetricsVersion", iotSdkMetricsVersion)); |
| 78 | + return new IoTDeviceSDKMetrics(sdkLibraryName, metadata); |
76 | 79 | } |
77 | 80 |
|
78 | 81 | } |
0 commit comments