Compare commits

...

7 Commits

Author SHA1 Message Date
Stanislav Nikitin 3ebb5b657c
Add ability to build tagged images.
continuous-integration/drone/push Build is passing Details
2022-06-30 14:14:40 +05:00
Stanislav Nikitin dc1287dae8
Make linters happy and disable deprecated exhaustivestruct.
continuous-integration/drone/push Build is passing Details
2022-06-29 13:00:38 +05:00
Stanislav Nikitin 9d5d5c262e
Update Drone configuration and Dockerfile to use mirrorred images.
continuous-integration/drone/push Build is failing Details
Expect pipeline to fail.
2022-06-29 00:41:56 +05:00
Stanislav Nikitin 26ce90bb84
Linting fixes. 2021-11-21 14:45:09 +05:00
Stanislav Nikitin 16be332d38
Add Drone configuration. 2021-11-21 14:12:09 +05:00
Stanislav Nikitin e2a928fac4 Merge branch 'shanvl/bug_fix' into 'master'
Fix logger config and parser bugs; use scanner instead of reading request body at once

See merge request pztrn/metricator!1
2021-06-04 08:50:12 +00:00
Andrey Shcherbinin a252681b26 Fix logger config and parser bugs; use scanner instead of reading request body at once 2021-06-04 08:50:12 +00:00
17 changed files with 149 additions and 78 deletions

43
.drone.yml Normal file
View File

@ -0,0 +1,43 @@
---
kind: pipeline
type: docker
name: build
steps:
- name: lint
image: code.pztrn.name/containers/mirror/golangci/golangci-lint:v1.46.2
environment:
CGO_ENABLED: 0
commands:
- golangci-lint run
- name: test
image: code.pztrn.name/containers/mirror/golang:1.18.3-alpine
environment:
CGO_ENABLED: 0
commands:
- go test ./...
- name: build master image
image: code.pztrn.name/containers/mirror/plugins/docker:20.13.0
when:
branch: ["master"]
settings:
registry: code.pztrn.name
username: drone
password:
from_secret: drone_secret
repo: code.pztrn.name/apps/metricator
auto_tag: true
- name: build tagged image
image: code.pztrn.name/containers/mirror/plugins/docker:20.13.0
when:
event: ["tag"]
settings:
registry: code.pztrn.name
username: drone
password:
from_secret: drone_secret
repo: code.pztrn.name/apps/metricator
auto_tag: true

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
._bin
.vscode
metricator.yaml
.idea
*DS_Store*

View File

@ -9,11 +9,15 @@ linters:
- gomnd
# Why? WHY? WHY _test???
- testpackage
# Structs will contain some context.Context.
- containedctx
# Deprecated
- exhaustivestruct
linters-settings:
lll:
line-length: 120
gocyclo:
min-complexity: 40
cyclop:
max-complexity: 40
gocognit:
min-complexity: 40
funlen:

View File

@ -1,4 +1,4 @@
FROM registry.gitlab.pztrn.name/containers/mirror/golang:1.15.5-alpine AS build
FROM code.pztrn.name/containers/mirror/golang:1.18.3-alpine AS build
WORKDIR /go/src/gitlab.pztrn.name/pztrn/metricator
COPY . .
@ -13,7 +13,7 @@ RUN apk add bash git make
RUN make metricatord-build
RUN make metricator-client-build
FROM registry.gitlab.pztrn.name/containers/mirror/golang:1.15.5-alpine
FROM code.pztrn.name/containers/mirror/golang:1.18.3-alpine
LABEL maintainer="Stanislav N. <pztrn@pztrn.name>"
COPY --from=build /go/src/gitlab.pztrn.name/pztrn/metricator/._bin/metricatord /usr/local/bin/metricatord

View File

@ -25,6 +25,9 @@ var (
output = flag.String("output", "json", "Output format. Can be 'json' or 'plain-by-line'.")
)
// This function uses fmt.Println to print lines without timestamps to make it easy
// to parse output, so:
// nolint:forbidigo
func main() {
config := configuration.NewConfig()
@ -83,17 +86,17 @@ func main() {
Timeout: *metricatorTimeout,
}
c := client.NewClient(clientConfig, logger)
clnt := client.NewClient(clientConfig, logger)
var data interface{}
switch {
case *appsList:
data = c.GetAppsList()
data = clnt.GetAppsList()
case *metricsList:
data = c.GetMetricsList(*application)
data = clnt.GetMetricsList(*application)
case *metric != "":
data = c.GetMetric(*application, *metric)
data = clnt.GetMetric(*application, *metric)
}
switch *output {

View File

@ -29,8 +29,7 @@ func main() {
// Parse configuration.
flag.Parse()
err := config.Parse()
if err != nil {
if err := config.Parse(); err != nil {
log.Fatalln("Failed to parse configuration:", err.Error())
}

View File

@ -28,16 +28,18 @@ type Application struct {
// NewApplication creates new application.
func NewApplication(ctx context.Context, name string, config *Config, logger *logger.Logger) *Application {
a := &Application{
// Some variables are initialized in initialize() function.
// nolint:exhaustruct
app := &Application{
config: config,
ctx: ctx,
doneChan: make(chan struct{}),
logger: logger,
name: name,
}
a.initialize()
app.initialize()
return a
return app
}
// GetDoneChan returns a channel which should be used to block execution until

View File

@ -11,5 +11,6 @@ type Config struct {
Endpoint string `yaml:"endpoint"`
// TimeBetweenRequests is a minimal amount of time which should pass
// between requests.
// nolint:tagliatelle
TimeBetweenRequests time.Duration `yaml:"time_between_requests"`
}

View File

@ -1,7 +1,6 @@
package application
import (
"io/ioutil"
"net/http"
"time"
)
@ -11,6 +10,9 @@ func (a *Application) fetch() {
// Do not do anything if fetching is running.
// ToDo: maybe another approach?
a.fetchIsRunningMutex.RLock()
// This is an optimization to avoid excessive waiting when using Lock().
// Most of time application will wait between fetches.
// nolint:ifshort
isFetching := a.fetchIsRunning
a.fetchIsRunningMutex.RUnlock()
@ -44,15 +46,13 @@ func (a *Application) fetch() {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
data, err := a.parse(resp.Body)
if err != nil {
a.logger.Infoln("Failed to read response body for", a.name, "metrics:", err.Error())
a.logger.Infoln("Failed to parse response body for", a.name, "metrics:", err.Error())
return
}
data := a.parse(string(body))
a.storage.Put(data)
a.fetchIsRunningMutex.Lock()
@ -64,7 +64,7 @@ func (a *Application) fetch() {
func (a *Application) startFetcher() {
fetchTicker := time.NewTicker(a.config.TimeBetweenRequests)
// nolint:exhaustivestruct
// nolint:exhaustruct
a.httpClient = &http.Client{
Timeout: time.Second * 5,
}

View File

@ -1,19 +1,27 @@
package application
import (
"bufio"
"fmt"
"io"
"strings"
"go.dev.pztrn.name/metricator/pkg/schema"
)
// Parses passed body and returns a map suitable for pushing into storage.
func (a *Application) parse(body string) map[string]schema.Metric {
// Parses io.Reader passed and returns a map suitable for pushing into storage.
func (a *Application) parse(r io.Reader) (map[string]schema.Metric, error) {
data := make(map[string]schema.Metric)
// ToDo: switch to bytes buffer and maybe do not read body in caller?
splittedBody := strings.Split(body, "\n")
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
// Skip empty lines.
if line == "" {
continue
}
for _, line := range splittedBody {
// Prometheus line contains metric name and metric parameters defined
// in "{}".
var (
@ -21,11 +29,6 @@ func (a *Application) parse(body string) map[string]schema.Metric {
params []string
)
// Skip empty lines.
if line == "" {
continue
}
a.logger.Debugln("Analyzing line:", line)
name = a.getMetricName(line)
@ -80,19 +83,22 @@ func (a *Application) parse(body string) map[string]schema.Metric {
newMetric.Params = params
metric = newMetric
data[metric.Name] = metric
}
metric.Value = a.getMetricValue(line)
a.logger.Debugf("Got metric: %+v\n", metric)
data[name] = metric
data[metric.Name] = metric
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("wasn't able to scan input: %w", err)
}
a.logger.Debugf("Data parsed: %+v\n", data)
return data
return data, nil
}
// Gets metric description from passed line.
@ -137,8 +143,8 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
paramNameFinished, paramValueStarted, paramValueFinished bool
)
for _, r := range valuesString {
if paramValueFinished && string(r) == "," {
for _, runeChar := range valuesString {
if paramValueFinished && string(runeChar) == "," {
params = append(params, paramName+":"+paramValue)
paramName, paramValue = "", ""
paramNameFinished, paramValueStarted, paramValueFinished = false, false, false
@ -150,8 +156,8 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
// "deeply nested"? I think not. So:
// nolint:nestif
if !paramNameFinished {
if string(r) != "=" {
paramName += string(r)
if string(runeChar) != "=" {
paramName += string(runeChar)
continue
} else {
@ -160,19 +166,19 @@ func (a *Application) getParametersForPrometheusMetric(line string) []string {
continue
}
} else {
if string(r) == "\"" && !paramValueStarted {
if string(runeChar) == "\"" && !paramValueStarted {
paramValueStarted = true
continue
}
if paramValueStarted && string(r) != "\"" {
paramValue += string(r)
if paramValueStarted && string(runeChar) != "\"" {
paramValue += string(runeChar)
continue
}
if paramValueStarted && string(r) == "\"" {
if paramValueStarted && string(runeChar) == "\"" {
paramValueFinished = true
continue

View File

@ -21,16 +21,18 @@ var (
// Config is an application's configuration.
type Config struct {
configPath string
// Applications describes configuration for remote application's endpoints.
// Key is an application's name.
Applications map[string]*application.Config `yaml:"applications"`
// Logger is a logging configuration.
Logger *logger.Config `yaml:"logger"`
Logger *logger.Config `yaml:"logger"`
configPath string
}
// NewConfig returns new configuration.
func NewConfig() *Config {
// Fields are initialized when parsing YAML file.
// nolint:exhaustruct
c := &Config{}
c.initialize()

View File

@ -48,14 +48,14 @@ func (h *handler) getAppsList() ([]byte, error) {
// Gets request information from URL. Returns a structure with filled request
// info and error if it occurs.
func (h *handler) getRequestInfo(r *http.Request) (*models.RequestInfo, error) {
func (h *handler) getRequestInfo(req *http.Request) (*models.RequestInfo, error) {
// Request isn't for API or isn't versioned.
if !strings.HasPrefix(r.URL.Path, "/api/v") {
if !strings.HasPrefix(req.URL.Path, "/api/v") {
return nil, errInvalidPath
}
// Note: first element will always be empty!
pathSplitted := strings.Split(r.URL.Path, "/")
pathSplitted := strings.Split(req.URL.Path, "/")
// Request is for API but not enough items in URL was passed.
if len(pathSplitted) < 4 {
@ -119,20 +119,20 @@ func (h *handler) register(appName string, hndl common.HTTPHandlerFunc) {
}
// ServeHTTP handles every HTTP request.
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
startTime := time.Now()
defer func() {
requestDuration := time.Since(startTime)
log.Printf("[HTTP Request] from %s to %s, duration %.4fs\n", r.RemoteAddr, r.URL.Path, requestDuration.Seconds())
log.Printf("[HTTP Request] from %s to %s, duration %.4fs\n", req.RemoteAddr, req.URL.Path, requestDuration.Seconds())
}()
// Validate request and extract needed info.
rInfo, err := h.getRequestInfo(r)
rInfo, err := h.getRequestInfo(req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 bad request - " + err.Error()))
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("400 bad request - " + err.Error()))
return
}
@ -144,14 +144,14 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "apps_list":
appsList, err := h.getAppsList()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 bad request - " + err.Error()))
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("400 bad request - " + err.Error()))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(appsList)
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write(appsList)
return
case "info":
@ -167,17 +167,18 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Version: common.Version,
}
// nolint:errchkjson
infoBytes, _ := json.Marshal(infoData)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(infoBytes)
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write(infoBytes)
return
case "metrics":
handler, found := h.handlers[rInfo.Application]
if !found {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 bad request - " + errInvalidApplication.Error()))
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("400 bad request - " + errInvalidApplication.Error()))
return
}
@ -185,16 +186,16 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Get data from handler.
data := handler(rInfo)
if data == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 bad request - " + errNoData.Error()))
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("400 bad request - " + errNoData.Error()))
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(data))
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte(data))
return
}
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 bad request - " + errInvalidPath.Error()))
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("400 bad request - " + errInvalidPath.Error()))
}

View File

@ -25,15 +25,16 @@ type HTTPServer struct {
// NewHTTPServer creates HTTP server and executes preliminary initialization
// (HTTP server structure initialized but it doesn't start).
func NewHTTPServer(ctx context.Context, cfg *configuration.Config, logger *logger.Logger) (*HTTPServer, chan struct{}) {
h := &HTTPServer{
// nolint:exhaustruct
httpServer := &HTTPServer{
config: cfg,
ctx: ctx,
doneChan: make(chan struct{}),
logger: logger,
}
h.initialize()
httpServer.initialize()
return h, h.doneChan
return httpServer, httpServer.doneChan
}
// Returns request's context based on main context of application.
@ -50,7 +51,7 @@ func (h *HTTPServer) initialize() {
handlers: make(map[string]common.HTTPHandlerFunc),
}
// We do not need to specify all possible parameters for HTTP server, so:
// nolint:exhaustivestruct
// nolint:exhaustruct
h.server = &http.Server{
// ToDo: make it all configurable.
Addr: ":34421",

View File

@ -9,6 +9,10 @@ type Logger struct {
// NewLogger creates new logging wrapper and returns it to caller.
func NewLogger(config *Config) *Logger {
if config == nil {
config = &Config{Debug: false}
}
l := &Logger{config: config}
return l

View File

@ -14,25 +14,27 @@ var ErrMetricNotFound = errors.New("metric not found")
// Storage is an in-memory storage.
type Storage struct {
dataMutex sync.RWMutex
ctx context.Context
doneChan chan struct{}
logger *logger.Logger
data map[string]schema.Metric
name string
dataMutex sync.RWMutex
}
// NewStorage creates new in-memory storage to use.
func NewStorage(ctx context.Context, name string, logger *logger.Logger) (*Storage, chan struct{}) {
s := &Storage{
// nolint:exhaustruct
storage := &Storage{
ctx: ctx,
doneChan: make(chan struct{}),
logger: logger,
name: name,
data: make(map[string]schema.Metric),
}
s.initialize()
storage.initialize()
return s, s.doneChan
return storage, storage.doneChan
}
// Get returns data from storage by key.

View File

@ -23,13 +23,14 @@ type Client struct {
// NewClient creates new Metricator client.
func NewClient(config *Config, logger *logger.Logger) *Client {
c := &Client{
// nolint:exhaustruct
client := &Client{
config: config,
logger: logger,
}
c.initialize()
client.initialize()
return c
return client
}
// Executes request and parses it's contents.
@ -139,7 +140,7 @@ func (c *Client) GetMetricsList(appName string) schema.Metrics {
// Initializes internal states and storages.
func (c *Client) initialize() {
// We do not need to set everything for client actually, so:
// nolint:exhaustivestruct
// nolint:exhaustruct
c.httpClient = &http.Client{
Timeout: time.Second * time.Duration(c.config.Timeout),
}

View File

@ -18,7 +18,7 @@ type Metric struct {
// NewMetric creates new structure for storing single metric data.
func NewMetric(name, mType, description string, params []string) Metric {
m := Metric{
metric := Metric{
BaseName: name,
Name: name,
Description: description,
@ -27,7 +27,7 @@ func NewMetric(name, mType, description string, params []string) Metric {
Value: "",
}
return m
return metric
}
// GetValue returns metric's value.