diff --git a/features/response_times.feature b/features/response_times.feature index 7f9fff5..55ab5c1 100644 --- a/features/response_times.feature +++ b/features/response_times.feature @@ -19,6 +19,17 @@ Feature: Upstream response times are summarized """ Then the exporter should report value 20 for metric nginx_http_upstream_time_seconds{method="GET",status="200",quantile="0.5"} + Scenario: .5 quantile of upstream connect time is computed + Given a running exporter listening on "access.log" with upstream-connect-time format + When the following HTTP request is logged to "access.log" + """ + 172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 10 5 + 172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 20 5 + 172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 30 10 + 172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 40 10 + """ + Then the exporter should report value 5 for metric nginx_http_upstream_connect_time_seconds{method="GET",status="200",quantile="0.5"} + Scenario: .5 quantile of response time is computed Given a running exporter listening on "access.log" with request-time format When the following HTTP request is logged to "access.log" diff --git a/features/steps/steps.py b/features/steps/steps.py index 36fa3ec..34a258b 100644 --- a/features/steps/steps.py +++ b/features/steps/steps.py @@ -8,6 +8,7 @@ formats = { "default": '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"', "upstream-time": '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time', + "upstream-connect-time": '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time $upstream_connect_time', "request-time": '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $request_time' } diff --git a/main.go b/main.go index 1380b9b..3521f91 100644 --- a/main.go +++ b/main.go @@ -27,148 +27,19 @@ import ( "sync" "syscall" - "github.com/martin-helmich/prometheus-nginxlog-exporter/syslog" - - "github.com/martin-helmich/prometheus-nginxlog-exporter/config" - "github.com/martin-helmich/prometheus-nginxlog-exporter/discovery" - "github.com/martin-helmich/prometheus-nginxlog-exporter/parser" - "github.com/martin-helmich/prometheus-nginxlog-exporter/prof" - "github.com/martin-helmich/prometheus-nginxlog-exporter/relabeling" - "github.com/martin-helmich/prometheus-nginxlog-exporter/tail" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/discovery" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/metrics" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/parser" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/prof" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/relabeling" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/syslog" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/tail" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) -type NSMetrics struct { - cfg *config.NamespaceConfig - registry *prometheus.Registry - Metrics -} - -func NewNSMetrics(cfg *config.NamespaceConfig) *NSMetrics { - m := &NSMetrics{ - cfg: cfg, - registry: prometheus.NewRegistry(), - } - m.Init(cfg) - - m.registry.MustRegister(m.countTotal) - m.registry.MustRegister(m.requestBytesTotal) - m.registry.MustRegister(m.responseBytesTotal) - m.registry.MustRegister(m.upstreamSeconds) - m.registry.MustRegister(m.upstreamSecondsHist) - m.registry.MustRegister(m.responseSeconds) - m.registry.MustRegister(m.responseSecondsHist) - m.registry.MustRegister(m.parseErrorsTotal) - return m -} - -// Metrics is a struct containing pointers to all metrics that should be -// exposed to Prometheus -type Metrics struct { - countTotal *prometheus.CounterVec - responseBytesTotal *prometheus.CounterVec - requestBytesTotal *prometheus.CounterVec - upstreamSeconds *prometheus.SummaryVec - upstreamSecondsHist *prometheus.HistogramVec - responseSeconds *prometheus.SummaryVec - responseSecondsHist *prometheus.HistogramVec - parseErrorsTotal prometheus.Counter -} - -func inLabels(label string, labels []string) bool { - for _, l := range labels { - if label == l { - return true - } - } - return false -} - -// Init initializes a metrics struct -func (m *Metrics) Init(cfg *config.NamespaceConfig) { - cfg.MustCompile() - - labels := cfg.OrderedLabelNames - counterLabels := labels - - for i := range cfg.RelabelConfigs { - if !cfg.RelabelConfigs[i].OnlyCounter { - labels = append(labels, cfg.RelabelConfigs[i].TargetLabel) - } - counterLabels = append(counterLabels, cfg.RelabelConfigs[i].TargetLabel) - } - - for _, r := range relabeling.DefaultRelabelings { - if !inLabels(r.TargetLabel, labels) { - labels = append(labels, r.TargetLabel) - } - if !inLabels(r.TargetLabel, counterLabels) { - counterLabels = append(counterLabels, r.TargetLabel) - } - } - - m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_response_count_total", - Help: "Amount of processed HTTP requests", - }, counterLabels) - - m.responseBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_response_size_bytes", - Help: "Total amount of transferred bytes", - }, labels) - - m.requestBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_request_size_bytes", - Help: "Total amount of received bytes", - }, labels) - - m.upstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_upstream_time_seconds", - Help: "Time needed by upstream servers to handle requests", - Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, - }, labels) - - m.upstreamSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_upstream_time_seconds_hist", - Help: "Time needed by upstream servers to handle requests", - Buckets: cfg.HistogramBuckets, - }, labels) - - m.responseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_response_time_seconds", - Help: "Time needed by NGINX to handle requests", - Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, - }, labels) - - m.responseSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "http_response_time_seconds_hist", - Help: "Time needed by NGINX to handle requests", - Buckets: cfg.HistogramBuckets, - }, labels) - - m.parseErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: cfg.NamespacePrefix, - ConstLabels: cfg.NamespaceLabels, - Name: "parse_errors_total", - Help: "Total number of log file lines that could not be parsed", - }) -} - func main() { var opts config.StartupFlags var cfg = config.Config{ @@ -237,11 +108,11 @@ func main() { } for _, ns := range cfg.Namespaces { - nsMetrics := NewNSMetrics(&ns) - nsGatherers = append(nsGatherers, nsMetrics.registry) + nsMetrics := metrics.NewForNamespace(&ns) + nsGatherers = append(nsGatherers, nsMetrics.Gatherer()) fmt.Printf("starting listener for namespace %s\n", ns.Name) - go processNamespace(ns, &(nsMetrics.Metrics)) + go processNamespace(ns, &(nsMetrics.Collection)) } listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port) @@ -300,7 +171,7 @@ func setupConsul(cfg *config.Config, stopChan <-chan bool, stopHandlers *sync.Wa stopHandlers.Add(1) } -func processNamespace(nsCfg config.NamespaceConfig, metrics *Metrics) { +func processNamespace(nsCfg config.NamespaceConfig, metrics *metrics.Collection) { var followers []tail.Follower parser := parser.NewParser(nsCfg) @@ -356,7 +227,7 @@ func processNamespace(nsCfg config.NamespaceConfig, metrics *Metrics) { } -func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser parser.Parser, metrics *Metrics, hasCounterOnlyLabels bool) { +func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser parser.Parser, metrics *metrics.Collection, hasCounterOnlyLabels bool) { relabelings := relabeling.NewRelabelings(nsCfg.RelabelConfigs) relabelings = append(relabelings, relabeling.DefaultRelabelings...) relabelings = relabeling.UniqueRelabelings(relabelings) @@ -377,7 +248,7 @@ func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser parser. fields, err := parser.ParseString(line) if err != nil { fmt.Printf("error while parsing line '%s': %s\n", line, err) - metrics.parseErrorsTotal.Inc() + metrics.ParseErrorsTotal.Inc() continue } @@ -397,24 +268,29 @@ func processSource(nsCfg config.NamespaceConfig, t tail.Follower, parser parser. notCounterValues = labelValues } - metrics.countTotal.WithLabelValues(labelValues...).Inc() + metrics.CountTotal.WithLabelValues(labelValues...).Inc() + + if v, ok := observeMetrics(fields, "body_bytes_sent", floatFromFields, metrics.ParseErrorsTotal); ok { + metrics.ResponseBytesTotal.WithLabelValues(notCounterValues...).Add(v) + } - if v, ok := observeMetrics(fields, "body_bytes_sent", floatFromFields, metrics.parseErrorsTotal); ok { - metrics.responseBytesTotal.WithLabelValues(notCounterValues...).Add(v) + if v, ok := observeMetrics(fields, "request_length", floatFromFields, metrics.ParseErrorsTotal); ok { + metrics.RequestBytesTotal.WithLabelValues(notCounterValues...).Add(v) } - if v, ok := observeMetrics(fields, "request_length", floatFromFields, metrics.parseErrorsTotal); ok { - metrics.requestBytesTotal.WithLabelValues(notCounterValues...).Add(v) + if v, ok := observeMetrics(fields, "upstream_response_time", floatFromFieldsMulti, metrics.ParseErrorsTotal); ok { + metrics.UpstreamSeconds.WithLabelValues(notCounterValues...).Observe(v) + metrics.UpstreamSecondsHist.WithLabelValues(notCounterValues...).Observe(v) } - if v, ok := observeMetrics(fields, "upstream_response_time", floatFromFieldsMulti, metrics.parseErrorsTotal); ok { - metrics.upstreamSeconds.WithLabelValues(notCounterValues...).Observe(v) - metrics.upstreamSecondsHist.WithLabelValues(notCounterValues...).Observe(v) + if v, ok := observeMetrics(fields, "upstream_connect_time", floatFromFieldsMulti, metrics.ParseErrorsTotal); ok { + metrics.UpstreamConnectSeconds.WithLabelValues(notCounterValues...).Observe(v) + metrics.UpstreamConnectSecondsHist.WithLabelValues(notCounterValues...).Observe(v) } - if v, ok := observeMetrics(fields, "request_time", floatFromFields, metrics.parseErrorsTotal); ok { - metrics.responseSeconds.WithLabelValues(notCounterValues...).Observe(v) - metrics.responseSecondsHist.WithLabelValues(notCounterValues...).Observe(v) + if v, ok := observeMetrics(fields, "request_time", floatFromFields, metrics.ParseErrorsTotal); ok { + metrics.ResponseSeconds.WithLabelValues(notCounterValues...).Observe(v) + metrics.ResponseSecondsHist.WithLabelValues(notCounterValues...).Observe(v) } } } diff --git a/config/loader.go b/pkg/config/loader.go similarity index 100% rename from config/loader.go rename to pkg/config/loader.go diff --git a/config/loader_flags.go b/pkg/config/loader_flags.go similarity index 100% rename from config/loader_flags.go rename to pkg/config/loader_flags.go diff --git a/config/loader_flags_test.go b/pkg/config/loader_flags_test.go similarity index 100% rename from config/loader_flags_test.go rename to pkg/config/loader_flags_test.go diff --git a/config/loader_hcl.go b/pkg/config/loader_hcl.go similarity index 99% rename from config/loader_hcl.go rename to pkg/config/loader_hcl.go index d828771..b6a6ac5 100644 --- a/config/loader_hcl.go +++ b/pkg/config/loader_hcl.go @@ -1,9 +1,10 @@ package config import ( - "github.com/hashicorp/hcl" "io" "io/ioutil" + + "github.com/hashicorp/hcl" ) func loadConfigFromHCLStream(config *Config, file io.Reader) error { diff --git a/config/loader_test.go b/pkg/config/loader_test.go similarity index 100% rename from config/loader_test.go rename to pkg/config/loader_test.go diff --git a/config/loader_yaml.go b/pkg/config/loader_yaml.go similarity index 99% rename from config/loader_yaml.go rename to pkg/config/loader_yaml.go index 92177a6..b3c4309 100644 --- a/config/loader_yaml.go +++ b/pkg/config/loader_yaml.go @@ -1,9 +1,10 @@ package config import ( - "gopkg.in/yaml.v2" "io" "io/ioutil" + + "gopkg.in/yaml.v2" ) func loadConfigFromYAMLStream(config *Config, file io.Reader) error { diff --git a/config/struct_namespace.go b/pkg/config/struct_namespace.go similarity index 100% rename from config/struct_namespace.go rename to pkg/config/struct_namespace.go diff --git a/config/struct_namespace_test.go b/pkg/config/struct_namespace_test.go similarity index 100% rename from config/struct_namespace_test.go rename to pkg/config/struct_namespace_test.go diff --git a/config/struct_relabel.go b/pkg/config/struct_relabel.go similarity index 100% rename from config/struct_relabel.go rename to pkg/config/struct_relabel.go diff --git a/config/structs.go b/pkg/config/structs.go similarity index 100% rename from config/structs.go rename to pkg/config/structs.go diff --git a/config/test/file_3.txt b/pkg/config/test/file_3.txt similarity index 100% rename from config/test/file_3.txt rename to pkg/config/test/file_3.txt diff --git a/config/test/file_pattern_1.txt b/pkg/config/test/file_pattern_1.txt similarity index 100% rename from config/test/file_pattern_1.txt rename to pkg/config/test/file_pattern_1.txt diff --git a/config/test/file_pattern_2.txt b/pkg/config/test/file_pattern_2.txt similarity index 100% rename from config/test/file_pattern_2.txt rename to pkg/config/test/file_pattern_2.txt diff --git a/config/test/unrelated_file.txt b/pkg/config/test/unrelated_file.txt similarity index 100% rename from config/test/unrelated_file.txt rename to pkg/config/test/unrelated_file.txt diff --git a/discovery/consul.go b/pkg/discovery/consul.go similarity index 96% rename from discovery/consul.go rename to pkg/discovery/consul.go index 003c611..dca3f95 100644 --- a/discovery/consul.go +++ b/pkg/discovery/consul.go @@ -2,7 +2,7 @@ package discovery import ( "github.com/hashicorp/consul/api" - "github.com/martin-helmich/prometheus-nginxlog-exporter/config" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" ) // ConsulRegistrator is a helper struct that handles Consul service registration diff --git a/pkg/metrics/collection.go b/pkg/metrics/collection.go new file mode 100644 index 0000000..a3bad38 --- /dev/null +++ b/pkg/metrics/collection.go @@ -0,0 +1,18 @@ +package metrics + +import "github.com/prometheus/client_golang/prometheus" + +// Collection is a struct containing pointers to all metrics that should be +// exposed to Prometheus +type Collection struct { + CountTotal *prometheus.CounterVec + ResponseBytesTotal *prometheus.CounterVec + RequestBytesTotal *prometheus.CounterVec + UpstreamSeconds *prometheus.SummaryVec + UpstreamSecondsHist *prometheus.HistogramVec + UpstreamConnectSeconds *prometheus.SummaryVec + UpstreamConnectSecondsHist *prometheus.HistogramVec + ResponseSeconds *prometheus.SummaryVec + ResponseSecondsHist *prometheus.HistogramVec + ParseErrorsTotal prometheus.Counter +} diff --git a/pkg/metrics/collection_init.go b/pkg/metrics/collection_init.go new file mode 100644 index 0000000..fa9abe1 --- /dev/null +++ b/pkg/metrics/collection_init.go @@ -0,0 +1,116 @@ +package metrics + +import ( + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/relabeling" + "github.com/prometheus/client_golang/prometheus" +) + +func inLabels(label string, labels []string) bool { + for _, l := range labels { + if label == l { + return true + } + } + return false +} + +// Init initializes a metrics struct +func (m *Collection) Init(cfg *config.NamespaceConfig) { + cfg.MustCompile() + + labels := cfg.OrderedLabelNames + counterLabels := labels + + for i := range cfg.RelabelConfigs { + if !cfg.RelabelConfigs[i].OnlyCounter { + labels = append(labels, cfg.RelabelConfigs[i].TargetLabel) + } + counterLabels = append(counterLabels, cfg.RelabelConfigs[i].TargetLabel) + } + + for _, r := range relabeling.DefaultRelabelings { + if !inLabels(r.TargetLabel, labels) { + labels = append(labels, r.TargetLabel) + } + if !inLabels(r.TargetLabel, counterLabels) { + counterLabels = append(counterLabels, r.TargetLabel) + } + } + + m.CountTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_response_count_total", + Help: "Amount of processed HTTP requests", + }, counterLabels) + + m.ResponseBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_response_size_bytes", + Help: "Total amount of transferred bytes", + }, labels) + + m.RequestBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_request_size_bytes", + Help: "Total amount of received bytes", + }, labels) + + m.UpstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_upstream_time_seconds", + Help: "Time needed by upstream servers to handle requests", + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + }, labels) + + m.UpstreamSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_upstream_time_seconds_hist", + Help: "Time needed by upstream servers to handle requests", + Buckets: cfg.HistogramBuckets, + }, labels) + + m.UpstreamConnectSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_upstream_connect_time_seconds", + Help: "Time needed to connect to upstream servers", + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + }, labels) + + m.UpstreamConnectSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_upstream_connect_time_seconds_hist", + Help: "Time needed to connect to upstream servers", + Buckets: cfg.HistogramBuckets, + }, labels) + + m.ResponseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_response_time_seconds", + Help: "Time needed by NGINX to handle requests", + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + }, labels) + + m.ResponseSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "http_response_time_seconds_hist", + Help: "Time needed by NGINX to handle requests", + Buckets: cfg.HistogramBuckets, + }, labels) + + m.ParseErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: cfg.NamespacePrefix, + ConstLabels: cfg.NamespaceLabels, + Name: "parse_errors_total", + Help: "Total number of log file lines that could not be parsed", + }) +} diff --git a/pkg/metrics/collection_register.go b/pkg/metrics/collection_register.go new file mode 100644 index 0000000..233897a --- /dev/null +++ b/pkg/metrics/collection_register.go @@ -0,0 +1,16 @@ +package metrics + +import "github.com/prometheus/client_golang/prometheus" + +func (c *Collection) MustRegister(r *prometheus.Registry) { + r.MustRegister(c.CountTotal) + r.MustRegister(c.RequestBytesTotal) + r.MustRegister(c.ResponseBytesTotal) + r.MustRegister(c.UpstreamSeconds) + r.MustRegister(c.UpstreamSecondsHist) + r.MustRegister(c.UpstreamConnectSeconds) + r.MustRegister(c.UpstreamConnectSecondsHist) + r.MustRegister(c.ResponseSeconds) + r.MustRegister(c.ResponseSecondsHist) + r.MustRegister(c.ParseErrorsTotal) +} diff --git a/pkg/metrics/namespace.go b/pkg/metrics/namespace.go new file mode 100644 index 0000000..f77a109 --- /dev/null +++ b/pkg/metrics/namespace.go @@ -0,0 +1,28 @@ +package metrics + +import ( + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" + "github.com/prometheus/client_golang/prometheus" +) + +type NamespaceMetrics struct { + cfg *config.NamespaceConfig + registry *prometheus.Registry + + Collection +} + +func NewForNamespace(cfg *config.NamespaceConfig) *NamespaceMetrics { + m := &NamespaceMetrics{ + cfg: cfg, + registry: prometheus.NewRegistry(), + } + m.Init(cfg) + m.MustRegister(m.registry) + + return m +} + +func (m *NamespaceMetrics) Gatherer() prometheus.Gatherer { + return m.registry +} diff --git a/parser/jsonparser/jsonparser.go b/pkg/parser/jsonparser/jsonparser.go similarity index 100% rename from parser/jsonparser/jsonparser.go rename to pkg/parser/jsonparser/jsonparser.go diff --git a/parser/jsonparser/jsonparser_test.go b/pkg/parser/jsonparser/jsonparser_test.go similarity index 100% rename from parser/jsonparser/jsonparser_test.go rename to pkg/parser/jsonparser/jsonparser_test.go diff --git a/parser/parser.go b/pkg/parser/parser.go similarity index 67% rename from parser/parser.go rename to pkg/parser/parser.go index 7242ca7..3a5f3ea 100644 --- a/parser/parser.go +++ b/pkg/parser/parser.go @@ -1,9 +1,9 @@ package parser import ( - "github.com/martin-helmich/prometheus-nginxlog-exporter/config" - "github.com/martin-helmich/prometheus-nginxlog-exporter/parser/jsonparser" - "github.com/martin-helmich/prometheus-nginxlog-exporter/parser/textparser" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/parser/jsonparser" + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/parser/textparser" ) // Parser parses a line of log to a map[string]string. diff --git a/parser/textparser/textparser.go b/pkg/parser/textparser/textparser.go similarity index 100% rename from parser/textparser/textparser.go rename to pkg/parser/textparser/textparser.go diff --git a/parser/textparser/textparser_test.go b/pkg/parser/textparser/textparser_test.go similarity index 100% rename from parser/textparser/textparser_test.go rename to pkg/parser/textparser/textparser_test.go diff --git a/prof/cpu.go b/pkg/prof/cpu.go similarity index 100% rename from prof/cpu.go rename to pkg/prof/cpu.go diff --git a/prof/mem.go b/pkg/prof/mem.go similarity index 100% rename from prof/mem.go rename to pkg/prof/mem.go diff --git a/relabeling/defaults.go b/pkg/relabeling/defaults.go similarity index 89% rename from relabeling/defaults.go rename to pkg/relabeling/defaults.go index b6375e5..ad77426 100644 --- a/relabeling/defaults.go +++ b/pkg/relabeling/defaults.go @@ -1,6 +1,6 @@ package relabeling -import "github.com/martin-helmich/prometheus-nginxlog-exporter/config" +import "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" // DefaultRelabelings are hardcoded relabeling configs that are always there // and do not need to be explicitly configured diff --git a/relabeling/mapping.go b/pkg/relabeling/mapping.go similarity index 100% rename from relabeling/mapping.go rename to pkg/relabeling/mapping.go diff --git a/relabeling/mapping_test.go b/pkg/relabeling/mapping_test.go similarity index 94% rename from relabeling/mapping_test.go rename to pkg/relabeling/mapping_test.go index 7956463..63f5560 100644 --- a/relabeling/mapping_test.go +++ b/pkg/relabeling/mapping_test.go @@ -1,8 +1,9 @@ package relabeling import ( - "github.com/martin-helmich/prometheus-nginxlog-exporter/config" "testing" + + "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" ) func buildRelabeling(cfg config.RelabelConfig) (*Relabeling, error) { diff --git a/relabeling/types.go b/pkg/relabeling/types.go similarity index 95% rename from relabeling/types.go rename to pkg/relabeling/types.go index 52d1e71..762425c 100644 --- a/relabeling/types.go +++ b/pkg/relabeling/types.go @@ -1,6 +1,6 @@ package relabeling -import "github.com/martin-helmich/prometheus-nginxlog-exporter/config" +import "github.com/martin-helmich/prometheus-nginxlog-exporter/pkg/config" // Relabeling contains a relabeling configuration and is responsible for // executing the rules specified in the original configuration diff --git a/syslog/syslog.go b/pkg/syslog/syslog.go similarity index 100% rename from syslog/syslog.go rename to pkg/syslog/syslog.go diff --git a/tail/struct.go b/pkg/tail/struct.go similarity index 100% rename from tail/struct.go rename to pkg/tail/struct.go diff --git a/tail/syslog.go b/pkg/tail/syslog.go similarity index 100% rename from tail/syslog.go rename to pkg/tail/syslog.go diff --git a/tail/tailer.go b/pkg/tail/tailer.go similarity index 100% rename from tail/tailer.go rename to pkg/tail/tailer.go