|
| 1 | +/* |
| 2 | + * Copyright 2017 WeightWatchers |
| 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.weightwatchers.reactive.kinesis.producer |
| 18 | + |
| 19 | +import com.amazonaws.auth.AWSCredentialsProvider |
| 20 | +import com.amazonaws.regions.Regions |
| 21 | +import com.amazonaws.services.kinesis.producer.KinesisProducerConfiguration |
| 22 | +import com.amazonaws.services.kinesis.producer.KinesisProducerConfiguration.ThreadingModel |
| 23 | +import com.amazonaws.services.kinesis.producer.protobuf.Config.AdditionalDimension |
| 24 | +import com.typesafe.config.{Config, ConfigFactory} |
| 25 | + |
| 26 | +/** Typed config class for the KPL */ |
| 27 | +final case class KinesisProducerConfig( |
| 28 | + additionalMetricDimensions: List[AdditionalDimension], |
| 29 | + credentialsProvider: Option[AWSCredentialsProvider], |
| 30 | + metricsCredentialsProvider: Option[AWSCredentialsProvider], |
| 31 | + aggregationEnabled: Boolean, |
| 32 | + aggregationMaxCount: Long, |
| 33 | + aggregationMaxSize: Long, |
| 34 | + cloudwatchEndpoint: Option[String], |
| 35 | + cloudwatchPort: Long, |
| 36 | + collectionMaxCount: Long, |
| 37 | + collectionMaxSize: Long, |
| 38 | + connectTimeout: Long, |
| 39 | + credentialsRefreshDelay: Long, |
| 40 | + enableCoreDumps: Boolean, |
| 41 | + failIfThrottled: Boolean, |
| 42 | + kinesisEndpoint: Option[String], |
| 43 | + kinesisPort: Long, |
| 44 | + logLevel: String, |
| 45 | + maxConnections: Long, |
| 46 | + metricsGranularity: String, |
| 47 | + metricsLevel: String, |
| 48 | + metricsNamespace: String, |
| 49 | + metricsUploadDelay: Long, |
| 50 | + minConnections: Long, |
| 51 | + nativeExecutable: Option[String], |
| 52 | + rateLimit: Long, |
| 53 | + recordMaxBufferedTime: Long, |
| 54 | + recordTtl: Long, |
| 55 | + region: Option[Regions], |
| 56 | + requestTimeout: Long, |
| 57 | + tempDirectory: Option[String], |
| 58 | + verifyCertificate: Boolean, |
| 59 | + threadingModel: ThreadingModel, |
| 60 | + threadPoolSize: Int |
| 61 | +) { |
| 62 | + |
| 63 | + def toAwsConfig: KinesisProducerConfiguration = { |
| 64 | + val initial = new KinesisProducerConfiguration() |
| 65 | + .setAggregationEnabled(aggregationEnabled) |
| 66 | + .setAggregationMaxCount(aggregationMaxCount) |
| 67 | + .setAggregationMaxSize(aggregationMaxSize) |
| 68 | + .setCloudwatchPort(cloudwatchPort) |
| 69 | + .setCollectionMaxCount(collectionMaxCount) |
| 70 | + .setCollectionMaxSize(collectionMaxSize) |
| 71 | + .setConnectTimeout(connectTimeout) |
| 72 | + .setCredentialsRefreshDelay(credentialsRefreshDelay) |
| 73 | + .setEnableCoreDumps(enableCoreDumps) |
| 74 | + .setFailIfThrottled(failIfThrottled) |
| 75 | + .setKinesisPort(kinesisPort) |
| 76 | + .setLogLevel(logLevel) |
| 77 | + .setMaxConnections(maxConnections) |
| 78 | + .setMetricsGranularity(metricsGranularity) |
| 79 | + .setMetricsLevel(metricsLevel) |
| 80 | + .setMetricsNamespace(metricsNamespace) |
| 81 | + .setMetricsUploadDelay(metricsUploadDelay) |
| 82 | + .setMinConnections(minConnections) |
| 83 | + .setRateLimit(rateLimit) |
| 84 | + .setRecordMaxBufferedTime(recordMaxBufferedTime) |
| 85 | + .setRecordTtl(recordTtl) |
| 86 | + .setRequestTimeout(requestTimeout) |
| 87 | + .setVerifyCertificate(verifyCertificate) |
| 88 | + .setThreadingModel(threadingModel) |
| 89 | + .setThreadPoolSize(threadPoolSize) |
| 90 | + |
| 91 | + KinesisProducerConfig.setAdditionalDimensions(initial, additionalMetricDimensions) |
| 92 | + |
| 93 | + // This is ugly |
| 94 | + val wCredProv = credentialsProvider.fold(initial)(initial.setCredentialsProvider) |
| 95 | + val wMetricCredProv = |
| 96 | + metricsCredentialsProvider.fold(wCredProv)(wCredProv.setMetricsCredentialsProvider) |
| 97 | + val wCWEP = cloudwatchEndpoint.fold(wMetricCredProv)(wMetricCredProv.setCloudwatchEndpoint) |
| 98 | + val wKinesisEP = kinesisEndpoint.fold(wCWEP)(wCWEP.setKinesisEndpoint) |
| 99 | + val wNativeExec = nativeExecutable.fold(wKinesisEP)(wKinesisEP.setNativeExecutable) |
| 100 | + val wRegion = region.fold(wNativeExec)(reg => wNativeExec.setRegion(reg.getName)) |
| 101 | + val wTempDir = tempDirectory.fold(wRegion)(wRegion.setTempDirectory) |
| 102 | + |
| 103 | + wTempDir |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +object KinesisProducerConfig { |
| 108 | + def apply(): KinesisProducerConfig = default |
| 109 | + def apply(config: KinesisProducerConfiguration): KinesisProducerConfig = fromAwsConfig(config) |
| 110 | + def apply(config: Config): KinesisProducerConfig = |
| 111 | + fromAwsConfig(ProducerConf.buildKPLConfig(config, None)) |
| 112 | + def apply(config: Config, credentialsProvider: AWSCredentialsProvider): KinesisProducerConfig = |
| 113 | + fromAwsConfig(ProducerConf.buildKPLConfig(config, Some(credentialsProvider))) |
| 114 | + |
| 115 | + // Sets default values as if no typesafe configuration was passed. This ensures that the default |
| 116 | + // KinesisProducerConfiguration is used |
| 117 | + def default: KinesisProducerConfig = { |
| 118 | + val defaultConfig = ProducerConf.buildKPLConfig(ConfigFactory.empty(), None) |
| 119 | + fromAwsConfig(defaultConfig) |
| 120 | + } |
| 121 | + |
| 122 | + private def fromAwsConfig(config: KinesisProducerConfiguration): KinesisProducerConfig = |
| 123 | + KinesisProducerConfig( |
| 124 | + additionalMetricDimensions = List(), // No way to retrieve this from a KinesisProducerConfiguration |
| 125 | + credentialsProvider = Some(config.getCredentialsProvider), |
| 126 | + metricsCredentialsProvider = Some(config.getMetricsCredentialsProvider), |
| 127 | + aggregationEnabled = config.isAggregationEnabled, |
| 128 | + aggregationMaxCount = config.getAggregationMaxCount, |
| 129 | + aggregationMaxSize = config.getAggregationMaxSize, |
| 130 | + cloudwatchEndpoint = Some(config.getCloudwatchEndpoint), |
| 131 | + cloudwatchPort = config.getCloudwatchPort, |
| 132 | + collectionMaxCount = config.getCollectionMaxCount, |
| 133 | + collectionMaxSize = config.getCollectionMaxSize, |
| 134 | + connectTimeout = config.getConnectTimeout, |
| 135 | + credentialsRefreshDelay = config.getCredentialsRefreshDelay, |
| 136 | + enableCoreDumps = config.isEnableCoreDumps, |
| 137 | + failIfThrottled = config.isFailIfThrottled, |
| 138 | + kinesisEndpoint = Some(config.getKinesisEndpoint), |
| 139 | + kinesisPort = config.getKinesisPort, |
| 140 | + logLevel = config.getLogLevel, |
| 141 | + maxConnections = config.getMaxConnections, |
| 142 | + metricsGranularity = config.getMetricsGranularity, |
| 143 | + metricsLevel = config.getMetricsLevel, |
| 144 | + metricsNamespace = config.getMetricsNamespace, |
| 145 | + metricsUploadDelay = config.getMetricsUploadDelay, |
| 146 | + minConnections = config.getMinConnections, |
| 147 | + nativeExecutable = Some(config.getNativeExecutable), |
| 148 | + rateLimit = config.getRateLimit, |
| 149 | + recordMaxBufferedTime = config.getRecordMaxBufferedTime, |
| 150 | + recordTtl = config.getRecordTtl, |
| 151 | + region = config.getRegion match { |
| 152 | + case x if x.isEmpty => None |
| 153 | + case x => Some(Regions.fromName(x)) |
| 154 | + }, |
| 155 | + requestTimeout = config.getRequestTimeout, |
| 156 | + tempDirectory = Some(config.getTempDirectory), |
| 157 | + verifyCertificate = config.isVerifyCertificate, |
| 158 | + threadingModel = config.getThreadingModel, |
| 159 | + threadPoolSize = config.getThreadPoolSize |
| 160 | + ) |
| 161 | + |
| 162 | + private def setAdditionalDimensions( |
| 163 | + conf: KinesisProducerConfiguration, |
| 164 | + dimensions: List[AdditionalDimension] |
| 165 | + ) = dimensions.foldLeft(conf) { (conf, dimension) => |
| 166 | + conf.addAdditionalMetricsDimension(dimension.getKey, |
| 167 | + dimension.getValue, |
| 168 | + dimension.getGranularity) |
| 169 | + conf |
| 170 | + } |
| 171 | +} |
0 commit comments