File: submitter_dummy.go

package info (click to toggle)
fever 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 512 kB
  • sloc: makefile: 17; sh: 12
file content (70 lines) | stat: -rw-r--r-- 1,724 bytes parent folder | download | duplicates (4)
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
package util

// DCSO FEVER
// Copyright (c) 2018, DCSO GmbH

import (
	"unicode"

	log "github.com/sirupsen/logrus"
)

// DummySubmitter is a StatsSubmitter that just logs submissions without
// sending them over the network.
type DummySubmitter struct {
	Logger   *log.Entry
	SensorID string
}

func isASCIIPrintable(s string) bool {
	for _, r := range s {
		if r > unicode.MaxASCII || !unicode.IsPrint(r) {
			return false
		}
	}
	return true
}

// MakeDummySubmitter creates a new submitter just logging to the default log
// target.
func MakeDummySubmitter() (*DummySubmitter, error) {
	mySubmitter := &DummySubmitter{
		Logger: log.WithFields(log.Fields{
			"domain":    "submitter",
			"submitter": "dummy",
		}),
	}
	sensorID, err := GetSensorID()
	if err != nil {
		return nil, err
	}
	mySubmitter.SensorID = sensorID
	return mySubmitter, nil
}

// UseCompression enables gzip compression of submitted payloads (not
// applicable in this implementation).
func (s *DummySubmitter) UseCompression() {
	// pass
}

// Submit logs the rawData payload.
func (s *DummySubmitter) Submit(rawData []byte, key string, contentType string) {
	s.SubmitWithHeaders(rawData, key, contentType, nil)
}

// SubmitWithHeaders logs rawData payload, adding some extra key-value pairs to
// the header.
func (s *DummySubmitter) SubmitWithHeaders(rawData []byte, key string, contentType string, myHeaders map[string]string) {
	bytestring := string(rawData)
	if isASCIIPrintable(bytestring) {
		s.Logger.Info(bytestring)
	} else {
		s.Logger.Infof("%s (%s) - submitting non-printable byte array of length %d", key, contentType, len(rawData))
	}
}

// Finish is a no-op in this implementation.
func (s *DummySubmitter) Finish() {
	// pass
}