Giter Club home page Giter Club logo

resty-counter's Introduction

resty-counter

Simple high-performance hit/impression counter

resty-counter is a userful/powerful SaaS tool. Every SaaS will need something cheap to track hit/impression and provide reporting to the client.

We use redis to provide live counter data. The formula for getting live counter is:

{aggregated log data} + {redis data} = {!!! profit !!!}

  • daily backup to s3
  • weekly update of maxmind geoip2 database

TECH STACK

  • openresty - use to create daily log files in the format "yyyy-mm-dd-access.log" and daily cron sync to "s3://bucket-name/year=yyyy/month=mm/day=dd/yyyy-mm-dd-access.log"
  • redis - store 24-48 hours worth of counter. This allow for 0-24 hours delay between log processing.
  • lua-resty-auto-ssl - for letsencrypt certificate; because, you know, the web is more secure now?

USAGE

To create impression, simply hit the pixel url:

https://pi.example.com/p/tenantcode/counterid
# response: empty gif/pixel

To lookup impression/count in the last 24-48 hours:

https://pi.example.com/p-query/tenantcode/counterid?apikey=resty-counter
# response: todayCount, yesterdayCount

Docker environment variable and examples:

--env ALLOWED_DOMAINS='(pi.example.com|pixel.example.com)' \
--env AWS_ACCESS_KEY_ID=<<YOUR_ACCESS_KEY>> \
--env AWS_SECRET_ACCESS_KEY=<<YOUR_SECRET_ACCESS>> \
--env AWS_DEFAULT_REGION=us-west-2 \
--env AWS_PATH=bucketname/rootfolder
--env REDIS_HOST=redis-host-name
--env REDIS_AUTH=redis-auth-password
--env REDIS_EXPIRE_DAYS=3
--env API_KEY=pass-this-in-querystring-as-{apikey}
  • REDIS_EXPIRE_DAYS - (default is 3) - set this to a number of days you want to store your counter. Example where you want to store it for, let say 1 month or 31 days max. This allow you to write some custom redis query to get your entire counter value from redis. Set to any value greater than 9999 days to disable expiration.

Suggestion

You can setup something with AWS Lambda to trigger on s3 event and immediately process the log file. Of course, once you hit a certain threshold, there are many limitations with Lambda. The tip/trick here is to use AWS Athena since we already sending into s3 in the format that can be consumed by Athena.

NOTE

This tool was not created for tracking user on the internet; though, it is possible to do so. You would have to modify the nginx config and turn on userid stuff, see: http://nginx.org/en/docs/http/ngx_http_userid_module.html

Alternative, you can piggyback your user tracking pixel using our forward/redirect (redir query parameter) feature: https://github.com/niiknow/resty-counter/blob/master/rootfs/usr/local/openresty/nginx/utils.lua#L11

MIT

resty-counter's People

Contributors

noogen avatar

Stargazers

 avatar

Watchers

 avatar  avatar

resty-counter's Issues

maxmind geoip database was discontinued

https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 404s

Step 6/8 : RUN cd /tmp     && curl -fSL https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -o /tmp/GeoLiteCity.dat.gz     && gunzip -f GeoLiteCity.dat.gz     && rm -f /usr/local/openresty/nginx/conf/app/GeoLiteCity.dat     && mv GeoLiteCity.dat /usr/local/openresty/nginx/conf/app/GeoLiteCity.dat     && chown -R nginx:nginx /usr/local/openresty/nginx/conf/     && mkdir -p /usr/local/openresty/nginx/conf-bak     && rsync --update -raz /usr/local/openresty/nginx/conf/* /usr/local/openresty/nginx/conf-bak     && rm -rf /var/cache/apk/* /tmp/*
 ---> Running in a8750f6079d7
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404 
ERROR: Service 'app' failed to build: The command '/bin/sh -c cd /tmp     && curl -fSL https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -o /tmp/GeoLiteCity.dat.gz     && gunzip -f GeoLiteCity.dat.gz     && rm -f /usr/local/openresty/nginx/conf/app/GeoLiteCity.dat     && mv GeoLiteCity.dat /usr/local/openresty/nginx/conf/app/GeoLiteCity.dat     && chown -R nginx:nginx /usr/local/openresty/nginx/conf/     && mkdir -p /usr/local/openresty/nginx/conf-bak     && rsync --update -raz /usr/local/openresty/nginx/conf/* /usr/local/openresty/nginx/conf-bak     && rm -rf /var/cache/apk/* /tmp/*' returned a non-zero code: 22

https://dev.maxmind.com/geoip/geoip2/geolite2/
Looks like the "GeoLite Legacy databases" were discontinued. is the GeoIP2 database a drop-in replacement? will try it out on my end but I'm unfamiliar with this software so wanted to go ahead and open an issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.