77import java .nio .file .Path ;
88import java .nio .file .Paths ;
99import java .nio .file .StandardCopyOption ;
10+ import java .util .ArrayList ;
11+ import java .util .List ;
1012import java .util .Map ;
1113import org .slf4j .Logger ;
1214import org .slf4j .LoggerFactory ;
@@ -16,6 +18,7 @@ enum CloudEnvironment {
1618 AZURE_FUNCTION ,
1719 AZURE_SPRING_APP ,
1820 GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN ,
21+ GOOGLE_CLOUD_RUN_FUNCTION_2ND_GEN ,
1922 UNKNOWN
2023}
2124
@@ -51,22 +54,37 @@ public static boolean isLinux() {
5154
5255 public static CloudEnvironment getEnvironment () {
5356 Map <String , String > env = System .getenv ();
57+ List <CloudEnvironment > detected = new ArrayList <>();
5458
55- if (env .get ("FUNCTIONS_EXTENSION_VERSION" ) != null &&
56- env .get ("FUNCTIONS_WORKER_RUNTIME" ) != null ) {
57- return CloudEnvironment .AZURE_FUNCTION ;
59+ if (env .get ("FUNCTIONS_EXTENSION_VERSION" ) != null
60+ && env .get ("FUNCTIONS_WORKER_RUNTIME" ) != null ) {
61+ detected . add ( CloudEnvironment .AZURE_FUNCTION ) ;
5862 }
5963
6064 if (env .get ("ASCSVCRT_SPRING__APPLICATION__NAME" ) != null ) {
61- return CloudEnvironment .AZURE_SPRING_APP ;
65+ detected . add ( CloudEnvironment .AZURE_SPRING_APP ) ;
6266 }
6367
64- if (env .get ("FUNCTION_NAME" ) != null &&
65- env .get ("GCP_PROJECT" ) != null ) {
66- return CloudEnvironment .GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN ;
68+ if (env .get ("K_SERVICE" ) != null && env .get ("FUNCTION_TARGET" ) != null ) {
69+ // Set by Google Cloud Functions for newer runtimes
70+ detected .add (CloudEnvironment .GOOGLE_CLOUD_RUN_FUNCTION_2ND_GEN );
71+ } else if (env .get ("FUNCTION_NAME" ) != null && env .get ("GCP_PROJECT" ) != null ) {
72+ // Set by Google Cloud Functions for older runtimes
73+ detected .add (CloudEnvironment .GOOGLE_CLOUD_RUN_FUNCTION_1ST_GEN );
6774 }
6875
69- return CloudEnvironment .UNKNOWN ;
76+ if (detected .isEmpty ()) {
77+ log .error ("No cloud environment detected" );
78+ return CloudEnvironment .UNKNOWN ;
79+ }
80+ if (detected .size () > 1 ) {
81+ log .error ("Multiple cloud environments detected: {}. Returning UNKNOWN." , detected );
82+ return CloudEnvironment .UNKNOWN ;
83+ }
84+
85+ CloudEnvironment environment = detected .get (0 );
86+ log .debug ("Detected cloud environment: {}" , environment );
87+ return environment ;
7088 }
7189
7290 public static String getPackageVersion () {
0 commit comments