Skip to content

Commit ff8f3e6

Browse files
committed
fix spring boot micrometer
1 parent 73e1f74 commit ff8f3e6

18 files changed

+314
-167
lines changed

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/execution/ExecutionUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ fun getModuleMetadata(module: Module?): ModuleMetadata? {
2323
val moduleExt = modulesDepsService.getModuleExt(module.name) ?: return null
2424
return moduleExt.metadata
2525
}
26+
27+
28+
fun mapToFlatString(attributes: Map<String, String>): String {
29+
return attributes.entries.joinToString(",")
30+
}
31+

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/execution/ExternalSystemConfigurationCleaner.kt

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,14 @@ import com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunCo
55

66
class ExternalSystemConfigurationCleaner(configuration: RunConfiguration) : ConfigurationCleaner(configuration) {
77

8-
98
//currently we need to clean only for gradle
109
override fun cleanConfiguration() {
1110

12-
//casting must succeed or we have a bug
13-
val myConfiguration = configuration as ExternalSystemRunConfiguration
14-
15-
if (myConfiguration.settings.env.containsKey(ORG_JAVA_TOOL_OPTIONS)) {
16-
val newEnv = mutableMapOf<String, String>()
17-
val orgJavaToolOptions = myConfiguration.settings.env[ORG_JAVA_TOOL_OPTIONS]
18-
newEnv.putAll(myConfiguration.settings.env)
19-
if (orgJavaToolOptions != null) {
20-
newEnv[JAVA_TOOL_OPTIONS] = orgJavaToolOptions
21-
}
22-
newEnv.remove(ORG_JAVA_TOOL_OPTIONS)
23-
myConfiguration.settings.env = newEnv
24-
} else if (myConfiguration.settings.env.containsKey(JAVA_TOOL_OPTIONS)) {
25-
val newEnv = mutableMapOf<String, String>()
26-
newEnv.putAll(myConfiguration.settings.env)
27-
newEnv.remove(JAVA_TOOL_OPTIONS)
28-
myConfiguration.settings.env = newEnv
29-
}
30-
31-
cleanOtelResourceAttributes(myConfiguration)
32-
}
33-
34-
35-
private fun cleanOtelResourceAttributes(configuration: ExternalSystemRunConfiguration) {
36-
37-
if (configuration.settings.env.containsKey(ORG_OTEL_RESOURCE_ATTRIBUTES)) {
38-
val newEnv = mutableMapOf<String, String>()
39-
val orgOtelResourceAttributes = configuration.settings.env[ORG_OTEL_RESOURCE_ATTRIBUTES]
40-
newEnv.putAll(configuration.settings.env)
41-
if (orgOtelResourceAttributes != null) {
42-
newEnv[OTEL_RESOURCE_ATTRIBUTES] = orgOtelResourceAttributes
11+
if (configuration is ExternalSystemRunConfiguration) {
12+
val orgEnv = ExternalSystemConfigurationTempStorage.orgConfigurationEnvironmentVars[configuration]
13+
if (orgEnv != null) {
14+
configuration.settings.env = orgEnv
4315
}
44-
newEnv.remove(ORG_OTEL_RESOURCE_ATTRIBUTES)
45-
configuration.settings.env = newEnv
46-
} else if (configuration.settings.env.containsKey(OTEL_RESOURCE_ATTRIBUTES)) {
47-
val newEnv = mutableMapOf<String, String>()
48-
newEnv.putAll(configuration.settings.env)
49-
newEnv.remove(OTEL_RESOURCE_ATTRIBUTES)
50-
configuration.settings.env = newEnv
5116
}
5217
}
5318
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.digma.intellij.plugin.idea.execution
2+
3+
import com.intellij.execution.configurations.RunConfiguration
4+
5+
object ExternalSystemConfigurationTempStorage {
6+
7+
//used to keep existing env of configuration, so we can restore them after program runs
8+
val orgConfigurationEnvironmentVars = mutableMapOf<RunConfiguration, Map<String, String>>()
9+
10+
}

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/execution/ExternalSystemJavaParametersMerger.kt

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@ package org.digma.intellij.plugin.idea.execution
33
import com.intellij.execution.configurations.RunConfiguration
44
import com.intellij.execution.configurations.SimpleProgramParameters
55
import com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration
6+
import org.digma.intellij.plugin.idea.execution.flavor.InstrumentationFlavorType
7+
68

79
class ExternalSystemJavaParametersMerger(
810
configuration: RunConfiguration,
911
params: SimpleProgramParameters,
1012
parametersExtractor: ParametersExtractor
1113
) : JavaParametersMerger(configuration, params, parametersExtractor) {
1214

13-
override fun mergeJavaToolOptionsAndOtelResourceAttributes(instrumentedJavaToolOptions: String?, otelResourceAttributes: String?) {
15+
override fun mergeJavaToolOptionsAndOtelResourceAttributes(
16+
instrumentationFlavorType: InstrumentationFlavorType,
17+
instrumentedJavaToolOptions: String?,
18+
otelResourceAttributes: Map<String, String>
19+
) {
1420

1521
//casting must succeed or we have a bug
1622
val myConfiguration = configuration as ExternalSystemRunConfiguration
1723

18-
if (instrumentedJavaToolOptions != null) {
24+
//keep the original env, we need to restore it after program start
25+
ExternalSystemConfigurationTempStorage.orgConfigurationEnvironmentVars[configuration] = configuration.settings.env
26+
27+
28+
29+
if (!instrumentedJavaToolOptions.isNullOrBlank()) {
1930

2031
var javaToolOptions = instrumentedJavaToolOptions
2132

@@ -29,9 +40,13 @@ class ExternalSystemJavaParametersMerger(
2940
//it's probably our JAVA_TOOL_OPTIONS that was not cleaned for some reason
3041
if (currentJavaToolOptions.trim().endsWith(DIGMA_MARKER)) {
3142
newEnv.remove(JAVA_TOOL_OPTIONS)
43+
//if we decided to remove JAVA_TOOL_OPTIONS then also remove it from the saved env that we
44+
// keep for later cleaning
45+
val orgEnv = ExternalSystemConfigurationTempStorage
46+
.orgConfigurationEnvironmentVars[configuration]?.toMutableMap()
47+
orgEnv?.remove(JAVA_TOOL_OPTIONS)
3248
} else {
33-
javaToolOptions = smartMergeJavaToolOptions(javaToolOptions, currentJavaToolOptions)
34-
newEnv[ORG_JAVA_TOOL_OPTIONS] = currentJavaToolOptions
49+
javaToolOptions = smartMergeJavaToolOptions(instrumentedJavaToolOptions, currentJavaToolOptions)
3550
}
3651
}
3752

@@ -42,26 +57,68 @@ class ExternalSystemJavaParametersMerger(
4257
myConfiguration.settings.env = newEnv
4358
}
4459

45-
updateOtelResourceAttribute(myConfiguration, otelResourceAttributes)
60+
updateResourceAttribute(myConfiguration, params, instrumentationFlavorType, otelResourceAttributes)
4661
}
4762

4863

49-
private fun updateOtelResourceAttribute(configuration: ExternalSystemRunConfiguration, ourOtelResourceAttributes: String?) {
64+
private fun updateResourceAttribute(
65+
configuration: ExternalSystemRunConfiguration,
66+
params: SimpleProgramParameters,
67+
instrumentationFlavorType: InstrumentationFlavorType,
68+
ourOtelResourceAttributes: Map<String, String>
69+
) {
70+
71+
if (ourOtelResourceAttributes.isEmpty()) {
72+
return
73+
}
5074

51-
if (ourOtelResourceAttributes != null) {
5275

53-
val newEnv = configuration.settings.env.toMutableMap()
54-
val otelResourceAttributes = if (configuration.settings.env.containsKey(OTEL_RESOURCE_ATTRIBUTES)) {
55-
val currentOtelResourceAttributes = configuration.settings.env[OTEL_RESOURCE_ATTRIBUTES]
56-
newEnv[ORG_OTEL_RESOURCE_ATTRIBUTES] = currentOtelResourceAttributes
57-
currentOtelResourceAttributes.plus(",")
58-
} else {
59-
""
60-
}.plus(ourOtelResourceAttributes)
76+
when (instrumentationFlavorType) {
77+
InstrumentationFlavorType.Default,
78+
InstrumentationFlavorType.JavaServer,
79+
InstrumentationFlavorType.Micronaut,
80+
InstrumentationFlavorType.OpenLiberty,
81+
InstrumentationFlavorType.Quarkus -> updateOtelResourceAttribute(configuration, params, ourOtelResourceAttributes)
6182

62-
newEnv[OTEL_RESOURCE_ATTRIBUTES] = otelResourceAttributes
63-
configuration.settings.env = newEnv
83+
InstrumentationFlavorType.SpringBootMicrometer -> updateSpringBootMicrometerResourceAttribute(
84+
configuration,
85+
params,
86+
ourOtelResourceAttributes
87+
)
6488
}
6589
}
6690

91+
92+
private fun updateOtelResourceAttribute(
93+
configuration: ExternalSystemRunConfiguration,
94+
params: SimpleProgramParameters,
95+
ourOtelResourceAttributes: Map<String, String>
96+
) {
97+
98+
val ourOtelResourceAttributesStr = mapToFlatString(ourOtelResourceAttributes)
99+
100+
val newEnv = configuration.settings.env.toMutableMap()
101+
val currentOtelResourceAttributes = configuration.settings.env[OTEL_RESOURCE_ATTRIBUTES]
102+
val otelResourceAttributes = if (!currentOtelResourceAttributes.isNullOrBlank()) {
103+
currentOtelResourceAttributes.plus(",")
104+
} else {
105+
""
106+
}.plus(ourOtelResourceAttributesStr)
107+
108+
newEnv[OTEL_RESOURCE_ATTRIBUTES] = otelResourceAttributes
109+
configuration.settings.env = newEnv
110+
}
111+
112+
113+
private fun updateSpringBootMicrometerResourceAttribute(
114+
configuration: ExternalSystemRunConfiguration,
115+
params: SimpleProgramParameters,
116+
ourOtelResourceAttributes: Map<String, String>
117+
) {
118+
//need to replace the map because its immutable
119+
val newEnv = configuration.settings.env.toMutableMap()
120+
newEnv.putAll(ourOtelResourceAttributes)
121+
configuration.settings.env = newEnv
122+
}
123+
67124
}

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/execution/JavaParametersMerger.kt

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ package org.digma.intellij.plugin.idea.execution
22

33
import com.intellij.execution.configurations.RunConfiguration
44
import com.intellij.execution.configurations.SimpleProgramParameters
5+
import org.digma.intellij.plugin.idea.execution.flavor.InstrumentationFlavorType
6+
7+
//JavaParametersMerger and its derivatives are aware of the instrumentation flavor,
8+
// usually will treat resource attributes differently for each flavor.
9+
//ConfigurationCleaner and its derivatives always do the opposite to clean without knowledge of
10+
// the flavor relying on conventions and known properties and environment variables names.
511

612
open class JavaParametersMerger(
713
protected val configuration: RunConfiguration,
@@ -12,8 +18,12 @@ open class JavaParametersMerger(
1218

1319

1420
//default implementation that is probably ok for most configurations
15-
open fun mergeJavaToolOptionsAndOtelResourceAttributes(instrumentedJavaToolOptions: String?, otelResourceAttributes: String?) {
16-
if (instrumentedJavaToolOptions != null) {
21+
open fun mergeJavaToolOptionsAndOtelResourceAttributes(
22+
instrumentationFlavorType: InstrumentationFlavorType,
23+
instrumentedJavaToolOptions: String?,
24+
otelResourceAttributes: Map<String, String>
25+
) {
26+
if (!instrumentedJavaToolOptions.isNullOrBlank()) {
1727
var javaToolOptions = instrumentedJavaToolOptions
1828
val currentJavaToolOptions = params.env[JAVA_TOOL_OPTIONS]
1929
if (currentJavaToolOptions != null) {
@@ -22,25 +32,66 @@ open class JavaParametersMerger(
2232
params.env[JAVA_TOOL_OPTIONS] = javaToolOptions
2333
}
2434

25-
updateOtelResourceAttribute(configuration, params, otelResourceAttributes)
35+
updateResourceAttribute(configuration, params, instrumentationFlavorType, otelResourceAttributes)
2636
}
2737

2838

29-
private fun updateOtelResourceAttribute(configuration: RunConfiguration, params: SimpleProgramParameters, ourOtelResourceAttributes: String?) {
30-
31-
if (ourOtelResourceAttributes != null) {
39+
private fun updateResourceAttribute(
40+
configuration: RunConfiguration,
41+
params: SimpleProgramParameters,
42+
instrumentationFlavorType: InstrumentationFlavorType,
43+
ourOtelResourceAttributes: Map<String, String>
44+
) {
3245

33-
val mergedOtelResourceAttributes = if (params.env.containsKey(OTEL_RESOURCE_ATTRIBUTES)) {
34-
params.env[OTEL_RESOURCE_ATTRIBUTES].plus(",")
35-
} else {
36-
""
37-
}.plus(ourOtelResourceAttributes)
46+
if (ourOtelResourceAttributes.isEmpty()) {
47+
return
48+
}
3849

39-
params.env[OTEL_RESOURCE_ATTRIBUTES] = mergedOtelResourceAttributes
50+
when (instrumentationFlavorType) {
51+
InstrumentationFlavorType.Default,
52+
InstrumentationFlavorType.JavaServer,
53+
InstrumentationFlavorType.Micronaut,
54+
InstrumentationFlavorType.OpenLiberty,
55+
InstrumentationFlavorType.Quarkus -> updateOtelResourceAttribute(configuration, params, ourOtelResourceAttributes)
56+
57+
InstrumentationFlavorType.SpringBootMicrometer -> updateSpringBootMicrometerResourceAttribute(
58+
configuration,
59+
params,
60+
ourOtelResourceAttributes
61+
)
4062
}
4163
}
4264

4365

66+
private fun updateOtelResourceAttribute(
67+
configuration: RunConfiguration,
68+
params: SimpleProgramParameters,
69+
ourOtelResourceAttributes: Map<String, String>
70+
) {
71+
72+
val ourOtelResourceAttributesStr = mapToFlatString(ourOtelResourceAttributes)
73+
74+
val mergedOtelResourceAttributes = if (params.env.containsKey(OTEL_RESOURCE_ATTRIBUTES)) {
75+
params.env[OTEL_RESOURCE_ATTRIBUTES].plus(",")
76+
} else {
77+
""
78+
}.plus(ourOtelResourceAttributesStr)
79+
80+
params.env[OTEL_RESOURCE_ATTRIBUTES] = mergedOtelResourceAttributes
81+
}
82+
83+
private fun updateSpringBootMicrometerResourceAttribute(
84+
configuration: RunConfiguration,
85+
params: SimpleProgramParameters,
86+
ourOtelResourceAttributes: Map<String, String>
87+
) {
88+
//need to replace the map because its immutable
89+
val newEnv = params.env.toMutableMap()
90+
newEnv.putAll(ourOtelResourceAttributes)
91+
params.env = newEnv
92+
}
93+
94+
4495
protected fun smartMergeJavaToolOptions(myJavaToolOptions: String, currentJavaToolOptions: String): String {
4596

4697
//merge two java tool options strings. values from myJavaToolOptions override values from currentJavaToolOptions

0 commit comments

Comments
 (0)