gstats

package module
v0.1.6-0...-3d0e14c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2020 License: CC-BY-SA-4.0 Imports: 16 Imported by: 0

README

DISCONTINUED DUE TO PERFORMANCE PROBLEMS

gstats

GoDoc GitHub release (latest SemVer) Go Report Card

Realtime HTTP Stats for Go.

Example

func main() {
	gs := gstats.New("gstats")
	listener, err := net.Listen("tcp", "127.0.0.1:80")
	if err != nil {
		log.Fatal(err)
	}
	glistener := gs.Listener(listener)
	http.Handle("/gstats/", gs.Collect(http.StripPrefix("/gstats", gs.Show())))
	http.Handle("/", gs.Collect(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("You are visiting " + request.URL.Path))
	})))
	err = http.Serve(glistener, nil)
	gs.PrepareToExit()
	log.Fatal(err)
}

Then go to http://localhost/gstats

alt text

Features

  • Geolocation
  • Unique Visitors
  • Most Visited Pages
  • Average Response Sizes
  • Average Response Times
  • Referrers
  • Visitor OS Stats
  • Visitor Browser Stats
  • Bandwidth

License

Creative Commons Attribution-ShareAlike 4.0 International with default geolocation source. MIT otherwise. See https://github.com/cevatbarisyilmaz/gstats/blob/master/LICENSE.md for details.

Production Usage

gstats are still in early development stage. Be catious for production usages.

Attribution

This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com

Documentation

Overview

Package gstats collects HTTP server statistics and and shows them inside a web page.

Example
package main

import (
	"github.com/cevatbarisyilmaz/gstats"
	"log"
	"net"
	"net/http"
)

func main() {
	gs := gstats.New("gstats")
	listener, err := net.Listen("tcp", "127.0.0.1:80")
	if err != nil {
		log.Fatal(err)
	}
	glistener := gs.Listener(listener)
	http.Handle("/gstats/", gs.Collect(http.StripPrefix("/gstats", gs.Show())))
	http.Handle("/", gs.Collect(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("You are visiting " + request.URL.Path))
	})))
	err = http.Serve(glistener, nil)
	gs.PrepareToExit()
	log.Fatal(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GStats

type GStats struct {

	// Country function is used for geolocation.
	// If not overridden, it will use https://github.com/cevatbarisyilmaz/ip2country
	// which is licensed under Creative Commons Attribution Share Alike 4.0 International (CC-BY-SA-4.0)
	// as it uses GeoLite2 database created by MaxMind (which is also licensed with CC-BY-SA-4.0).
	// Therefore, with the default value, this package falls under CC-BY-SA-4.0 License.
	// However, if you decide to use another source for geolocation by overriding this attribute,
	// you can use this package under MIT License.
	Country func(net.IP) (string, error)
	// contains filtered or unexported fields
}

func New

func New(root string) *GStats

New returns a new GStats which will use given root folder to save needed data files.

func (*GStats) Collect

func (g *GStats) Collect(subhandler http.Handler) http.Handler

Collect wraps the given handler and returns another handler which will track HTTP-related statistics.

func (*GStats) Listener

func (g *GStats) Listener(sublistener net.Listener) net.Listener

Listener wraps the given listener and returns another listener which will track hosts, connections and bandwidth.

func (*GStats) PrepareToExit

func (g *GStats) PrepareToExit()

PrepareToExit saves any unsaved data to disk to not lose them on exiting the program. Normally, GStats does disk saves at the beginning of each hour.

func (*GStats) Show

func (g *GStats) Show() http.Handler

Show returns a handler which serves the web page to browse collected statistics. At public-facing usages, it is advised to authenticate the users first before calling this handler. If handler lies at a non-root path, any prefixes should be removed before invoking the handler. i.e. http.Handle("/statistics/", http.StripPrefix("/statistics", gs.Show()))

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL