Gradleに組み込まれたMicrometer Cloudwatch 1.1.3を使用していますcompile 'io.micrometer:micrometer-registry-cloudwatch:1.1.3'
Java ではCloudWatchConfig
、次のようにしてを作成できます。
CloudWatchConfig cloudWatchConfig = new CloudWatchConfig() {
@Override
public String get(String s) {
return "my-service-metrics";
}
@Override
public boolean enabled() {
return true;
}
@Override
public Duration step() {
return Duration.ofSeconds(30);
}
@Override
public int batchSize() {
return CloudWatchConfig.MAX_BATCH_SIZE;
}
};
Kotlinで同等のものは、次のようにする必要があると思います:
val cloudWatchConfig = CloudWatchConfig {
fun get(s:String) = "my-service-metrics"
fun enabled() = true
fun step() = Duration.ofSeconds(30)
fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
}
Koltin コンパイラはこれに失敗し、ブロックの最後の行を指摘します: fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
String? 型の値が必要だと言っています。
多くのデバッグの後、ステップ関数の toString を返すことでこれを修正できました。Duration によって生成されたかのように解析されるため、文字列を渡すことはできません。私の Kotlin コードが機能するようになり、次のようになります。
val cloudWatchConfig = CloudWatchConfig {
fun get(s:String) = "my-service-metrics"
fun enabled() = true
fun step() = Duration.ofSeconds(30)
fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
step().toString()
}
CloudWatchConfig、StepRegisteryConfig、および MeterRegistryConfig インターフェースを調べた後、なぜそうなのかわかりません。Koltin がこれを行うのはなぜですか? また、なぜ Duration の toString を期待しているのですか?