Search in sources :

Example 1 with AppInfoImpl

use of com.nike.riposte.server.config.impl.AppInfoImpl in project riposte by Nike-Inc.

the class AwsUtil method getAppInfoFutureWithAwsInfo.

/**
 * @param appId The app ID for the running application.
 * @param environment The environment the application is running in (local, test, prod, etc).
 * @param asyncHttpClientHelper The async HTTP client you want this method to use to make the AWS metadata calls.
 *
 * @return A {@link CompletableFuture} that will eventually yield an {@link AppInfo} with the values coming from the
 * given arguments for app ID and environment, and coming from the AWS metadata services for datacenter and instance
 * ID. If the given environment is "local" then {@link AppInfoImpl#createLocalInstance(String)} will be returned
 * (see the javadocs of that method for more information on what values it will contain). Otherwise the AWS metadata
 * services will be used to determine {@link AppInfo#dataCenter()} and {@link AppInfo#instanceId()}. If those AWS
 * metadata calls fail for any reason then {@link AppInfo#UNKNOWN_VALUE} will be used instead.
 */
public static CompletableFuture<AppInfo> getAppInfoFutureWithAwsInfo(String appId, String environment, AsyncHttpClientHelper asyncHttpClientHelper) {
    if ("local".equalsIgnoreCase(environment) || "compiletimetest".equalsIgnoreCase(environment)) {
        AppInfo localAppInfo = AppInfoImpl.createLocalInstance(appId);
        logger.info("Local environment. Using the following data for AppInfo. " + "appId={}, environment={}, dataCenter={}, instanceId={}", localAppInfo.appId(), localAppInfo.environment(), localAppInfo.dataCenter(), localAppInfo.instanceId());
        return CompletableFuture.completedFuture(AppInfoImpl.createLocalInstance(appId));
    }
    // Not local, so assume AWS.
    CompletableFuture<String> dataCenterFuture = getAwsRegion(asyncHttpClientHelper);
    CompletableFuture<String> instanceIdFuture = getAwsInstanceId(asyncHttpClientHelper);
    return CompletableFuture.allOf(dataCenterFuture, instanceIdFuture).thenApply((aVoid) -> {
        String dataCenter = dataCenterFuture.join();
        String instanceId = instanceIdFuture.join();
        if (AppInfo.UNKNOWN_VALUE.equals(instanceId)) {
            try {
                instanceId = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                logger.error("An error occurred trying to use local hostname as fallback. " + "Using 'unknown' as the fallback's fallback.", e);
            }
        }
        logger.info("Non-local environment. Using the following data for AppInfo. " + "appId={}, environment={}, dataCenter={}, instanceId={}", appId, environment, dataCenter, instanceId);
        return new AppInfoImpl(appId, environment, dataCenter, instanceId);
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) AppInfoImpl(com.nike.riposte.server.config.impl.AppInfoImpl) AppInfo(com.nike.riposte.server.config.AppInfo)

Aggregations

AppInfo (com.nike.riposte.server.config.AppInfo)1 AppInfoImpl (com.nike.riposte.server.config.impl.AppInfoImpl)1 UnknownHostException (java.net.UnknownHostException)1