/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/aws-cloudwatch-statsd-backend-multiregion@1.0.0/lib/aws-cloudwatch-statsd-backend.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var util=require("util"),AWS=require("aws-sdk");function CloudwatchBackend(e,t,i){var n=this;function s(){n.cloudwatch=new AWS.CloudWatch(n.config),i.on("flush",(function(e,t){n.flush(e,t)}))}this.config=t||{},AWS.config=this.config,this.config.iamRole?"any"==this.config.iamRole?(ms=new AWS.EC2MetadataCredentials,ms.refresh((function(e){e&&console.log("Failed to fetch IAM role credentials: "+e),n.config.credentials=ms,s()}))):(ms=new AWS.MetadataService,ms.request("/latest/meta-data/iam/security-credentials/"+this.config.iamRole,(function(e,t){var i=JSON.parse(t);e&&console.log("Failed to fetch IAM role credentials: "+e),n.config.credentials=new AWS.Credentials(i.AccessKeyId,i.SecretAccessKey,i.Token),s()}))):s()}CloudwatchBackend.prototype.processKey=function(e){var t=e.split(/[\.\/-]/);return{metricName:t[t.length-1],namespace:t.length>1?t.splice(0,t.length-1).join("/"):null}},CloudwatchBackend.prototype.flush=function(e,t){console.log("Flushing metrics at "+new Date(1e3*e).toISOString());var i=t.counters,n=t.gauges,s=t.timers,c=t.sets;for(key in i)if(0!=key.indexOf("statsd."))if(this.config.whitelist&&this.config.whitelist.length>0&&-1==this.config.whitelist.indexOf(key))console.log("Key (counter) "+key+" not in whitelist");else{var o=this.config.processKeyForNamespace?this.processKey(key):{},a=this.config.namespace||o.namespace||"AwsCloudWatchStatsdBackend",l=this.config.metricName||o.metricName||key;this.cloudwatch.putMetricData({MetricData:[{MetricName:l,Unit:"Count",Timestamp:new Date(1e3*e).toISOString(),Value:i[key]}],Namespace:a},(function(e,t){console.log(util.inspect(e)),console.log(util.inspect(t))}))}for(key in s)if(s[key].length>0){if(this.config.whitelist&&this.config.whitelist.length>0&&-1==this.config.whitelist.indexOf(key)){console.log("Key (counter) "+key+" not in whitelist");continue}for(var r=s[key].sort((function(e,t){return e-t})),h=r.length,g=r[0],u=r[h-1],f=[g],m=1;m<h;m++)f.push(r[m]+f[m-1]);var p;(p=f[h-1])/h;o=this.config.processKeyForNamespace?this.processKey(key):{},a=this.config.namespace||o.namespace||"AwsCloudWatchStatsdBackend",l=this.config.metricName||o.metricName||key;this.cloudwatch.putMetricData({MetricData:[{MetricName:l,Unit:"Milliseconds",Timestamp:new Date(1e3*e).toISOString(),StatisticValues:{Minimum:g,Maximum:u,Sum:p,SampleCount:h}}],Namespace:a},(function(e,t){console.log(util.inspect(e)),console.log(util.inspect(t))}))}for(key in n)if(this.config.whitelist&&this.config.whitelist.length>0&&-1==this.config.whitelist.indexOf(key))console.log("Key (counter) "+key+" not in whitelist");else{o=this.config.processKeyForNamespace?this.processKey(key):{},a=this.config.namespace||o.namespace||"AwsCloudWatchStatsdBackend",l=this.config.metricName||o.metricName||key;this.cloudwatch.putMetricData({MetricData:[{MetricName:l,Unit:"None",Timestamp:new Date(1e3*e).toISOString(),Value:n[key]}],Namespace:a},(function(e,t){console.log(util.inspect(e)),console.log(util.inspect(t))}))}for(key in c)if(this.config.whitelist&&this.config.whitelist.length>0&&-1==this.config.whitelist.indexOf(key))console.log("Key (counter) "+key+" not in whitelist");else{o=this.config.processKeyForNamespace?this.processKey(key):{},a=this.config.namespace||o.namespace||"AwsCloudWatchStatsdBackend",l=this.config.metricName||o.metricName||key;this.cloudwatch.putMetricData({MetricData:[{MetricName:l,Unit:"None",Timestamp:new Date(1e3*e).toISOString(),Value:c[key].values().length}],Namespace:a},(function(e,t){console.log(util.inspect(e)),console.log(util.inspect(t))}))}},exports.init=function(e,t,i){var n=t.cloudwatch||{},s=n.instances||[n];for(key in s){instanceConfig=s[key],console.log("Starting cloudwatch reporter instance in region:",instanceConfig.region);new CloudwatchBackend(e,instanceConfig,i)}return!0};
//# sourceMappingURL=/sm/f956e318dcebe73199da4649ac4c578ac4fd1c4feb4574ee5a822394bcb014c3.map