1 /*
2  * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.monitoring;
16
17 import com.amazonaws.SdkClientException;
18 import com.amazonaws.annotation.ThreadSafe;
19
20 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_CLIENT_ID_ENV_VAR;
21 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_ENABLED_ENV_VAR;
22 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_HOST_ENV_VAR;
23 import static com.amazonaws.SDKGlobalConfiguration.AWS_CSM_PORT_ENV_VAR;
24 import static com.amazonaws.SDKGlobalConfiguration.DEFAULT_AWS_CSM_CLIENT_ID;
25 import static com.amazonaws.SDKGlobalConfiguration.DEFAULT_AWS_CSM_PORT;
26 import static com.amazonaws.SDKGlobalConfiguration.DEFAULT_AWS_CSM_HOST;
27
28 /**
29  * Configuration provider that sources the client side monitoring
30  * configuration parameters from environment variables.
31  *
32  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_CLIENT_ID_ENV_VAR
33  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_ENABLED_ENV_VAR
34  * @see com.amazonaws.SDKGlobalConfiguration#AWS_CSM_PORT_ENV_VAR
35  */

36 @ThreadSafe
37 public final class EnvironmentVariableCsmConfigurationProvider implements CsmConfigurationProvider {
38     @Override
39     public CsmConfiguration getConfiguration() {
40         String enabled = System.getenv(AWS_CSM_ENABLED_ENV_VAR);
41
42         if (enabled == null) {
43             throw new SdkClientException("Unable to load Client Side Monitoring configurations from"
44                                          + " environment variables!");
45         }
46
47         String host = System.getenv(AWS_CSM_HOST_ENV_VAR);
48         host = host == null ? DEFAULT_AWS_CSM_HOST : host;
49         String port = System.getenv(AWS_CSM_PORT_ENV_VAR);
50         String clientId = System.getenv(AWS_CSM_CLIENT_ID_ENV_VAR);
51         clientId = clientId == null ? DEFAULT_AWS_CSM_CLIENT_ID : clientId;
52
53         try {
54             int portNumber = port == null ? DEFAULT_AWS_CSM_PORT : Integer.parseInt(port);
55             return CsmConfiguration.builder()
56                     .withEnabled(Boolean.parseBoolean(enabled))
57                     .withHost(host)
58                     .withPort(portNumber)
59                     .withClientId(clientId)
60                     .build();
61         } catch (Exception e) {
62             throw new SdkClientException("Unable to load Client Side Monitoring configurations from"
63                     + " environment variables!", e);
64         }
65     }
66 }
67