This repository was archived by the owner on Sep 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.go
More file actions
54 lines (46 loc) · 1.47 KB
/
client.go
File metadata and controls
54 lines (46 loc) · 1.47 KB
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
package stats
import (
"errors"
"net/url"
"strconv"
"strings"
"github.com/hellofresh/stats-go/client"
"github.com/hellofresh/stats-go/incrementer"
"github.com/hellofresh/stats-go/state"
)
const (
// StatsD is a dsn scheme value for statsd client
statsD = "statsd"
// prometheus is a dsn scheme value for prometheus client
prometheus = "prometheus"
// Log is a dsn scheme value for log client
log = "log"
// Memory is a dsn scheme value for memory client
memory = "memory"
// Noop is a dsn scheme value for noop client
noop = "noop"
)
// ErrUnknownClient is an error returned when trying to create stats client of unknown type
var ErrUnknownClient = errors.New("unknown stats client type")
// NewClient creates and builds new stats client instance by given dsn
func NewClient(dsn string) (client.Client, error) {
dsnURL, err := url.Parse(dsn)
if err != nil {
return nil, err
}
// do not care about parse error, as default value is set to false that is fine for us
unicode, _ := strconv.ParseBool(dsnURL.Query().Get("unicode"))
switch dsnURL.Scheme {
case statsD:
return client.NewStatsD(dsnURL.Host, strings.Trim(dsnURL.Path, "/"), unicode)
case prometheus:
return client.NewPrometheus(dsnURL.Host, incrementer.NewPrometheusIncrementerFactory(), state.NewPrometheusStateFactory()), nil
case log:
return client.NewLog(unicode), nil
case memory:
return client.NewMemory(unicode), nil
case noop:
return client.NewNoop(), nil
}
return nil, ErrUnknownClient
}