-
Notifications
You must be signed in to change notification settings - Fork 332
Add Integration Specific Handling for Config Inversion Linter #11074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mhlidd
wants to merge
12
commits into
master
Choose a base branch
from
mhlidd/add_integration_gradle_task
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+222
−4
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d550f17
adding logback env var to supported-configurations.json
mhlidd c44baa3
adding spring-messaging-kotlin
mhlidd f8b124a
adding profiling config
mhlidd f66c37e
Merge branch 'master' into mhlidd/add_logback_config
mhlidd b6f0dab
init
mhlidd 08dd521
adding tasks to gitlab job
mhlidd 01ad130
ensure GeneratedSupportedConfigurations is generated before the task …
mhlidd c2e09d5
Merge branch 'mhlidd/add_logback_config' into mhlidd/add_integration_…
mhlidd f633d3b
Merge branch 'master' into mhlidd/add_integration_gradle_task
mhlidd d011080
updating pr feedback
mhlidd d3e04ce
updating PR comments 1
mhlidd 244d3c3
abstracting instrumentation checks into abstract class and extending …
mhlidd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,25 @@ import com.github.javaparser.ParserConfiguration | |
| import com.github.javaparser.StaticJavaParser | ||
| import com.github.javaparser.ast.CompilationUnit | ||
| import com.github.javaparser.ast.Modifier | ||
| import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration | ||
| import com.github.javaparser.ast.body.FieldDeclaration | ||
| import com.github.javaparser.ast.body.MethodDeclaration | ||
| import com.github.javaparser.ast.body.VariableDeclarator | ||
| import com.github.javaparser.ast.expr.StringLiteralExpr | ||
| import com.github.javaparser.ast.nodeTypes.NodeWithModifiers | ||
| import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt | ||
| import com.github.javaparser.ast.stmt.ReturnStmt | ||
| import org.gradle.api.DefaultTask | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.Plugin | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.file.ConfigurableFileCollection | ||
| import org.gradle.api.provider.Property | ||
| import org.gradle.api.tasks.Input | ||
| import org.gradle.api.tasks.InputFiles | ||
| import org.gradle.api.tasks.SourceSet | ||
| import org.gradle.api.tasks.SourceSetContainer | ||
| import org.gradle.api.tasks.TaskAction | ||
| import org.gradle.kotlin.dsl.getByType | ||
| import java.net.URLClassLoader | ||
| import java.nio.file.Path | ||
|
|
@@ -23,13 +33,16 @@ class ConfigInversionLinter : Plugin<Project> { | |
| registerLogEnvVarUsages(target, extension) | ||
| registerCheckEnvironmentVariablesUsage(target) | ||
| registerCheckConfigStringsTask(target, extension) | ||
| registerCheckInstrumenterModuleConfigurations(target, extension) | ||
| registerCheckDecoratorAnalyticsConfigurations(target, extension) | ||
| } | ||
| } | ||
|
|
||
| // Data class for fields from generated class | ||
| private data class LoadedConfigFields( | ||
| data class LoadedConfigFields( | ||
| val supported: Set<String>, | ||
| val aliasMapping: Map<String, String> = emptyMap() | ||
| val aliasMapping: Map<String, String> = emptyMap(), | ||
| val aliases: Map<String, List<String>> = emptyMap() | ||
| ) | ||
|
|
||
| // Cache for fields from generated class | ||
|
|
@@ -55,7 +68,9 @@ private fun loadConfigFields( | |
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| val aliasMappingMap = clazz.getField("ALIAS_MAPPING").get(null) as Map<String, String> | ||
| LoadedConfigFields(supportedSet, aliasMappingMap) | ||
| @Suppress("UNCHECKED_CAST") | ||
| val aliasesMap = clazz.getField("ALIASES").get(null) as Map<String, List<String>> | ||
| LoadedConfigFields(supportedSet, aliasMappingMap, aliasesMap) | ||
| }.also { cachedConfigFields = it } | ||
| } | ||
| } | ||
|
|
@@ -248,3 +263,206 @@ private fun registerCheckConfigStringsTask(project: Project, extension: Supporte | |
| } | ||
| } | ||
| } | ||
|
|
||
| /** Collects violations for [key] against [supported] and [aliases], checking that all [expectedAliases] are values of that alias entry. */ | ||
| private fun collectMissingKeysAndAliases( | ||
| key: String, | ||
| expectedAliases: List<String>, | ||
| supported: Set<String>, | ||
| aliases: Map<String, List<String>>, | ||
| location: String, | ||
| context: String | ||
| ): List<String> = buildList { | ||
| if (key !in supported) { | ||
| add("$location -> $context: '$key' is missing from SUPPORTED") | ||
| } | ||
| if (key !in aliases) { | ||
| add("$location -> $context: '$key' is missing from ALIASES") | ||
| } else { | ||
| val aliasValues = aliases[key] ?: emptyList() | ||
| for (expected in expectedAliases) { | ||
| if (expected !in aliasValues) { | ||
| add("$location -> $context: '$expected' is missing from ALIASES['$key']") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Abstract base for tasks that scan instrumentation source files against the generated config class. */ | ||
| abstract class InstrumentationConfigCheckTask : DefaultTask() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: By the way I would move these classes in their own files. (Maybe |
||
| @get:InputFiles | ||
| abstract val mainSourceSetOutput: ConfigurableFileCollection | ||
|
|
||
| @get:InputFiles | ||
| abstract val instrumentationFiles: ConfigurableFileCollection | ||
|
|
||
| @get:Input | ||
| abstract val generatedClassName: Property<String> | ||
|
|
||
| @get:Input | ||
| abstract val errorHeader: Property<String> | ||
|
|
||
| @get:Input | ||
| abstract val errorMessage: Property<String> | ||
|
|
||
| @get:Input | ||
| abstract val successMessage: Property<String> | ||
|
|
||
| @TaskAction | ||
| fun execute() { | ||
| val configFields = loadConfigFields(mainSourceSetOutput, generatedClassName.get()) | ||
|
|
||
| val parserConfig = ParserConfiguration() | ||
| parserConfig.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8) | ||
| StaticJavaParser.setConfiguration(parserConfig) | ||
|
|
||
| val repoRoot = project.rootProject.projectDir.toPath() | ||
| val violations = instrumentationFiles.files.flatMap { file -> | ||
| val rel = repoRoot.relativize(file.toPath()).toString() | ||
| val cu: CompilationUnit = try { | ||
| StaticJavaParser.parse(file) | ||
| } catch (_: Exception) { | ||
| return@flatMap emptyList() | ||
| } | ||
| collectPropertyViolations(configFields, rel, cu) | ||
| } | ||
|
|
||
| if (violations.isNotEmpty()) { | ||
| logger.error(errorHeader.get()) | ||
| violations.forEach { logger.lifecycle(it) } | ||
| throw GradleException(errorMessage.get()) | ||
| } else { | ||
| logger.info(successMessage.get()) | ||
| } | ||
| } | ||
|
|
||
| protected abstract fun collectPropertyViolations( | ||
| configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit | ||
| ): List<String> | ||
| } | ||
|
|
||
| /** Checks that InstrumenterModule integration names have proper entries in SUPPORTED and ALIASES. */ | ||
| abstract class CheckInstrumenterModuleConfigTask : InstrumentationConfigCheckTask() { | ||
| override fun collectPropertyViolations( | ||
| configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit | ||
| ): List<String> { | ||
| val violations = mutableListOf<String>() | ||
|
|
||
| cu.findAll(ClassOrInterfaceDeclaration::class.java).forEach classLoop@{ classDecl -> | ||
| val extendsModule = classDecl.extendedTypes.any { it.toString().startsWith("InstrumenterModule") } | ||
| if (!extendsModule) return@classLoop | ||
|
|
||
| classDecl.findAll(ExplicitConstructorInvocationStmt::class.java) | ||
| .filter { !it.isThis } | ||
| .forEach { superCall -> | ||
| val names = superCall.arguments | ||
| .filterIsInstance<StringLiteralExpr>() | ||
| .map { it.value } | ||
| val line = superCall.range.map { it.begin.line }.orElse(1) | ||
|
|
||
| for (name in names) { | ||
| val normalized = name.uppercase().replace("-", "_").replace(".", "_") | ||
| val enabledKey = "DD_TRACE_${normalized}_ENABLED" | ||
| val context = "Integration '$name' (super arg)" | ||
| val location = "$relativePath:$line" | ||
|
|
||
| violations.addAll(collectMissingKeysAndAliases( | ||
| enabledKey, | ||
| listOf("DD_TRACE_INTEGRATION_${normalized}_ENABLED", "DD_INTEGRATION_${normalized}_ENABLED"), | ||
| configFields.supported, configFields.aliases, location, context | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return violations | ||
| } | ||
| } | ||
|
|
||
| /** Checks that Decorator instrumentationNames have proper analytics entries in SUPPORTED and ALIASES. */ | ||
| abstract class CheckDecoratorAnalyticsConfigTask : InstrumentationConfigCheckTask() { | ||
| override fun collectPropertyViolations( | ||
| configFields: LoadedConfigFields, relativePath: String, cu: CompilationUnit | ||
| ): List<String> { | ||
| val violations = mutableListOf<String>() | ||
|
|
||
| cu.findAll(MethodDeclaration::class.java) | ||
| .filter { it.nameAsString == "instrumentationNames" && it.parameters.isEmpty() } | ||
| .forEach { method -> | ||
| val names = method.findAll(ReturnStmt::class.java).flatMap { ret -> | ||
| ret.expression.map { it.findAll(StringLiteralExpr::class.java).map { s -> s.value } } | ||
| .orElse(emptyList()) | ||
| } | ||
| val line = method.range.map { it.begin.line }.orElse(1) | ||
|
|
||
| for (name in names) { | ||
| val normalized = name.uppercase().replace("-", "_").replace(".", "_") | ||
| val context = "Decorator instrumentationName '$name'" | ||
| val location = "$relativePath:$line" | ||
|
|
||
| violations.addAll(collectMissingKeysAndAliases( | ||
| "DD_TRACE_${normalized}_ANALYTICS_ENABLED", | ||
| listOf("DD_${normalized}_ANALYTICS_ENABLED"), | ||
| configFields.supported, configFields.aliases, location, context | ||
| )) | ||
| violations.addAll(collectMissingKeysAndAliases( | ||
| "DD_TRACE_${normalized}_ANALYTICS_SAMPLE_RATE", | ||
| listOf("DD_${normalized}_ANALYTICS_SAMPLE_RATE"), | ||
| configFields.supported, configFields.aliases, location, context | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| return violations | ||
| } | ||
| } | ||
|
|
||
| /** Registers `checkInstrumenterModuleConfigurations` to verify each InstrumenterModule's integration name has proper entries in SUPPORTED and ALIASES. */ | ||
| private fun registerCheckInstrumenterModuleConfigurations(project: Project, extension: SupportedTracerConfigurations) { | ||
| val ownerPath = extension.configOwnerPath | ||
| val generatedFile = extension.className | ||
|
|
||
| project.tasks.register("checkInstrumenterModuleConfigurations", CheckInstrumenterModuleConfigTask::class.java) { | ||
| group = "verification" | ||
| description = "Validates that InstrumenterModule integration names have corresponding entries in SUPPORTED and ALIASES" | ||
|
|
||
| mainSourceSetOutput.from(ownerPath.map { | ||
| project.project(it) | ||
| .extensions.getByType<SourceSetContainer>() | ||
| .named(SourceSet.MAIN_SOURCE_SET_NAME) | ||
| .map { main -> main.output } | ||
| }) | ||
| instrumentationFiles.from(project.fileTree(project.rootProject.projectDir) { | ||
| include("dd-java-agent/instrumentation/**/src/main/java/**/*.java") | ||
| }) | ||
| generatedClassName.set(generatedFile) | ||
| errorHeader.set("\nFound InstrumenterModule integration names with missing SUPPORTED/ALIASES entries:") | ||
| errorMessage.set("InstrumenterModule integration names are missing from SUPPORTED or ALIASES in '${extension.jsonFile.get()}'.") | ||
| successMessage.set("All InstrumenterModule integration names have proper SUPPORTED and ALIASES entries.") | ||
| } | ||
| } | ||
|
|
||
| /** Registers `checkDecoratorAnalyticsConfigurations` to verify each BaseDecorator subclass's instrumentationNames have proper analytics entries in SUPPORTED and ALIASES. */ | ||
| private fun registerCheckDecoratorAnalyticsConfigurations(project: Project, extension: SupportedTracerConfigurations) { | ||
| val ownerPath = extension.configOwnerPath | ||
| val generatedFile = extension.className | ||
|
|
||
| project.tasks.register("checkDecoratorAnalyticsConfigurations", CheckDecoratorAnalyticsConfigTask::class.java) { | ||
| group = "verification" | ||
| description = "Validates that Decorator instrumentationNames have corresponding analytics entries in SUPPORTED and ALIASES" | ||
|
|
||
| mainSourceSetOutput.from(ownerPath.map { | ||
| project.project(it) | ||
| .extensions.getByType<SourceSetContainer>() | ||
| .named(SourceSet.MAIN_SOURCE_SET_NAME) | ||
| .map { main -> main.output } | ||
| }) | ||
| instrumentationFiles.from(project.fileTree(project.rootProject.projectDir) { | ||
| include("dd-java-agent/instrumentation/**/src/main/java/**/*.java") | ||
| }) | ||
| generatedClassName.set(generatedFile) | ||
| errorHeader.set("\nFound Decorator instrumentationNames with missing analytics SUPPORTED/ALIASES entries:") | ||
| errorMessage.set("Decorator instrumentationNames are missing analytics entries from SUPPORTED or ALIASES in '${extension.jsonFile.get()}'.") | ||
| successMessage.set("All Decorator instrumentationNames have proper analytics SUPPORTED and ALIASES entries.") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: the task changes looks good, now I think the tsk should not "leak" into the gitlab file.
So I propose to wire the task as dependencies. Rereading this I think you could create a dumb lifecycle task, e.g.
checkConfigurations, that depends on the other tasks.