Getting Started
Amberflo SDKs

Go

18min
links github https //github com/amberflo/metering go releases https //github com/amberflo/metering go/releases package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"os" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/rs/zerolog" &#x9;"github com/xtgo/uuid" ) func main() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid "1234", &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, close all channels, and shutdown the client &#x9;meteringclient shutdown() } func usage() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]string) &#x9;filter\["customerid"] = "1234" &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } func setupcustomer() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe &#x9;//and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//instantiate a new metering client &#x9;customerclient = metering newcustomerclient(apikey) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;//traits are optional traits can be used as filters or aggregation buckets &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } func createclientwithcustomerlogger() { &#x9;//obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;metering = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) } func queryusagecost(){ //obtain your amberflo api key &#x9;apikey = "my api key" &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient(apikey) //set the start time of the time range in epoch seconds starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} //specify the limit and sort order &#x9;takeforcost = \&metering take{ &#x9; limit 10, &#x9; isascending false, &#x9;} &#x9;// example 1 group by customers &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9;}) &#x9;fmt println("usage cost result") &#x9;printusagecostdata( usagecostresult, err) &#x9;//example 2 filter for a cost for specific customer &#x9;//setup usage query params &#x9;filterforcost = make(map\[string]\[]string) &#x9;filterforcost\["customerid"] = \[]string{customerid} &#x9;usagecostresult, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; take takeforcost, &#x9; filters filterforcost, &#x9;}) &#x9;fmt println("usage cost for specific customer") &#x9;printusagecostdata( usagecostresult, err) } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) }{"success"\ true} quick start go get github com/amberflo/metering go/v2\@v2 0 1 ingesting meters ingest meter records docid\ t8ybtn9tmllhzvbed2w2p overview docid\ m8nhua0puamjra p9czsy package main import ( &#x9;"fmt" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/xtgo/uuid" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;//optional ingest options &#x9;//frequency at which queued data will be sent to api default is 1 second &#x9;intervalseconds = 30 time second &#x9;//number of messages posted to the api default is 100 &#x9;batchsize = 5 &#x9;//debug mode logging default is false &#x9;debug = true &#x9;//instantiate a new metering client &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withbatchsize(batchsize), &#x9; metering withintervalseconds(intervalseconds), &#x9; metering withdebug(debug), &#x9;) &#x9;customerid = "dell 10" &#x9;//define dimesions for your meters dimensions can be used as filters &#x9;dimensions = make(map\[string]string) &#x9;dimensions\["region"] = "midwest" &#x9;dimensions\["customertype"] = "tech" &#x9;for i = 0; i < 50; i++ { &#x9; utcmillis = time now() unixnano() / int64(time millisecond) &#x9; //queue meter messages for ingestion &#x9; //queue will be flushed asyncrhonously when metering batchsize is exceeded &#x9; //or periodically at metering intervalseconds &#x9; //unique id is optional, but setting it &#x9; //helps with de dupe and revoking an ingested meter &#x9; uniqueid = uuid newrandom() string() &#x9; meteringerror = meteringclient meter(\&metering metermessage{ &#x9; uniqueid uniqueid, &#x9; meterapiname "apicalls from go", &#x9; customerid customerid, &#x9; metervalue float64(i) + 234 0, &#x9; metertimeinmillis utcmillis, &#x9; dimensions dimensions, &#x9; }) &#x9; if meteringerror != nil { &#x9; fmt println("metering error ", meteringerror) &#x9; } &#x9; time sleep(500 time millisecond) &#x9;} &#x9;//perform graceful shutdown &#x9;//flush all messages in the queue, stop the timer, &#x9;//close all channels, and shutdown the client &#x9;meteringclient shutdown() } 📘 cancel an ingested meter cancel an ingested meter a meter can be cancelled by resending the same ingestion event and setting metering cancelmeter dimension to "true" see code below cancel ingested meter dimensions\[metering cancelmeter] = "true"&#x9; //cancel an ingested meter meteringerror = metering meter(\&metering metermessage{ uniqueid uniqueid, meterapiname "apicalls from go", customerid customerid, metervalue metervalue, metertimeinmillis utcmillis, dimensions dimensions, }) query usage query the usage data docid\ smvdzw1fyu6jwyl5ufqrd package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;customerid = "dell 8" &#x9;//initialize the usage client &#x9;usageclient = metering newusageclient( &#x9; apikey, &#x9; // metering withcustomlogger(customerlogger), &#x9;) &#x9;//set the start time of the time range in epoch seconds &#x9;starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} &#x9;//specify the limit and sort order &#x9;take = \&metering take{ &#x9; limit 10, &#x9; isascending true, &#x9;} &#x9;// example 1 group by customers for a specific meter and all customers &#x9;// setup usage query params &#x9;// visit following link for description of payload &#x9;// https //amberflo readme io/reference#usage &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9;}) &#x9;fmt println("usage by meterapiname in json format") &#x9;printusagedata( usageresult, err) &#x9;//example 2 filter for a meter for specific customer &#x9;//setup usage query params &#x9;filter = make(map\[string]\[]string) &#x9;filter\["customerid"] = \[]string{customerid} &#x9;usageresult, err = usageclient getusage(\&metering usagepayload{ &#x9; meterapiname "apicalls from go", &#x9; aggregation metering sum, &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"customerid"}, &#x9; timerange timerange, &#x9; filter filter, &#x9;}) &#x9;fmt println("usage for meter for specific customer in json format") &#x9;printusagedata( usageresult, err) } func printusagedata(usageresult metering detailedmeteraggregation, err error) { &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usageresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } manage customers create a customer docid\ iwtzya4jwllxbgp3qazsl package main import ( &#x9;"fmt" &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;customerid = "dell 8" &#x9;//automatically create customer in stripe //and add stripeid to traits &#x9;createcustomerinstripe = true &#x9;//initialize the customer client &#x9;customerclient = metering newcustomerclient( &#x9; apikey, &#x9; //metering withcustomlogger(customerlogger), &#x9;) &#x9;//check if customer exists &#x9;customer, err = customerclient getcustomer(customerid) &#x9;if err != nil { &#x9; fmt println("error getting customer details ", err) &#x9;} &#x9;//setup customer &#x9;if customer != nil { &#x9; //customer exists &#x9; //update properties &#x9; customer customername = "dell 2" &#x9;} else { &#x9; //setup new customer &#x9; //traits are optional traits can be used as filters or aggregation buckets &#x9; traits = make(map\[string]string) &#x9; traits\["region"] = "us west" &#x9; traits\["customertype"] = "tech" &#x9; //in case createcustomerinstripe is false, set the trait for stripeid &#x9; //traits\[metering stripetraitkey] = "cus lvxxpbqvyn3v49" &#x9; //set the aws marketplace id trait &#x9; //traits\[metering awsmarketplacetraitkey] = "aws marketplace id" &#x9; customer = \&metering customer{ &#x9; customerid customerid, &#x9; customername "dell", &#x9; customeremail "test\@dell com", &#x9; traits traits, &#x9; enabled true, &#x9; } &#x9;} &#x9;customer, err = customerclient addorupdatecustomer(customer, createcustomerinstripe) &#x9;if err != nil { &#x9; fmt println("error creating customer details ", err) &#x9;} &#x9;customerstatus = fmt sprintf("stripe id for customer %s", customer traits\[metering stripetraitkey]) &#x9;fmt println(customerstatus) } custom logger by default, metering go uses the default go logger you can inject your own logger by implementing the following interface logger type logger interface { &#x9;log(v interface{}) &#x9;logf(format string, v interface{}) } define the custom logger package main import ( &#x9;"fmt" &#x9;"os" &#x9;"github com/rs/zerolog" ) type customlogger struct { &#x9;logger zerolog logger } func newcustomlogger() customlogger { &#x9;loglevel = zerolog debuglevel &#x9;zerolog setgloballevel(loglevel) &#x9;logger = zerolog new(os stdout) with() timestamp() logger() &#x9;return \&customlogger{logger \&logger} } func (l customlogger) log(args interface{}) { &#x9;msg = fmt sprintln(args ) &#x9;l logger debug() msg(msg) } func (l customlogger) logf(format string, args interface{}) { &#x9;l logger debug() msgf(format, args ) } instantiate metering client with custom logger package main import ( &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;customerlogger = newcustomlogger() &#x9;//instantiate a new metering client with custom logger &#x9;meteringclient = metering newmeteringclient( &#x9; apikey, &#x9; metering withlogger(customerlogger), &#x9;) &#x9;//initialize the usage client with custom logger &#x9;usageclient = metering newusageclient( &#x9; apikey, &#x9; metering withcustomlogger(customerlogger), &#x9;) &#x9;//initialize the usage cost client with custom logger &#x9;usagecostclient = metering newusagecostclient( &#x9; apikey, &#x9; metering withcustomlogger(customerlogger), &#x9;) } query usage cost with paging usage cost docid\ w 5buluc37bavepvieuf4 package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;customerid = "dell 8" &#x9;//initialize the usage cost client &#x9;usagecostclient = metering newusagecostclient( &#x9; apikey, &#x9; // metering withcustomlogger(customerlogger), &#x9;) &#x9;//set the start time of the time range in epoch seconds &#x9;starttimeinseconds = (time now() unixnano() / int64(time second)) (24 60 60) &#x9;timerange = \&metering timerange{ &#x9; starttimeinseconds starttimeinseconds, &#x9;} &#x9;// paging &#x9;pageindex = int64(1) &#x9;page = \&metering page{ &#x9; number pageindex, &#x9; size 2, &#x9;} &#x9;// example 1 group by product plan &#x9;// setup usage cost query params &#x9;// visit following link for description of payload &#x9;// https //docs amberflo io/reference/post payments cost usage cost &#x9;for pageindex < 5 { &#x9; usagecostresultforpage, err = usagecostclient getusagecost(\&metering usagecostskey{ &#x9; timegroupinginterval metering day, &#x9; groupby \[]string{"product plan id"}, &#x9; timerange timerange, &#x9; page page, &#x9; }) &#x9; fmt println("usage cost result for page ", pageindex) &#x9; printusagecostdata( usagecostresultforpage, err) &#x9; //increment the page number &#x9; pageindex = pageindex + 1 &#x9; //obtain total pages from result and stop if limit reached &#x9; if usagecostresultforpage pageinfo totalpages < pageindex { &#x9; break &#x9; } &#x9; page number = pageindex &#x9; //a token from a previous query page result to track pages and improve performance &#x9; pagetoken = usagecostresultforpage pageinfo pagetoken &#x9; page token = pagetoken &#x9;} } func printusagecostdata(usagecostresult metering usagecosts, err error) { &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;jsonstring, err = json marshalindent(usagecostresult, "", " ") &#x9;if err != nil { &#x9; fmt println("usage cost error ", err) &#x9; return &#x9;} &#x9;fmt println(string(jsonstring)) } pricing plans assign a product plan to a customer docid 0jdvdpttwte czaskhp n package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() {&#x9; &#x9;customerid = "dell 8" &#x9;//assign pricing plan to customer &#x9;productplanid = "8e880691 1ae8 493b b0a7 12a71e5dfcca" &#x9;customerpricingclient = metering newcustomerpricingplanclient(apikey) &#x9;customerpricingplan, err = customerpricingclient addorupdate(\&metering customerproductplan{ &#x9; productplanid productplanid, &#x9; customerid customerid, &#x9; starttimeinseconds (time now() unixnano() / int64(time second)), &#x9;}) &#x9;if err != nil { &#x9; fmt println("error assigning customer plan ", err) &#x9;} &#x9;pricingstatus = fmt sprintf("customer pricing plan %s assigned to customer %s", customerpricingplan productplanid, customerid) &#x9;fmt println(pricingstatus) } prepaid client order a one time or a recurrence prepaid card by price docid 0mhimkgho6rpx6lefiwpf package main import ( &#x9;"fmt" &#x9;"time" &#x9;"github com/amberflo/metering go/v2" &#x9;"github com/xtgo/uuid" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;customerid = "dell 1800" &#x9;starttimeinseconds = (time now() unixnano() / int64(time second)) (1 24 60 60) &#x9;// &#x9;// &#x9;//prepaid sdk &#x9;//initialize the prepaidclient &#x9;prepaidclient = metering newprepaidclient( &#x9; apikey, &#x9; //use a custom logger &#x9; //metering withcustomlogger(customerlogger), for custom logger &#x9;) &#x9;recurrencefrequency = \&metering billingperiod{ &#x9; interval metering day, &#x9; intervalscount 1, &#x9;} &#x9;prepaidorder = \&metering customerprepaid{ &#x9; id uuid newrandom() string(), &#x9; customerid customerid, &#x9; externalpayment true, &#x9; starttimeinseconds starttimeinseconds, &#x9; prepaidprice 123, &#x9; prepaidofferversion 1, &#x9; recurrencefrequency recurrencefrequency, &#x9;} &#x9;// create a prepaid order &#x9;prepaidorder, err = prepaidclient createprepaidorder(prepaidorder) &#x9;if err != nil { &#x9; fmt println("prepaid api error ", err) &#x9; return &#x9;} &#x9;// get a list of all active prepaid orders &#x9;prepaidorders, err = prepaidclient getactiveprepaidorders(customerid) &#x9;if err != nil { &#x9; fmt println("prepaid api error ", err) &#x9; return &#x9;} &#x9;// update the external payment status of a prepaid order &#x9;externalprepaidpaymentstatus = \&metering externalprepaidpaymentstatus{ &#x9; paymentstatus metering settled, &#x9; systemname "stripe", &#x9; paymentid "payment id 1", &#x9; paymenttimeinseconds (time now() unixnano() / int64(time second)), &#x9; prepaiduri prepaidorder firstinvoiceuri, &#x9;} &#x9;externalprepaidpaymentstatus, err = prepaidclient updateexternalprepaidstatus(externalprepaidpaymentstatus) &#x9;// delete a prepaid order &#x9;err = prepaidclient deleteprepaidorder(prepaidorder id, customerid) &#x9;if err != nil { &#x9; fmt println("prepaid api error ", err) &#x9; return &#x9;} } signals create a notification docid\ f6y3dmmq5u0ffdwbcxkjq guide https //app archbee com/docs/ k7r5cdr2dsk3glzqxty8/lb lfrukrlxcvuhxxyz1h package main import ( &#x9;"encoding/json" &#x9;"fmt" &#x9;"github com/amberflo/metering go/v2" ) //obtain your amberflo api key var apikey = "my api key" func main() { &#x9;signalsclient = metering newsignalsclient( &#x9; apikey, &#x9; //use a custom logger &#x9; //metering withcustomlogger(customlogger), &#x9;) &#x9;invoicealert = \&metering notification{ &#x9; name "invoice tracker alert", &#x9; notificationtype metering invoice, &#x9; email \[]string{"amberflo tester\@gmail com"}, &#x9; thresholdvalue "200", &#x9; customerfiltermode metering percustomer, &#x9; enabled true, &#x9;} &#x9;//create a new signal &#x9;invoicealert, err = signalsclient createsignal(invoicealert) &#x9;if err != nil { &#x9; fmt println("api error ", err) &#x9; return &#x9;} &#x9;//update an existing signal &#x9;invoicealert thresholdvalue = "150" &#x9;invoicealert enabled = false //disable a signal without deleting &#x9;invoicealert, err = signalsclient updatesignal(invoicealert) &#x9;if err != nil { &#x9; fmt println("api error ", err) &#x9; return &#x9;} &#x9;//get an existing signal &#x9;invoicealert, err = signalsclient getsignal(invoicealert id) &#x9;if err != nil { &#x9; fmt println("api error ", err) &#x9; return &#x9;} &#x9;//delete a signal &#x9;invoicealert, err = signalsclient deletesignal(invoicealert id) &#x9;if err != nil { &#x9; fmt println("api error ", err) &#x9; return &#x9;} &#x9;fmt println("signal with following id deleted ", invoicealert id) } batching records amberflo io libraries are built to support high throughput environments that means you can safely send hundreds of meter records per second for example, you can chose to deploy it on a web server that is serving hundreds of requests per second however, every call does not result in a http request, but is queued in memory instead messages are batched and flushed in the background, allowing for much faster operation the size of batch and rate of flush can be customized defaults the library will flush every 100 messages (configuration parameter batchsize ) or if 1 second has passed since the last flush (configuration parameter intervalseconds) there is a maximum of 500kb per batch request and 32kb per call flush on demand (blocking call) you can flush on demand for example, at the end of your program, you’ll want to flush to make sure there’s nothing left in the queue just call the flush method metering flush() please note calling this method will block the calling thread until there are no messages left in the queue so, you’ll want to use it as part of your cleanup scripts and avoid using it as part of the request lifecycle