diff --git a/Datadog/E2ETests/E2ETests.swift b/Datadog/E2ETests/E2ETests.swift index bbcf2bc39c..cc519ed789 100644 --- a/Datadog/E2ETests/E2ETests.swift +++ b/Datadog/E2ETests/E2ETests.swift @@ -31,23 +31,24 @@ class E2ETests: XCTestCase { /// - common performance monitor - measures `Datadog.initialize(...)` performance: /// ```apm + /// $feature = core /// $monitor_id = sdk_initialize_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - sdk_initialize: has a high average execution time" - /// $monitor_query = "avg(last_1d):avg:trace.sdk_initialize{env:instrumentation,resource_name:sdk_initialize,service:com.datadog.ios.nightly} > 0.016" + /// $monitor_query = "avg(last_1d):avg:trace.perf_measure{env:instrumentation,resource_name:sdk_initialize,service:com.datadog.ios.nightly} > 0.016" /// $monitor_threshold = 0.016 /// ``` // MARK: - Measuring Performance with APM - /// Measures time of execution for given `block` - sends it as a `Span` with a given name. - func measure(spanName: String, _ block: () -> Void) { + /// Measures time of execution for given `block` - sends it as a `"perf_measure"` `Span` with a given resource name. + func measure(resourceName: String, _ block: () -> Void) { let start = Date() block() let stop = Date() - Global.sharedTracer - .startRootSpan(operationName: spanName, startTime: start) - .finish(at: stop) + let performanceSpan = Global.sharedTracer.startRootSpan(operationName: "perf_measure", startTime: start) + performanceSpan.setTag(key: DDTags.resource, value: resourceName) + performanceSpan.finish(at: stop) } // MARK: - SDK Lifecycle diff --git a/Datadog/E2ETests/Logging/LoggerBuilderE2ETests.swift b/Datadog/E2ETests/Logging/LoggerBuilderE2ETests.swift index 57459a1a15..d3cd002123 100644 --- a/Datadog/E2ETests/Logging/LoggerBuilderE2ETests.swift +++ b/Datadog/E2ETests/Logging/LoggerBuilderE2ETests.swift @@ -18,9 +18,10 @@ class LoggerBuilderE2ETests: E2ETests { /// - common performance monitor - measures `Logger.builder.build()` performance: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_initialize_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_initialize: has a high average execution time" - /// $monitor_query = "avg(last_1d):avg:trace.logs_logger_initialize{env:instrumentation,resource_name:logs_logger_initialize,service:com.datadog.ios.nightly} > 0.016" + /// $monitor_query = "avg(last_1d):avg:trace.perf_measure{env:instrumentation,resource_name:logs_logger_initialize,service:com.datadog.ios.nightly} > 0.016" /// $monitor_threshold = 0.016 /// ``` @@ -35,7 +36,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly.custom @test_method_name:logs_logger_builder_set_service_name\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_set_SERVICE_NAME() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.set(serviceName: "com.datadog.ios.nightly.custom").build() } @@ -51,7 +52,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_set_logger_name @logger.name:custom_logger_name\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_set_LOGGER_NAME() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.set(loggerName: "custom_logger_name").build() } @@ -67,7 +68,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_send_network_info_enabled @network.client.reachability:*\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_SEND_NETWORK_INFO_enabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.sendNetworkInfo(true).build() } @@ -85,7 +86,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_logger_builder_SEND_NETWORK_INFO_disabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.sendNetworkInfo(false).build() } @@ -103,7 +104,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_send_logs_to_datadog_enabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_SEND_LOGS_TO_DATADOG_enabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.sendLogsToDatadog(true).build() } @@ -121,7 +122,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_logger_builder_SEND_LOGS_TO_DATADOG_disabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.sendLogsToDatadog(false).build() } @@ -137,7 +138,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_print_logs_to_console_enabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_PRINT_LOGS_TO_CONSOLE_enabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.printLogsToConsole(true, usingFormat: .random()).build() } @@ -153,7 +154,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_print_logs_to_console_disabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_PRINT_LOGS_TO_CONSOLE_disabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.printLogsToConsole(false, usingFormat: .random()).build() } @@ -171,7 +172,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_bundle_with_rum_enabled @application_id:* @session_id:*\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_BUNDLE_WITH_RUM_enabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.bundleWithRUM(true).build() } @@ -193,7 +194,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_logger_builder_BUNDLE_WITH_RUM_disabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.bundleWithRUM(false).build() } @@ -215,7 +216,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_bundle_with_trace_enabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_BUNDLE_WITH_TRACE_enabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.bundleWithTrace(true).build() } @@ -235,7 +236,7 @@ class LoggerBuilderE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_logger_builder_bundle_with_trace_disabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_logger_builder_BUNDLE_WITH_TRACE_disabled() { - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.bundleWithTrace(false).build() } diff --git a/Datadog/E2ETests/Logging/LoggerE2ETests.swift b/Datadog/E2ETests/Logging/LoggerE2ETests.swift index 10368af145..97a70306b6 100644 --- a/Datadog/E2ETests/Logging/LoggerE2ETests.swift +++ b/Datadog/E2ETests/Logging/LoggerE2ETests.swift @@ -32,12 +32,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_debug_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_debug_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_debug_log{env:instrumentation,resource_name:logs_logger_debug_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_debug_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_DEBUG_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.debug(.mockRandom(), attributes: DD.logAttributes()) } } @@ -53,12 +54,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_debug_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_debug_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_debug_log_with_error{env:instrumentation,resource_name:logs_logger_debug_log_with_error*,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_debug_log_with_error*,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_DEBUG_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.debug(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -74,12 +76,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_info_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_info_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_info_log{env:instrumentation,resource_name:logs_logger_info_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_info_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_INFO_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.info(.mockRandom(), attributes: DD.logAttributes()) } } @@ -95,12 +98,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_info_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_info_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_info_log_with_error{env:instrumentation,resource_name:logs_logger_info_log_with_error,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_info_log_with_error,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_INFO_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.info(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -116,12 +120,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_notice_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_notice_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_notice_log{env:instrumentation,resource_name:logs_logger_notice_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_notice_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_NOTICE_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.notice(.mockRandom(), attributes: DD.logAttributes()) } } @@ -137,12 +142,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_notice_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_notice_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_notice_log_with_error{env:instrumentation,resource_name:logs_logger_notice_log_with_error,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_notice_log_with_error,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_NOTICE_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.notice(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -158,12 +164,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_warn_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_warn_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_warn_log{env:instrumentation,resource_name:logs_logger_warn_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_warn_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_WARN_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.warn(.mockRandom(), attributes: DD.logAttributes()) } } @@ -179,12 +186,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_warn_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_warn_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_warn_log_with_error{env:instrumentation,resource_name:logs_logger_warn_log_with_error,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_warn_log_with_error,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_WARN_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.warn(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -200,12 +208,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_error_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_error_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_error_log{env:instrumentation,resource_name:logs_logger_error_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_error_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_ERROR_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.error(.mockRandom(), attributes: DD.logAttributes()) } } @@ -221,12 +230,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_error_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_error_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_error_log_with_error{env:instrumentation,resource_name:logs_logger_error_log_with_error,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_error_log_with_error,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_ERROR_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.error(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -242,12 +252,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_critical_log_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_critical_log: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_critical_log{env:instrumentation,resource_name:logs_logger_critical_log,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_critical_log,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_CRITICAL_log() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.critical(.mockRandom(), attributes: DD.logAttributes()) } } @@ -263,12 +274,13 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_critical_log_with_error_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_critical_log_with_error: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_critical_log_with_error{env:instrumentation,resource_name:logs_logger_critical_log_with_error,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_critical_log_with_error,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_CRITICAL_log_with_error() { - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.critical(.mockRandom(), error: ErrorMock(.mockRandom()), attributes: DD.logAttributes()) } } @@ -286,14 +298,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_string_attribute_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_string_attribute: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_string_attribute{env:instrumentation,resource_name:logs_logger_add_string_attribute,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_string_attribute,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_STRING_attribute() { let attribute = DD.specialStringAttribute() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.addAttribute(forKey: attribute.key, value: attribute.value) } @@ -311,14 +324,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_int_attribute_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_int_attribute: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_int_attribute{env:instrumentation,resource_name:logs_logger_add_int_attribute,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_int_attribute,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_INT_attribute() { let attribute = DD.specialIntAttribute() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.addAttribute(forKey: attribute.key, value: attribute.value) } @@ -336,14 +350,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_double_attribute_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_double_attribute: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_double_attribute{env:instrumentation,resource_name:logs_logger_add_double_attribute,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_double_attribute,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_DOUBLE_attribute() { let attribute = DD.specialDoubleAttribute() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.addAttribute(forKey: attribute.key, value: attribute.value) } @@ -361,14 +376,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_bool_attribute_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_bool_attribute: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_bool_attribute{env:instrumentation,resource_name:logs_logger_add_bool_attribute,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_bool_attribute,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_BOOL_attribute() { let attribute = DD.specialBoolAttribute() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.addAttribute(forKey: attribute.key, value: attribute.value) } @@ -388,9 +404,10 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_remove_attribute_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_remove_attribute: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_remove_attribute{env:instrumentation,resource_name:logs_logger_remove_attribute,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_remove_attribute,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_remove_attribute() { let possibleAttributes = [ @@ -403,7 +420,7 @@ class LoggerE2ETests: E2ETests { logger.addAttribute(forKey: randomAttribute.key, value: randomAttribute.value) - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.removeAttribute(forKey: randomAttribute.key) } @@ -423,14 +440,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_tag_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_tag: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_tag{env:instrumentation,resource_name:logs_logger_add_tag,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_tag,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_tag() { let tag = DD.specialTag() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.addTag(withKey: tag.key, value: tag.value) } @@ -448,14 +466,15 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_add_already_formatted_tag_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_add_already_formatted_tag: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_add_already_formatted_tag{env:instrumentation,resource_name:logs_logger_add_already_formatted_tag,service:com.datadog.ios.nightly} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,resource_name:logs_logger_add_already_formatted_tag,service:com.datadog.ios.nightly} > 0.024" /// ``` func test_logs_logger_add_already_formatted_tag() { let tag = DD.specialTag() - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.add(tag: "\(tag.key):\(tag.value)") } @@ -475,15 +494,16 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_remove_tag_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_remove_tag: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_remove_tag{env:instrumentation,service:com.datadog.ios.nightly,resource_name:logs_logger_remove_tag} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,service:com.datadog.ios.nightly,resource_name:logs_logger_remove_tag} > 0.024" /// ``` func test_logs_logger_remove_tag() { let tag = DD.specialTag() logger.addTag(withKey: tag.key, value: tag.value) - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.removeTag(withKey: tag.key) } @@ -501,15 +521,16 @@ class LoggerE2ETests: E2ETests { /// /// - performance monitor: /// ```apm + /// $feature = logs /// $monitor_id = logs_logger_remove_already_formatted_tag_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - logs_logger_remove_already_formatted_tag: has a high average execution time" - /// $monitor_query = "avg(last_1d):p50:trace.logs_logger_remove_already_formatted_tag{env:instrumentation,service:com.datadog.ios.nightly,resource_name:logs_logger_remove_already_formatted_tag} > 0.024" + /// $monitor_query = "avg(last_1d):p50:trace.perf_measure{env:instrumentation,service:com.datadog.ios.nightly,resource_name:logs_logger_remove_already_formatted_tag} > 0.024" /// ``` func test_logs_logger_remove_already_formatted_tag() { let tag = DD.specialTag() logger.add(tag: "\(tag.key):\(tag.value)") - measure(spanName: DD.PerfSpanName.fromCurrentMethodName()) { + measure(resourceName: DD.PerfSpanName.fromCurrentMethodName()) { logger.remove(tag: "\(tag.key):\(tag.value)") } diff --git a/Datadog/E2ETests/Logging/LoggingConfigurationE2ETests.swift b/Datadog/E2ETests/Logging/LoggingConfigurationE2ETests.swift index 53ed28b127..1850da2fae 100644 --- a/Datadog/E2ETests/Logging/LoggingConfigurationE2ETests.swift +++ b/Datadog/E2ETests/Logging/LoggingConfigurationE2ETests.swift @@ -29,7 +29,7 @@ class LoggingConfigurationE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_config_feature_enabled\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_config_feature_enabled() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK( trackingConsent: .granted, configuration: Datadog.Configuration.builderUsingE2EConfig() @@ -41,7 +41,7 @@ class LoggingConfigurationE2ETests: E2ETests { ) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } @@ -59,7 +59,7 @@ class LoggingConfigurationE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_feature_disabled() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK( trackingConsent: .granted, configuration: Datadog.Configuration.builderUsingE2EConfig() @@ -71,7 +71,7 @@ class LoggingConfigurationE2ETests: E2ETests { ) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } diff --git a/Datadog/E2ETests/Logging/LoggingTrackingConsentE2ETests.swift b/Datadog/E2ETests/Logging/LoggingTrackingConsentE2ETests.swift index 82fe94cf55..4ac2c86290 100644 --- a/Datadog/E2ETests/Logging/LoggingTrackingConsentE2ETests.swift +++ b/Datadog/E2ETests/Logging/LoggingTrackingConsentE2ETests.swift @@ -23,9 +23,10 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// - common performance monitor - measures `Datadog.set(trackingConsent:)` performance: /// ```apm + /// $feature = logs /// $monitor_id = sdk_set_tracking_consent_performance /// $monitor_name = "[RUM] [iOS] Nightly Performance - sdk_set_tracking_consent: has a high average execution time" - /// $monitor_query = "avg(last_1d):avg:trace.sdk_set_tracking_consent{env:instrumentation,resource_name:sdk_set_tracking_consent,service:com.datadog.ios.nightly} > 0.016" + /// $monitor_query = "avg(last_1d):avg:trace.perf_measure{env:instrumentation,resource_name:sdk_set_tracking_consent,service:com.datadog.ios.nightly} > 0.016" /// $monitor_threshold = 0.016 /// ``` @@ -41,10 +42,10 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_config_consent_granted\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_config_consent_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .granted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } logger.sendRandomLog(with: DD.logAttributes()) @@ -62,10 +63,10 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_NOT_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .notGranted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } logger.sendRandomLog(with: DD.logAttributes()) @@ -83,10 +84,10 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_PENDING() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .pending) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } logger.sendRandomLog(with: DD.logAttributes()) @@ -105,13 +106,13 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_GRANTED_to_NOT_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .granted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .notGranted) } @@ -129,13 +130,13 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_GRANTED_to_PENDING() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .granted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .pending) } @@ -151,13 +152,13 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_config_consent_not_granted_to_granted\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_config_consent_NOT_GRANTED_to_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .notGranted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .granted) } @@ -175,13 +176,13 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_NOT_GRANTED_to_PENDING() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .notGranted) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .pending) } @@ -197,16 +198,16 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $monitor_query = "logs(\"service:com.datadog.ios.nightly @test_method_name:logs_config_consent_pending_to_granted\").index(\"*\").rollup(\"count\").last(\"1d\") < 1" /// ``` func test_logs_config_consent_PENDING_to_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .pending) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } logger.sendRandomLog(with: DD.logAttributes()) - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .granted) } } @@ -222,16 +223,16 @@ class LoggingTrackingConsentE2ETests: E2ETests { /// $notify_no_data = false /// ``` func test_logs_config_consent_PENDING_to_NOT_GRANTED() { - measure(spanName: DD.PerfSpanName.sdkInitialize) { + measure(resourceName: DD.PerfSpanName.sdkInitialize) { initializeSDK(trackingConsent: .pending) } - measure(spanName: DD.PerfSpanName.loggerInitialize) { + measure(resourceName: DD.PerfSpanName.loggerInitialize) { logger = Logger.builder.build() } logger.sendRandomLog(with: DD.logAttributes()) - measure(spanName: DD.PerfSpanName.setTrackingConsent) { + measure(resourceName: DD.PerfSpanName.setTrackingConsent) { Datadog.set(trackingConsent: .notGranted) } } diff --git a/tools/nightly-e2e-tests/monitors-gen/templates/monitor-apm.tf.src b/tools/nightly-e2e-tests/monitors-gen/templates/monitor-apm.tf.src index c83ab5167f..6b442975e4 100644 --- a/tools/nightly-e2e-tests/monitors-gen/templates/monitor-apm.tf.src +++ b/tools/nightly-e2e-tests/monitors-gen/templates/monitor-apm.tf.src @@ -1,10 +1,16 @@ resource "datadog_monitor" ${{monitor_id}} { name = ${{monitor_name}} type = "query alert" - tags = ["service:com.datadog.ios.nightly", "env:instrumentation", "team:rumm", "source:ios", ] + tags = ["service:com.datadog.ios.nightly", "env:instrumentation", "team:rumm", "source:ios", "feature:${{feature}}", "monitor:performance"] message = < diff --git a/tools/nightly-e2e-tests/monitors-gen/templates/monitor-logs.tf.src b/tools/nightly-e2e-tests/monitors-gen/templates/monitor-logs.tf.src index 6fffc9a715..cef82c1047 100644 --- a/tools/nightly-e2e-tests/monitors-gen/templates/monitor-logs.tf.src +++ b/tools/nightly-e2e-tests/monitors-gen/templates/monitor-logs.tf.src @@ -1,10 +1,16 @@ resource "datadog_monitor" ${{monitor_id}} { name = ${{monitor_name}} type = "log alert" - tags = ["service:com.datadog.ios.nightly", "env:instrumentation", "team:rumm", "source:ios", ] + tags = ["service:com.datadog.ios.nightly", "env:instrumentation", "team:rumm", "source:ios", "feature:logs", "monitor:behavior"] message = < diff --git a/tools/nightly-e2e-tests/src/main_tf_renderer.py b/tools/nightly-e2e-tests/src/main_tf_renderer.py index b06aa6f179..77101462cf 100644 --- a/tools/nightly-e2e-tests/src/main_tf_renderer.py +++ b/tools/nightly-e2e-tests/src/main_tf_renderer.py @@ -73,6 +73,7 @@ def render(self, monitor: MonitorConfiguration) -> str: result = '' result += self.template_content result = MonitorTemplate.render_template_variables(template=result, monitor=monitor) + result = MonitorTemplate.render_monitor_code(template=result, monitor=monitor) return result @staticmethod @@ -131,3 +132,10 @@ def render_template_variables(template: str, monitor: MonitorConfiguration) -> s ) return '\n'.join(rendered_lines) + + @staticmethod + def render_monitor_code(template: str, monitor: MonitorConfiguration) -> str: + """ + Replaces '## MONITOR_CODE ##' anchor in the template with the code associated to this monitor. + """ + return template.replace("## MONITOR_CODE ##", monitor.code) diff --git a/tools/nightly-e2e-tests/src/test_file_parser.py b/tools/nightly-e2e-tests/src/test_file_parser.py index d5b2007318..5c97bfc191 100644 --- a/tools/nightly-e2e-tests/src/test_file_parser.py +++ b/tools/nightly-e2e-tests/src/test_file_parser.py @@ -25,6 +25,7 @@ class MonitorConfiguration: type: str # a type of monitor (allowed values: 'apm' | 'logs'), used to select monitor template variables: [MonitorVariable] # list of monitor variables code_reference: CodeReference + code: str = '' # monitor code (monitor definition from comment and the entire test method declaration) @dataclass @@ -32,6 +33,7 @@ class TestMethod: method_name: str # the name of this test method monitors: [MonitorConfiguration] # monitors defined in method comment code_reference: CodeReference + code: str # the entire test method declaration code @dataclass() @@ -47,38 +49,64 @@ def read_test_file(path: str): """ with open(path, 'r') as file: comment_regex = r'^[\t ]*(\/\/\/.*)' # e.g. ' /// Sample comment' - method_signature_regex = r'^[\s ]+func (test\w*)\(\)( throws)? {' # e.g. ' func test_sample() throws {' + method_signature_regex = r'^[\t ]+func (test\w*)\(\)( throws)? {' # e.g. ' func test_sample() throws {' test_methods: [TestMethod] = [] independent_monitors: [MonitorConfiguration] = [] - comment_lines_buffer: [(int, str)] = [] + comment_lines_buffer: [(int, str, str)] = [] # (line number, comment with no indent, original line) - for line_no, line_text in enumerate(file.readlines()): + lines_in_file = file.readlines() + + for line_no, line_text in enumerate(lines_in_file): if comment_match := re.match(comment_regex, line_text): # matched comment `///` - comment_lines_buffer.append((line_no, comment_match.groups()[0])) + comment_lines_buffer.append((line_no, comment_match.groups()[0], line_text)) elif method_signature_match := re.match(method_signature_regex, line_text): # matched test method signature method_name = method_signature_match.groups()[0] - method = TestMethod( - method_name=method_name, - monitors=read_monitor_configuration( + + method_code_reference = CodeReference( + file_path=path, + line_no=line_no + 1, + line_text=line_text + ) + + with linter_context(code_reference=method_code_reference): + method_monitors = read_monitors_configuration( # read monitors defined in this method comment comment_lines=comment_lines_buffer, file_path=path - ), - code_reference=CodeReference( - file_path=path, - line_no=line_no + 1, - line_text=line_text ) - ) + method_code = read_method_body( # read method body + lines_in_file=lines_in_file, + method_signature_line_no=line_no + ) + + # Link code declaration to each monitor in this test method: + for method_monitor in method_monitors: + comment_lines = list(map(lambda buffered_line: buffered_line[2], comment_lines_buffer)) + method_monitor.code = ''.join(comment_lines) + method_code + + method = TestMethod( + method_name=method_name, + monitors=method_monitors, + code_reference=method_code_reference, + code=method_code + ) + test_methods.append(method) comment_lines_buffer = [] else: # matched some other line in the file - # Check if buffered comments define any additional monitors: - additional_monitors = read_monitor_configuration( + # Check if buffered comments define any additional monitors (independent monitors defined + # in the test file, but not linked to any test method): + additional_monitors = read_monitors_configuration( comment_lines=comment_lines_buffer, file_path=path ) + + # Link code declaration to each additional monitor: + for additional_monitor in additional_monitors: + comment_lines = list(map(lambda buffered_line: buffered_line[2], comment_lines_buffer)) + additional_monitor.code = ''.join(comment_lines) + # Add to the list of independent monitors for this file: independent_monitors += additional_monitors @@ -93,11 +121,11 @@ def read_test_file(path: str): return None -def read_monitor_configuration(comment_lines: [(int, str)], file_path: str) -> [MonitorConfiguration]: +def read_monitors_configuration(comment_lines: [(int, str, str)], file_path: str) -> [MonitorConfiguration]: """ - Parses method comment lines and produces one or more `MonitorConfiguration` objects. + Parses comment lines and returns zero to many `MonitorConfiguration` objects. - The expected method comment has following structure: + The expected monitor comment has following structure: /// ... anything before /// ```logs @@ -113,7 +141,7 @@ def read_monitor_configuration(comment_lines: [(int, str)], file_path: str) -> [ The "```" token indicates the beginning and end of the monitor definition. There can be more than one monitor defined in each comment. - :return: returns list of `MonitorConfiguration` objects (0 to many) recognized in the method comment. + :return: returns list of `MonitorConfiguration` objects (0 to many) recognized in the comment. """ monitor_region_start_regex = r'^\/\/\/[\s ]+```([a-zA-Z0-9]+)[\s ]*$' # e.g. '/// ```apm' monitor_region_end_regex = r'^\/\/\/[\s ]+```[\s ]*$' # e.g. '/// ```' @@ -124,7 +152,7 @@ def read_monitor_configuration(comment_lines: [(int, str)], file_path: str) -> [ monitors: [MonitorConfiguration] = [] variables_buffer: [MonitorVariable] = [] - for line_no, line_text in comment_lines: + for line_no, line_text, _ in comment_lines: comment_line_code_reference = CodeReference( file_path=file_path, line_no=(line_no + 1), @@ -198,3 +226,36 @@ def read_variable(line_text: str, comment_line_code_reference: CodeReference): with linter_context(code_reference=comment_line_code_reference): Linter.shared.emit_error('Incorrect variable definitions - variable must follow `$name = value` syntax.') return None + + +def read_method_body(lines_in_file: [str], method_signature_line_no: int) -> str: + """ + Parses method body. + + It starts from `method_signature_line` and accepts lines until all opening brackets "{" are matched + with enclosing "}" brackets. + + :return: the body of test method, including its signature and enclosing `}` bracket. + """ + + method_body = '' + line_no = method_signature_line_no + brackets_matched = 0 + + while line_no < len(lines_in_file): + line_text = lines_in_file[line_no] + brackets_matched += line_text.count('{') + brackets_matched -= line_text.count('}') + + method_body += line_text + + if brackets_matched == 0: + break + else: + line_no += 1 + + if line_no == (len(lines_in_file) - 1): + Linter.shared.emit_warning('E2E monitors parser cannot read this method body. Make sure that all opening' + ' brackets "{" are matched with their closing bracket "}".') + + return method_body diff --git a/tools/nightly-e2e-tests/tests/test_parsing_test_file.py b/tools/nightly-e2e-tests/tests/test_parsing_test_file.py index dcf3a4d01c..a68514aeb1 100644 --- a/tools/nightly-e2e-tests/tests/test_parsing_test_file.py +++ b/tools/nightly-e2e-tests/tests/test_parsing_test_file.py @@ -55,9 +55,21 @@ def test_it_reads_monitor_definition_from_test_method(self): ) ], code_reference=CodeReference( - file_path=file.name, - line_no=3, - line_text='/// ```' + file_path=file.name, + line_no=3, + line_text='/// ```' + ), + code='\n'.join( + [ + ' /// Monitor definition assigned to the test method:', + ' /// ```apm', + ' /// $foo = ', + ' /// $bar = ', + ' /// ```', + ' func test_method_name() {', + ' }', + '' + ] ) ) ], @@ -65,6 +77,13 @@ def test_it_reads_monitor_definition_from_test_method(self): file_path=file.name, line_no=7, line_text=' func test_method_name() {\n' + ), + code='\n'.join( + [ + ' func test_method_name() {', + ' }', + '' + ] ) ) ], @@ -116,6 +135,16 @@ class Foo { file_path=file.name, line_no=4, line_text='/// ```' + ), + code='\n'.join( + [ + ' /// Monitor definition not assigned to any test method:', + ' /// ```apm', + ' /// $foo = ', + ' /// $bar = ', + ' /// ```', + '' + ] ) ) ] diff --git a/tools/nightly-e2e-tests/tests/test_rendering_monitor_template.py b/tools/nightly-e2e-tests/tests/test_rendering_monitor_template.py index 3fe9155328..3d07ff2e6b 100644 --- a/tools/nightly-e2e-tests/tests/test_rendering_monitor_template.py +++ b/tools/nightly-e2e-tests/tests/test_rendering_monitor_template.py @@ -110,11 +110,43 @@ def test_when_mandatory_variable_is_not_specified_it_fails(self): message='Variable $argument_with_no_default is required, but not defined for this monitor.' ) + def test_it_renders_monitor_code(self): + template = MonitorTemplate( + ''' + some text before + ## MONITOR_CODE ## + some text after + ''' + ) + + monitor_code = random_multiline_string(length=256) + + monitor = MonitorConfiguration( + type='', + variables=[], + code_reference=any_code_reference(), + code=monitor_code + ) + + rendered_template = template.render(monitor=monitor) + expected_template = f''' + some text before + {monitor_code} + some text after + ''' + + self.assertEqual(expected_template, rendered_template) + def random_string(length: int = 32): characters_set = string.ascii_letters + string.digits + string.punctuation + ' \t' return ''.join((random.choice(characters_set) for i in range(length))) +def random_multiline_string(length: int = 32): + characters_set = string.ascii_letters + string.digits + string.punctuation + ' \t' + '\n' + return ''.join((random.choice(characters_set) for i in range(length))) + + def any_code_reference(): return CodeReference(file_path='', line_no=0, line_text='')