diff --git a/opensearch-observability/build.gradle b/opensearch-observability/build.gradle index d232d8782..8e0c7c40c 100644 --- a/opensearch-observability/build.gradle +++ b/opensearch-observability/build.gradle @@ -36,7 +36,7 @@ buildscript { classpath "org.opensearch.gradle:build-tools:${opensearch_version}" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}" classpath "org.jetbrains.kotlin:kotlin-allopen:${kotlin_version}" - classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.12.0" + classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.21.0" classpath "org.jacoco:org.jacoco.agent:0.8.5" } } @@ -141,6 +141,7 @@ configurations.all { force "org.jetbrains.kotlin:kotlin-stdlib-common:${kotlin_version}" force "com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.4" force "org.mockito:mockito-core:4.6.1" + force "org.yaml:snakeyaml:1.32" } } diff --git a/opensearch-observability/detekt.yml b/opensearch-observability/detekt.yml index b758f5d1f..2a22a8314 100644 --- a/opensearch-observability/detekt.yml +++ b/opensearch-observability/detekt.yml @@ -15,3 +15,8 @@ style: ReturnCount: active: true max: 10 +complexity: + LongMethod: + threshold: 120 + NestedBlockDepth: + threshold: 5 diff --git a/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityIndex.kt b/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityIndex.kt index f10e0d673..3363715f8 100644 --- a/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityIndex.kt +++ b/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityIndex.kt @@ -54,7 +54,6 @@ internal object ObservabilityIndex { private const val NOTEBOOKS_INDEX_NAME = ".opensearch-notebooks" private const val OBSERVABILITY_MAPPING_FILE_NAME = "observability-mapping.yml" private const val OBSERVABILITY_SETTINGS_FILE_NAME = "observability-settings.yml" - private const val MAPPING_TYPE = "_doc" private var mappingsUpdated: Boolean = false private lateinit var client: Client @@ -102,10 +101,12 @@ internal object ObservabilityIndex { log.info("$LOG_PREFIX:Index $INDEX_NAME creation Acknowledged") reindexNotebooks() } else { - throw IllegalStateException("$LOG_PREFIX:Index $INDEX_NAME creation not Acknowledged") + error("$LOG_PREFIX:Index $INDEX_NAME creation not Acknowledged") } + } catch (exception: ResourceAlreadyExistsException) { + log.warn("message: ${exception.message}") } catch (exception: Exception) { - if (exception !is ResourceAlreadyExistsException && exception.cause !is ResourceAlreadyExistsException) { + if (exception.cause !is ResourceAlreadyExistsException) { throw exception } } @@ -129,7 +130,7 @@ internal object ObservabilityIndex { if (response.isAcknowledged) { log.info("$LOG_PREFIX:Index $INDEX_NAME update mapping Acknowledged") } else { - throw IllegalStateException("$LOG_PREFIX:Index $INDEX_NAME update mapping not Acknowledged") + error("$LOG_PREFIX:Index $INDEX_NAME update mapping not Acknowledged") } this.mappingsUpdated = true } catch (exception: IndexNotFoundException) { @@ -152,11 +153,11 @@ internal object ObservabilityIndex { .refresh(true) .get() if (reindexResponse.isTimedOut) { - throw IllegalStateException("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME timed out") + error("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME timed out") } else if (reindexResponse.searchFailures.isNotEmpty()) { - throw IllegalStateException("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME failed with searchFailures") + error("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME failed with searchFailures") } else if (reindexResponse.bulkFailures.isNotEmpty()) { - throw IllegalStateException("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME failed with bulkFailures") + error("$LOG_PREFIX:Index - reindex $NOTEBOOKS_INDEX_NAME failed with bulkFailures") } else if (reindexResponse.total != reindexResponse.created + reindexResponse.updated) { throw IllegalStateException( "$LOG_PREFIX:Index - reindex number of docs created:${reindexResponse.created} + " + @@ -167,8 +168,10 @@ internal object ObservabilityIndex { "$LOG_PREFIX:Index - reindex ${reindexResponse.created} docs created " + "and ${reindexResponse.updated} docs updated in $INDEX_NAME" ) + } catch (exception: ResourceNotFoundException) { + log.warn("message: ${exception.message}") } catch (exception: Exception) { - if (exception !is ResourceNotFoundException && exception.cause !is ResourceNotFoundException) { + if (exception.cause !is ResourceNotFoundException) { throw exception } } diff --git a/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityQueryHelper.kt b/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityQueryHelper.kt index 73ae47bd8..db7b16a13 100644 --- a/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityQueryHelper.kt +++ b/opensearch-observability/src/main/kotlin/org/opensearch/observability/index/ObservabilityQueryHelper.kt @@ -113,10 +113,6 @@ internal class ObservabilityQueryHelper(private val types: EnumSet timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"},\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]}}" + "{\"name\":\"test-saved-query\",\"description\":\"test description\",\"query\":\"source=index | " + + "where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"," + + "\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > " + + "timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"}," + + "\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":" + + "\"| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]}}" val recreatedObject = createObjectFromJsonString(jsonString) { SavedQuery.parse(it) } assertEquals(sampleSavedQuery, recreatedObject) } @@ -62,7 +67,12 @@ internal class SavedQueryTests { @Test fun `SavedQuery should safely ignore extra field in json object`() { val jsonString = - "{\"name\":\"test-saved-query\",\"description\":\"test description\",\"query\":\"source=index | where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"},\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]},\"another\":\"field\"}" + "{\"name\":\"test-saved-query\",\"description\":\"test description\",\"query\":\"source=index | where" + + " utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"," + + "\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > timestamp" + + "('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"},\"selected_timestamp\"" + + ":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"| fields clientip, " + + "bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]},\"another\":\"field\"}" val recreatedObject = createObjectFromJsonString(jsonString) { SavedQuery.parse(it) } assertEquals(sampleSavedQuery, recreatedObject) } diff --git a/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/SavedVisualizationTests.kt b/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/SavedVisualizationTests.kt index ea694c95d..76ff58319 100644 --- a/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/SavedVisualizationTests.kt +++ b/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/SavedVisualizationTests.kt @@ -51,7 +51,15 @@ internal class SavedVisualizationTests { @Test fun `SavedVisualization should deserialize json object using parser`() { val jsonString = - "{\"name\":\"test-saved-visualization\",\"description\":\"test description\",\"query\":\"source=index | where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\",\"type\":\"bar\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"},\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]},\"application_id\":\"KE1Ie34BbsTr-CsB4G6Y\",\"user_configs\":\"{\\\"dataConfig\\\":\\\"{}\\\",\\\"layoutConfig\\\":\\\"{}\\\"}\",\"sub_type\":\"metric\",\"units_of_measure\":\"hours (h)\"}" + "{\"name\":\"test-saved-visualization\",\"description\":\"test description\",\"query\":\"source=index | " + + "where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"," + + "\"type\":\"bar\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time >" + + " timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"}," + + "\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"" + + "| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]}," + + "\"application_id\":\"KE1Ie34BbsTr-CsB4G6Y\",\"user_configs\":\"{\\\"dataConfig\\\":\\\"{}\\\"," + + "\\\"layoutConfig\\\":\\\"{}\\\"}\",\"sub_type\":\"metric\",\"units_of_measure\":\"hours (h)\"}" + val recreatedObject = createObjectFromJsonString(jsonString) { SavedVisualization.parse(it) } assertEquals(sampleSavedVisualization, recreatedObject) } @@ -67,7 +75,15 @@ internal class SavedVisualizationTests { @Test fun `SavedVisualization should safely ignore extra field in json object`() { val jsonString = - "{\"name\":\"test-saved-visualization\",\"description\":\"test description\",\"query\":\"source=index | where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\",\"type\":\"bar\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"},\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"| fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]},\"application_id\":\"KE1Ie34BbsTr-CsB4G6Y\",\"user_configs\":\"{\\\"dataConfig\\\":\\\"{}\\\",\\\"layoutConfig\\\":\\\"{}\\\"}\",\"sub_type\":\"metric\",\"units_of_measure\":\"hours (h)\"}" + "{\"name\":\"test-saved-visualization\",\"description\":\"test description\",\"query\":\"source=index | " + + "where utc_time > timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"," + + "\"type\":\"bar\",\"selected_date_range\":{\"start\":\"now/15m\",\"end\":\"now\",\"text\":\"utc_time > " + + "timestamp('2021-07-01 00:00:00') and utc_time < timestamp('2021-07-02 00:00:00')\"}," + + "\"selected_timestamp\":{\"name\":\"utc_time\",\"type\":\"timestamp\"},\"selected_fields\":{\"text\":\"|" + + " fields clientip, bytes, memory, host\",\"tokens\":[{\"name\":\"utc_time\",\"type\":\"timestamp\"}]}," + + "\"application_id\":\"KE1Ie34BbsTr-CsB4G6Y\",\"user_configs\":\"{\\\"dataConfig\\\":\\\"{}\\\"," + + "\\\"layoutConfig\\\":\\\"{}\\\"}\",\"sub_type\":\"metric\",\"units_of_measure\":\"hours (h)\"}" + val recreatedObject = createObjectFromJsonString(jsonString) { SavedVisualization.parse(it) } assertEquals(sampleSavedVisualization, recreatedObject) } diff --git a/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/TimestampTests.kt b/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/TimestampTests.kt index adef69b1c..1d303921d 100644 --- a/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/TimestampTests.kt +++ b/opensearch-observability/src/test/kotlin/org/opensearch/observability/model/TimestampTests.kt @@ -53,7 +53,8 @@ internal class TimestampTests { @Test fun `Timestamp should safely ignore extra field in json object`() { val jsonString = - "{\"name\":\"test-timestamp\",\"index\":\"opensearch_dashboards_sample_data_logs\",\"type\":\"timestamp\",\"dsl_type\":\"date\",\"another\":\"field\"}" + "{\"name\":\"test-timestamp\",\"index\":\"opensearch_dashboards_sample_data_logs\"," + + "\"type\":\"timestamp\",\"dsl_type\":\"date\",\"another\":\"field\"}" val recreatedObject = createObjectFromJsonString(jsonString) { Timestamp.parse(it) } assertEquals(sampleTimestamp, recreatedObject) }