|
| 1 | +package datadog.gradle.plugin.config |
| 2 | + |
| 3 | +import com.github.javaparser.ParserConfiguration |
| 4 | +import com.github.javaparser.StaticJavaParser |
| 5 | +import com.github.javaparser.ast.CompilationUnit |
| 6 | +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration |
| 7 | +import com.github.javaparser.ast.body.MethodDeclaration |
| 8 | +import com.github.javaparser.ast.expr.StringLiteralExpr |
| 9 | +import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt |
| 10 | +import com.github.javaparser.ast.stmt.ReturnStmt |
| 11 | +import org.gradle.api.DefaultTask |
| 12 | +import org.gradle.api.GradleException |
| 13 | +import org.gradle.api.file.ConfigurableFileCollection |
| 14 | +import org.gradle.api.provider.Property |
| 15 | +import org.gradle.api.tasks.Input |
| 16 | +import org.gradle.api.tasks.InputFiles |
| 17 | +import org.gradle.api.tasks.TaskAction |
| 18 | + |
| 19 | +/** Abstract base for tasks that scan instrumentation source files against the generated config class. */ |
| 20 | +abstract class InstrumentationConfigCheckTask : DefaultTask() { |
| 21 | + @get:InputFiles |
| 22 | + abstract val mainSourceSetOutput: ConfigurableFileCollection |
| 23 | + |
| 24 | + @get:InputFiles |
| 25 | + abstract val instrumentationFiles: ConfigurableFileCollection |
| 26 | + |
| 27 | + @get:Input |
| 28 | + abstract val generatedClassName: Property<String> |
| 29 | + |
| 30 | + @get:Input |
| 31 | + abstract val errorHeader: Property<String> |
| 32 | + |
| 33 | + @get:Input |
| 34 | + abstract val errorMessage: Property<String> |
| 35 | + |
| 36 | + @get:Input |
| 37 | + abstract val successMessage: Property<String> |
| 38 | + |
| 39 | + @TaskAction |
| 40 | + fun execute() { |
| 41 | + val configFields = loadConfigFields(mainSourceSetOutput, generatedClassName.get()) |
| 42 | + |
| 43 | + val parserConfig = ParserConfiguration() |
| 44 | + parserConfig.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8) |
| 45 | + StaticJavaParser.setConfiguration(parserConfig) |
| 46 | + |
| 47 | + val repoRoot = project.rootProject.projectDir.toPath() |
| 48 | + val violations = instrumentationFiles.files.flatMap { file -> |
| 49 | + val rel = repoRoot.relativize(file.toPath()).toString() |
| 50 | + val cu: CompilationUnit = try { |
| 51 | + StaticJavaParser.parse(file) |
| 52 | + } catch (_: Exception) { |
| 53 | + return@flatMap emptyList() |
| 54 | + } |
| 55 | + collectPropertyViolations(configFields, rel, cu) |
| 56 | + } |
| 57 | + |
| 58 | + if (violations.isNotEmpty()) { |
| 59 | + logger.error(errorHeader.get()) |
| 60 | + violations.forEach { logger.lifecycle(it) } |
| 61 | + throw GradleException(errorMessage.get()) |
| 62 | + } else { |
| 63 | + logger.info(successMessage.get()) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected abstract fun collectPropertyViolations( |
| 68 | + configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit |
| 69 | + ): List<String> |
| 70 | + |
| 71 | + /** Collects violations for [key] against [supported] and [aliases], checking that all [expectedAliases] are values of that alias entry. */ |
| 72 | + protected fun collectMissingKeysAndAliases( |
| 73 | + key: String, |
| 74 | + expectedAliases: List<String>, |
| 75 | + supported: Set<String>, |
| 76 | + aliases: Map<String, List<String>>, |
| 77 | + location: String, |
| 78 | + context: String |
| 79 | + ): List<String> = buildList { |
| 80 | + if (key !in supported) { |
| 81 | + add("$location -> $context: '$key' is missing from SUPPORTED") |
| 82 | + } |
| 83 | + if (key !in aliases) { |
| 84 | + add("$location -> $context: '$key' is missing from ALIASES") |
| 85 | + } else { |
| 86 | + val aliasValues = aliases[key] ?: emptyList() |
| 87 | + for (expected in expectedAliases) { |
| 88 | + if (expected !in aliasValues) { |
| 89 | + add("$location -> $context: '$expected' is missing from ALIASES['$key']") |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** Checks that InstrumenterModule integration names have proper entries in SUPPORTED and ALIASES. */ |
| 97 | +abstract class CheckInstrumenterModuleConfigTask : InstrumentationConfigCheckTask() { |
| 98 | + override fun collectPropertyViolations( |
| 99 | + configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit |
| 100 | + ): List<String> { |
| 101 | + val violations = mutableListOf<String>() |
| 102 | + |
| 103 | + cu.findAll(ClassOrInterfaceDeclaration::class.java).forEach classLoop@{ classDecl -> |
| 104 | + val extendsModule = classDecl.extendedTypes.any { it.toString().startsWith("InstrumenterModule") } |
| 105 | + if (!extendsModule) return@classLoop |
| 106 | + |
| 107 | + classDecl.findAll(ExplicitConstructorInvocationStmt::class.java) |
| 108 | + .filter { !it.isThis } |
| 109 | + .forEach { superCall -> |
| 110 | + val names = superCall.arguments |
| 111 | + .filterIsInstance<StringLiteralExpr>() |
| 112 | + .map { it.value } |
| 113 | + val line = superCall.range.map { it.begin.line }.orElse(1) |
| 114 | + |
| 115 | + for (name in names) { |
| 116 | + val normalized = name.uppercase().replace("-", "_").replace(".", "_") |
| 117 | + val enabledKey = "DD_TRACE_${normalized}_ENABLED" |
| 118 | + val context = "Integration '$name' (super arg)" |
| 119 | + val location = "$relativePath:$line" |
| 120 | + |
| 121 | + violations.addAll(collectMissingKeysAndAliases( |
| 122 | + enabledKey, |
| 123 | + listOf("DD_TRACE_INTEGRATION_${normalized}_ENABLED", "DD_INTEGRATION_${normalized}_ENABLED"), |
| 124 | + configFields.supported, configFields.aliases, location, context |
| 125 | + )) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return violations |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** Checks that Decorator instrumentationNames have proper analytics entries in SUPPORTED and ALIASES. */ |
| 135 | +abstract class CheckDecoratorAnalyticsConfigTask : InstrumentationConfigCheckTask() { |
| 136 | + override fun collectPropertyViolations( |
| 137 | + configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit |
| 138 | + ): List<String> { |
| 139 | + val violations = mutableListOf<String>() |
| 140 | + |
| 141 | + cu.findAll(MethodDeclaration::class.java) |
| 142 | + .filter { it.nameAsString == "instrumentationNames" && it.parameters.isEmpty() } |
| 143 | + .forEach { method -> |
| 144 | + val names = method.findAll(ReturnStmt::class.java).flatMap { ret -> |
| 145 | + ret.expression.map { it.findAll(StringLiteralExpr::class.java).map { s -> s.value } } |
| 146 | + .orElse(emptyList()) |
| 147 | + } |
| 148 | + val line = method.range.map { it.begin.line }.orElse(1) |
| 149 | + |
| 150 | + for (name in names) { |
| 151 | + val normalized = name.uppercase().replace("-", "_").replace(".", "_") |
| 152 | + val context = "Decorator instrumentationName '$name'" |
| 153 | + val location = "$relativePath:$line" |
| 154 | + |
| 155 | + violations.addAll(collectMissingKeysAndAliases( |
| 156 | + "DD_TRACE_${normalized}_ANALYTICS_ENABLED", |
| 157 | + listOf("DD_${normalized}_ANALYTICS_ENABLED"), |
| 158 | + configFields.supported, configFields.aliases, location, context |
| 159 | + )) |
| 160 | + violations.addAll(collectMissingKeysAndAliases( |
| 161 | + "DD_TRACE_${normalized}_ANALYTICS_SAMPLE_RATE", |
| 162 | + listOf("DD_${normalized}_ANALYTICS_SAMPLE_RATE"), |
| 163 | + configFields.supported, configFields.aliases, location, context |
| 164 | + )) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return violations |
| 169 | + } |
| 170 | +} |
0 commit comments