All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metamx.emitter.core.Emitters Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/*
 * Copyright 2012 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metamx.emitter.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.metamx.common.IAE;
import com.metamx.common.ISE;
import com.metamx.common.lifecycle.Lifecycle;
import com.metamx.common.logger.Logger;
import com.metamx.emitter.core.factory.EmitterFactory;
import org.asynchttpclient.AsyncHttpClient;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Emitters
{
  private static final Logger log = new Logger(Emitters.class);

  private static final String LOG_EMITTER_PROP = "com.metamx.emitter.logging";
  private static final String HTTP_EMITTER_PROP = "com.metamx.emitter.http";
  private static final String CUSTOM_EMITTER_TYPE_PROP = "com.metamx.emitter.type";

  public static Emitter create(Properties props, AsyncHttpClient httpClient, Lifecycle lifecycle)
  {
    return create(props, httpClient, new ObjectMapper(), lifecycle);
  }

  public static Emitter create(Properties props, AsyncHttpClient httpClient, ObjectMapper jsonMapper, Lifecycle lifecycle)
  {
    Map jsonified = Maps.newHashMap();
    if (props.getProperty(LOG_EMITTER_PROP) != null) {
      jsonified = makeLoggingMap(props);
      jsonified.put("type", "logging");
    }
    else if (props.getProperty(HTTP_EMITTER_PROP) != null) {
      jsonified = makeHttpMap(props);
      jsonified.put("type", "http");
    }
    else if (props.getProperty(CUSTOM_EMITTER_TYPE_PROP) !=null) {
      jsonified = makeCustomFactoryMap(props);
    }
    else {
      throw new ISE(
          "Unknown type of emitter. Please set [%s], [%s] or provide registered subtype of com.metamx.emitter.core.factory.EmitterFactory via [%s]",
          LOG_EMITTER_PROP,
          HTTP_EMITTER_PROP,
          CUSTOM_EMITTER_TYPE_PROP
      );
    }
    return jsonMapper.convertValue(jsonified, EmitterFactory.class).makeEmitter(jsonMapper, httpClient, lifecycle);
  }

  // Package-visible for unit tests

  static Map makeHttpMap(Properties props)
  {
    Map httpMap = Maps.newHashMap();

    final String urlProperty = "com.metamx.emitter.http.url";

    final String baseUrl = props.getProperty(urlProperty);
    if (baseUrl == null) {
      throw new IAE("Property[%s] must be set", urlProperty);
    }

    httpMap.put("recipientBaseUrl", baseUrl);
    httpMap.put("flushMillis", Long.parseLong(props.getProperty("com.metamx.emitter.flushMillis", "60000")));
    httpMap.put("flushCount", Integer.parseInt(props.getProperty("com.metamx.emitter.flushCount", "300")));
    /**
     * The defaultValue for "com.metamx.emitter.http.flushTimeOut" must be same as {@link HttpEmitterConfig.DEFAULT_FLUSH_TIME_OUT}
     * */
    httpMap.put("flushTimeOut", Long.parseLong(props.getProperty("com.metamx.emitter.http.flushTimeOut", String.valueOf(Long.MAX_VALUE))));
    if (props.containsKey("com.metamx.emitter.http.basicAuthentication")) {
      httpMap.put("basicAuthentication", props.getProperty("com.metamx.emitter.http.basicAuthentication"));
    }
    if (props.containsKey("com.metamx.emitter.http.batchingStrategy")) {
      httpMap.put("batchingStrategy", props.getProperty("com.metamx.emitter.http.batchingStrategy").toUpperCase());
    }
    if (props.containsKey("com.metamx.emitter.http.maxBatchSize")) {
      httpMap.put("maxBatchSize", Integer.parseInt(props.getProperty("com.metamx.emitter.http.maxBatchSize")));
    }
    if (props.containsKey("com.metamx.emitter.http.batchQueueSizeLimit")) {
      httpMap.put("batchQueueSizeLimit", Integer.parseInt(props.getProperty("com.metamx.emitter.http.batchQueueSizeLimit")));
    }
    if (props.containsKey("com.metamx.emitter.http.httpTimeoutAllowanceFactor")) {
      httpMap.put("httpTimeoutAllowanceFactor", Float.parseFloat(props.getProperty("com.metamx.emitter.http.httpTimeoutAllowanceFactor")));
    }
    if (props.containsKey("com.metamx.emitter.http.minHttpTimeoutMillis")) {
      httpMap.put("minHttpTimeoutMillis", Float.parseFloat(props.getProperty("com.metamx.emitter.http.minHttpTimeoutMillis")));
    }
    return httpMap;
  }

  // Package-visible for unit tests
  static Map makeLoggingMap(Properties props)
  {
    Map loggingMap = Maps.newHashMap();

    loggingMap.put(
        "loggerClass", props.getProperty("com.metamx.emitter.logging.class", LoggingEmitter.class.getName())
    );
    loggingMap.put(
        "logLevel", props.getProperty("com.metamx.emitter.logging.level", "debug")
    );
    return loggingMap;
  }

  static Map makeCustomFactoryMap(Properties props)
  {
    Map factoryMap = Maps.newHashMap();
    String prefix = "com.metamx.emitter.";

    for (Map.Entry entry : props.entrySet()) {
      String key = entry.getKey().toString();
      if (key.startsWith(prefix)) {
        String combinedKey = key.substring(prefix.length());
        Map currentLevelJson = factoryMap;
        String currentKey = null;
        String[] keyPath = combinedKey.split("\\.");

        for (int i = 0; i < keyPath.length - 1; i++) {
          String keyPart = keyPath[i];
          Object nextLevelJson = currentLevelJson.get(keyPart);
          if (nextLevelJson == null) {
            nextLevelJson = new HashMap();
            currentLevelJson.put(keyPart, nextLevelJson);
          }
          currentLevelJson = (Map) nextLevelJson;
        }

        currentLevelJson.put(keyPath[keyPath.length - 1], entry.getValue());
      }
    }
    return factoryMap;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy