Skip to content

Commit bcf7dc9

Browse files
committed
Feature/RunConfig - simply modify the (System) properties
1 parent d7db36e commit bcf7dc9

File tree

1 file changed

+24
-103
lines changed

1 file changed

+24
-103
lines changed

java/src/main/kotlin/org/digma/intellij/plugin/idea/runcfg/AutoOtelAgentRunConfigurationExtension.kt

Lines changed: 24 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ import com.intellij.execution.configurations.JavaParameters
77
import com.intellij.execution.configurations.ModuleRunConfiguration
88
import com.intellij.execution.configurations.RunConfigurationBase
99
import com.intellij.execution.configurations.RunnerSettings
10-
import com.intellij.execution.process.ProcessEvent
11-
import com.intellij.execution.process.ProcessHandler
12-
import com.intellij.execution.process.ProcessListener
1310
import com.intellij.execution.ui.ConsoleView
1411
import com.intellij.execution.ui.ConsoleViewContentType
1512
import com.intellij.openapi.diagnostic.Logger
16-
import com.intellij.openapi.project.Project
1713
import org.digma.intellij.plugin.log.Log
1814
import org.digma.intellij.plugin.persistence.PersistenceService
1915
import org.digma.intellij.plugin.settings.SettingsState
2016
import org.jetbrains.idea.maven.execution.MavenRunConfiguration
2117
import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
2218

23-
private const val ORG_GRADLE_JAVA_TOOL_OPTIONS = "ORG_GRADLE_JAVA_TOOL_OPTIONS"
2419

2520
class AutoOtelAgentRunConfigurationExtension : RunConfigurationExtension() {
2621

@@ -61,25 +56,21 @@ class AutoOtelAgentRunConfigurationExtension : RunConfigurationExtension() {
6156
Log.log(logger::debug, "updateJavaParameters, project:{}, id:{}, name:{}, type:{}",
6257
configuration.project, configuration.id, configuration.name, configuration.type)
6358

64-
val project = configuration.project
65-
6659
//testing if enabled must be done here just before running.
6760
if (!enabled()) {
6861
Log.log(logger::debug, "AutoOtelAgentRunConfigurationExtension is not enabled")
6962
return
7063
}
7164

72-
7365
if (isJavaConfiguration(configuration)) {
74-
//this also works for: CommonJavaRunConfigurationParameters
75-
//params.vmParametersList.addParametersString("-verbose:class -javaagent:/home/shalom/tmp/run-configuration/opentelemetry-javaagent.jar")
76-
//params.vmParametersList.addProperty("myprop","myvalue")
77-
val javaToolOptions = buildJavaToolOptions(configuration, project)
78-
javaToolOptions?.let {
79-
mergeJavaToolOptions(params, it)
80-
}
66+
configuration as CommonJavaRunConfigurationParameters
8167

68+
adjustWithOtelAndDigma(configuration, params)
8269
} else if (isGradleConfiguration(configuration)) {
70+
configuration as GradleRunConfiguration
71+
72+
adjustWithOtelAndDigma(configuration, params)
73+
8374
//when injecting JAVA_TOOL_OPTIONS to GradleRunConfiguration the GradleRunConfiguration will also run with
8475
// JAVA_TOOL_OPTIONS which is not ideal. for example, gradle will execute with JAVA_TOOL_OPTIONS and then fork
8576
// a process for the main method or unit test with JAVA_TOOL_OPTIONS. ideally we want only the forked process
@@ -90,116 +81,46 @@ class AutoOtelAgentRunConfigurationExtension : RunConfigurationExtension() {
9081
// to learn how gradle does it see GradleTaskManager and GradleExecutionHelper.
9182
//when gradle runs the :test task it's not possible to pass system properties to the task.
9283
// JAVA_TOOL_OPTIONS is not the best as said above, but it works.
93-
configuration as GradleRunConfiguration
94-
val javaToolOptions = buildJavaToolOptions(configuration, project)
95-
javaToolOptions?.let {
96-
mergeGradleJavaToolOptions(configuration, javaToolOptions)
97-
}
84+
9885
} else if (isMavenConfiguration(configuration)) {
9986
configuration as MavenRunConfiguration
100-
val javaToolOptions = buildJavaToolOptions(configuration, project)
101-
javaToolOptions?.let {
102-
mergeJavaToolOptions(params, it)
103-
}
104-
}
105-
}
10687

107-
108-
//this is only for gradle. we need to keep original JAVA_TOOL_OPTIONS if exists and restore when the process is
109-
// finished, anyway we need to clean our JAVA_TOOL_OPTIONS because it will be saved in the run configuration settings.
110-
private fun mergeGradleJavaToolOptions(configuration: GradleRunConfiguration, myJavaToolOptions: String) {
111-
var javaToolOptions = myJavaToolOptions
112-
//need to replace the env because it may be immutable map
113-
val newEnv = configuration.settings.env.toMutableMap()
114-
if (configuration.settings.env.containsKey(JAVA_TOOL_OPTIONS)) {
115-
val currentJavaToolOptions = configuration.settings.env[JAVA_TOOL_OPTIONS]
116-
javaToolOptions = "$myJavaToolOptions $currentJavaToolOptions"
117-
newEnv[ORG_GRADLE_JAVA_TOOL_OPTIONS] = currentJavaToolOptions!!
88+
adjustWithOtelAndDigma(configuration, params)
11889
}
119-
newEnv[JAVA_TOOL_OPTIONS] = javaToolOptions
120-
configuration.settings.env = newEnv
12190
}
12291

123-
124-
//this is for java and maven run configurations. merge in case users have their own JAVA_TOOL_OPTIONS
125-
private fun mergeJavaToolOptions(params: JavaParameters, myJavaToolOptions: String) {
126-
var javaToolOptions = myJavaToolOptions
127-
if (params.env.containsKey(JAVA_TOOL_OPTIONS)) {
128-
val currentJavaToolOptions = params.env[JAVA_TOOL_OPTIONS]
129-
javaToolOptions = "$myJavaToolOptions $currentJavaToolOptions"
130-
}
131-
params.env[JAVA_TOOL_OPTIONS] = javaToolOptions
132-
}
133-
134-
135-
override fun attachToProcess(
136-
configuration: RunConfigurationBase<*>,
137-
handler: ProcessHandler,
138-
runnerSettings: RunnerSettings?
139-
) {
140-
//we need to clean gradle configuration from our JAVA_TOOL_OPTIONS
141-
if (isGradleConfiguration(configuration)) {
142-
handler.addProcessListener(object : ProcessListener {
143-
144-
override fun processWillTerminate(event: ProcessEvent, willBeDestroyed: Boolean) {
145-
cleanGradleSettings(configuration)
146-
}
147-
148-
private fun cleanGradleSettings(configuration: RunConfigurationBase<*>) {
149-
configuration as GradleRunConfiguration
150-
Log.log(logger::debug, "Cleaning gradle configuration {}", configuration)
151-
if (configuration.settings.env.containsKey(ORG_GRADLE_JAVA_TOOL_OPTIONS)) {
152-
val orgJavaToolOptions = configuration.settings.env[ORG_GRADLE_JAVA_TOOL_OPTIONS]
153-
configuration.settings.env[JAVA_TOOL_OPTIONS] = orgJavaToolOptions
154-
configuration.settings.env.remove(ORG_GRADLE_JAVA_TOOL_OPTIONS)
155-
} else if (configuration.settings.env.containsKey(JAVA_TOOL_OPTIONS)) {
156-
configuration.settings.env.remove(JAVA_TOOL_OPTIONS)
157-
}
158-
}
159-
})
160-
}
161-
162-
}
163-
164-
16592
override fun decorate(
16693
console: ConsoleView,
16794
configuration: RunConfigurationBase<*>,
16895
executor: Executor
16996
): ConsoleView {
170-
if (enabled() &&
171-
(isMavenConfiguration(configuration) || isJavaConfiguration(configuration))) {
172-
//that only works for java and maven run configurations.
173-
console.print("This process is enhanced by Digma OTEL agent !\n", ConsoleViewContentType.LOG_WARNING_OUTPUT)
97+
98+
if (enabled()) {
99+
console.print("Observability - this process is enhanced by Digma OTEL agent\n", ConsoleViewContentType.LOG_WARNING_OUTPUT)
174100
}
175101

176102
return console
177103
}
178104

179-
private fun buildJavaToolOptions(configuration: RunConfigurationBase<*>, project: Project): String? {
105+
private fun adjustWithOtelAndDigma(configuration: RunConfigurationBase<*>, params: JavaParameters) {
106+
107+
val otelAgentPath = OTELJarProvider.getInstance().getOtelAgentJarPath(configuration.project)
108+
val digmaExtensionPath = OTELJarProvider.getInstance().getDigmaAgentExtensionJarPath(configuration.project)
180109

181-
val otelAgentPath = OTELJarProvider.getInstance().getOtelAgentJarPath(project)
182-
val digmaExtensionPath = OTELJarProvider.getInstance().getDigmaAgentExtensionJarPath(project)
183110
if (otelAgentPath == null || digmaExtensionPath == null) {
184-
Log.log(logger::debug, "could not build $JAVA_TOOL_OPTIONS because otel agent or digma extension jar are not available. please check the logs")
185-
return null
111+
Log.log(logger::debug, "could not adjust process because OTEL agent and Digma extension are not available. please check the logs")
112+
return
186113
}
187114

188-
return "".plus("-javaagent:$otelAgentPath")
189-
.plus(" ")
190-
.plus("-Dotel.javaagent.extensions=$digmaExtensionPath")
191-
.plus(" ")
192-
.plus("-Dotel.traces.exporter=otlp")
193-
.plus(" ")
194-
.plus("-Dotel.metrics.exporter=none")
195-
.plus(" ")
196-
.plus("-Dotel.exporter.otlp.traces.endpoint=${getExporterUrl()}")
197-
.plus(" ")
198-
.plus("-Dotel.service.name=${getServiceName(configuration)}")
199-
.plus(" ")
115+
params.vmParametersList.addParametersString("-javaagent:$otelAgentPath")
116+
params.vmParametersList.addProperty("otel.javaagent.extensions", digmaExtensionPath)
117+
params.vmParametersList.addProperty("otel.traces.exporter", "otlp")
118+
params.vmParametersList.addProperty("otel.metrics.exporter", "none")
119+
params.vmParametersList.addProperty("otel.exporter.otlp.traces.endpoint", getExporterUrl())
120+
params.vmParametersList.addProperty("otel.service.name", evalServiceName(configuration))
200121
}
201122

202-
private fun getServiceName(configuration: RunConfigurationBase<*>): String {
123+
private fun evalServiceName(configuration: RunConfigurationBase<*>): String {
203124
return if (configuration is ModuleRunConfiguration && configuration.modules.isNotEmpty()) {
204125
val moduleName = configuration.modules.first().name
205126
moduleName.replace(" ", "").trim()

0 commit comments

Comments
 (0)