~jzs/aproxy

ref: f54d5fbdfabf1ecf63357b635fadc9c45fd6adde aproxy/aproxy/tracker.go -rw-r--r-- 3.2 KiB
f54d5fbdJens Zeilund Flush log more often 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package aproxy

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"
)

const logPath = "/var/log/aproxy"

// Event represents a tracking event
type Event struct {
	TS             time.Time
	URL            string
	RemoteAddr     string
	UserAgent      string
	AcceptLanguage string
	Referer        string
	Method         string
	Host           string
}

// Tracker represents a tracker that logs info about http requests and logs them
type Tracker struct {
	c      chan Event
	wg     sync.WaitGroup
	buffer bytes.Buffer
	mux    sync.Mutex
}

func NewTracker() *Tracker {
	t := &Tracker{}
	t.c = make(chan Event, 100000)
	//t.done = make(chan bool, 1)
	t.wg = sync.WaitGroup{}
	os.MkdirAll(logPath, 0700)
	// Create func that processes events.
	//  start tracking...
	t.wg.Add(1)
	go func() {
		tick := time.Tick(60 * time.Second)
		process := true
		for process {
			select {
			case evt, ok := <-t.c:
				if !ok { // Channel closed. Break out.
					process = false
					break
				}
				// Handle event.
				data, err := json.Marshal(evt)
				// If not if else/ then implement a function that does the stuff..
				if err != nil {
					log.Printf("Failed marshalling tracking json: %v", err)
				} else {
					t.buffer.Write(data)
					t.buffer.Write([]byte("\n"))

					// Roll buffer if too big... (2MB at most)
					if t.buffer.Len() > 1024*1024*2 {
						t.flush()
					}
				}
			case <-tick:
				// Flush buffer.
				t.flush()
			}
		}
		t.wg.Done()
	}()
	return t
}

// flush flushes the buffer to disk.
func (t *Tracker) flush() {
	t.mux.Lock()
	defer t.mux.Unlock()

	data := t.buffer.Bytes()
	if len(data) == 0 {
		return
	}

	dir, err := os.Open(logPath)
	if err != nil {
		return
	}
	infos, err := dir.Readdir(0)
	if err != nil {
		return
	}

	// Compute today string.
	today := time.Now().Format("2006-01-02")

	tfiles := []os.FileInfo{}

	// parse names. Find correct file.
	for _, i := range infos {
		// Handle infos...
		if strings.HasPrefix(i.Name(), today) {
			tfiles = append(tfiles, i)
		}
	}

	var file *os.File
	if len(tfiles) == 0 {
		// Create a new file.
		file, err = os.Create(path.Join(logPath, today+"-1.log"))
		if err != nil {
			panic(err)
			return
		}
	} else {
		sort.Slice(tfiles, func(i, j int) bool {
			return tfiles[i].Name() > tfiles[j].Name()
		})
		if tfiles[0].Size() > 1024*1024*50 {
			file, err = os.Create(path.Join(logPath, fmt.Sprintf("%v-%v.log", today, len(tfiles)+1)))
			if err != nil {
				panic(err)
				return
			}
		} else {
			file, err = os.OpenFile(path.Join(logPath, tfiles[0].Name()), os.O_APPEND|os.O_WRONLY, 0666)
			if err != nil {
				panic(err)
				return
			}
		}
	}
	defer file.Close()

	file.Write(data)

	t.buffer.Reset()
}

func (p *Tracker) Shutdown() {
	close(p.c)
	p.wg.Wait()

	p.flush()
}

// Perform logging...
func (p *Tracker) Track(ctx context.Context, r *http.Request) {
	// Consider respecting DNT header?
	h := r.Header
	evt := Event{
		TS:             time.Now(),
		URL:            r.URL.String(),
		RemoteAddr:     r.RemoteAddr,
		UserAgent:      strings.Join(h["User-Agent"], "|"),
		AcceptLanguage: strings.Join(h["Accept-Language"], "|"),
		Referer:        strings.Join(h["Referer"], "|"),
		Method:         r.Method,
		Host:           r.Host,
	}
	p.c <- evt
}