@@ -3,19 +3,30 @@ package org.digma.intellij.plugin.idea.execution
33import com.intellij.execution.configurations.RunConfiguration
44import com.intellij.execution.configurations.SimpleProgramParameters
55import com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration
6+ import org.digma.intellij.plugin.idea.execution.flavor.InstrumentationFlavorType
7+
68
79class 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}
0 commit comments