-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds usage reporting that periodically reports anonymous usage statistics to report.pikoproxy.com. Each report includes the servers uptime, requests processed, upstreams registered and host OS/architecture. This can be disabled with '--usage.disable'.
- Loading branch information
1 parent
5bc2b55
commit a3e06b6
Showing
6 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package usage | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/andydunstall/piko/pkg/log" | ||
"github.com/andydunstall/piko/server/proxy" | ||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
reportInterval = time.Hour | ||
) | ||
|
||
type Report struct { | ||
ID string `json:"id"` | ||
OS string `json:"os"` | ||
Arch string `json:"arch"` | ||
Uptime int64 `json:"uptime"` | ||
Requests uint64 `json:"requests"` | ||
Upstreams uint64 `json:"upstreams"` | ||
} | ||
|
||
// Reporter sends a periodic usage report. | ||
type Reporter struct { | ||
id string | ||
start time.Time | ||
proxy *proxy.Proxy | ||
logger log.Logger | ||
} | ||
|
||
func NewReporter(proxy *proxy.Proxy, logger log.Logger) *Reporter { | ||
return &Reporter{ | ||
id: uuid.New().String(), | ||
start: time.Now(), | ||
proxy: proxy, | ||
logger: logger.WithSubsystem("reporter"), | ||
} | ||
} | ||
|
||
func (r *Reporter) Run(ctx context.Context) { | ||
// Report on startup. | ||
r.report() | ||
|
||
ticker := time.NewTicker(reportInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
// Report on shutdown. | ||
r.report() | ||
return | ||
case <-ticker.C: | ||
// Report on interval. | ||
r.report() | ||
} | ||
} | ||
} | ||
|
||
func (r *Reporter) report() { | ||
report := &Report{ | ||
ID: r.id, | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
Uptime: int64(time.Since(r.start).Seconds()), | ||
Requests: r.proxy.Usage().Requests.Load(), | ||
Upstreams: r.proxy.Usage().Upstreams.Load(), | ||
} | ||
if err := r.send(report); err != nil { | ||
// Debug only as theres no user impact. | ||
r.logger.Debug("failed to send usage report", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (r *Reporter) send(report *Report) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
body, err := json.Marshal(report) | ||
if err != nil { | ||
return fmt.Errorf("marshal: %w", err) | ||
} | ||
req, err := http.NewRequestWithContext( | ||
ctx, http.MethodPost, "http://report.pikoproxy.com/v1", bytes.NewBuffer(body), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("request: %w", err) | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
return nil | ||
} |