Skip to content

Commit

Permalink
Adds a flag to control if the prog label is emitted by exporters.
Browse files Browse the repository at this point in the history
Returns the emission of the prometheus prog label, unless activated by this
flag.  Varz also changes behaviour.  The other push exporters are not modified
as the prog label is intrinsic to their formats (at the moment.)

Fixes #59
  • Loading branch information
jaqx0r committed Jul 23, 2017
1 parent a9b6960 commit d160163
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
11 changes: 6 additions & 5 deletions exporter/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ var (
// Exporter manages the export of metrics to passive and active collectors.
type Exporter struct {
store *metrics.Store
hostname string
o Options
pushTargets []pushOptions
}

// Options contains the required and optional parameters for constructing an
// Exporter.
type Options struct {
Store *metrics.Store
Hostname string // Not required, uses os.Hostname if zero.
Store *metrics.Store
Hostname string // Not required, uses os.Hostname if zero.
OmitProgLabel bool // If true, don't emit the prog label that identifies the source program in variable exports.
}

// New creates a new Exporter.
Expand All @@ -52,7 +53,7 @@ func New(o Options) (*Exporter, error) {
return nil, fmt.Errorf("Error getting hostname: %s\n", err)
}
}
e := &Exporter{store: o.Store, hostname: hostname}
e := &Exporter{store: o.Store, o: o}

if *collectdSocketPath != "" {
o := pushOptions{"unix", *collectdSocketPath, metricToCollectd, collectdExportTotal, collectdExportSuccess}
Expand Down Expand Up @@ -100,7 +101,7 @@ func (e *Exporter) writeSocketMetrics(c net.Conn, f formatter, exportTotal *expv
lc := make(chan *metrics.LabelSet)
go m.EmitLabelSets(lc)
for l := range lc {
line := f(e.hostname, m, l)
line := f(e.o.Hostname, m, l)
n, err := fmt.Fprint(c, line)
glog.V(2).Infof("Sent %d bytes\n", n)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion exporter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestHandleJSON(t *testing.T) {
for _, metric := range tc.metrics {
ms.Add(metric)
}
o := Options{ms, "gunstar"}
o := Options{ms, "gunstar", false}
e, err := New(o)
if err != nil {
t.Fatalf("couldn't make exporter: %s", err)
Expand Down
7 changes: 5 additions & 2 deletions exporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ func (e *Exporter) HandlePrometheusMetrics(w http.ResponseWriter, r *http.Reques
if m.Source != "" {
fmt.Fprintf(w, "# %s defined at %s\n", noHyphens(m.Name), m.Source)
}
line := metricToPrometheus(e.hostname, m, l)
line := metricToPrometheus(e.o, m, l)
fmt.Fprint(w, line)
}
m.RUnlock()
}
}
}

func metricToPrometheus(hostname string, m *metrics.Metric, l *metrics.LabelSet) string {
func metricToPrometheus(options Options, m *metrics.Metric, l *metrics.LabelSet) string {
var s []string
for k, v := range l.Labels {
// Prometheus quotes the value of each label=value pair.
s = append(s, fmt.Sprintf("%s=%q", k, v))
}
sort.Strings(s)
if !options.OmitProgLabel {
s = append(s, fmt.Sprintf("prog=\"%s\"", m.Program))
}
return fmt.Sprintf(prometheusFormat,
noHyphens(m.Name),
strings.Join(s, ","),
Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestHandlePrometheus(t *testing.T) {
for _, metric := range tc.metrics {
ms.Add(metric)
}
o := Options{ms, "gunstar"}
o := Options{ms, "gunstar", true}
e, err := New(o)
if err != nil {
t.Fatalf("couldn't make exporter: %s", err)
Expand Down
10 changes: 6 additions & 4 deletions exporter/varz.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ func (e *Exporter) HandleVarz(w http.ResponseWriter, r *http.Request) {
lc := make(chan *metrics.LabelSet)
go m.EmitLabelSets(lc)
for l := range lc {
line := metricToVarz(e.hostname, m, l)
line := metricToVarz(e.o, m, l)
fmt.Fprint(w, line)
}
m.RUnlock()
}
}
}

func metricToVarz(hostname string, m *metrics.Metric, l *metrics.LabelSet) string {
func metricToVarz(o Options, m *metrics.Metric, l *metrics.LabelSet) string {
var s []string
for k, v := range l.Labels {
s = append(s, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(s)
s = append(s, fmt.Sprintf("prog=%s", m.Program))
s = append(s, fmt.Sprintf("instance=%s", hostname))
if !o.OmitProgLabel {
s = append(s, fmt.Sprintf("prog=%s", m.Program))
}
s = append(s, fmt.Sprintf("instance=%s", o.Hostname))
return fmt.Sprintf(varzFormat,
m.Name,
strings.Join(s, ","),
Expand Down
2 changes: 1 addition & 1 deletion exporter/varz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestHandleVarz(t *testing.T) {
for _, metric := range tc.metrics {
ms.Add(metric)
}
o := Options{ms, "gunstar"}
o := Options{ms, "gunstar", false}
e, err := New(o)
if err != nil {
t.Fatalf("couldn't make exporter: %s", err)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ var (
port = flag.String("port", "3903", "HTTP port to listen on.")
progs = flag.String("progs", "", "Name of the directory containing mtail programs")

// Compiler behaviour flags
oneShot = flag.Bool("one_shot", false, "Run the contents of the provided logs until EOF and exit.")
oneShotMetrics = flag.Bool("one_shot_metrics", false, "Dump metrics (to stdout) after one shot mode.")
compileOnly = flag.Bool("compile_only", false, "Compile programs only, do not load the virtual machine.")
dumpAst = flag.Bool("dump_ast", false, "Dump AST of programs after parse (to INFO log).")
dumpAstTypes = flag.Bool("dump_ast_types", false, "Dump AST of programs with type annotation after typecheck (to INFO log).")
dumpBytecode = flag.Bool("dump_bytecode", false, "Dump bytecode of programs (to INFO log).")

// Runtime behaviour flags
syslogUseCurrentYear = flag.Bool("syslog_use_current_year", true, "Patch yearless timestamps with the present year.")
emitProgLabel = flag.Bool("emit_prog_label", true, "Emit the 'prog' label in variable exports.")
)

func init() {
Expand Down Expand Up @@ -108,6 +111,7 @@ func main() {
DumpAstTypes: *dumpAstTypes,
DumpBytecode: *dumpBytecode,
SyslogUseCurrentYear: *syslogUseCurrentYear,
OmitProgLabel: !*emitProgLabel,
BuildInfo: buildInfo(),
}
m, err := mtail.New(o)
Expand Down
3 changes: 2 additions & 1 deletion mtail/mtail.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type Options struct {
DumpBytecode bool
SyslogUseCurrentYear bool
OmitMetricSource bool
OmitProgLabel bool

BuildInfo string

Expand All @@ -226,7 +227,7 @@ func New(o Options) (*MtailServer, error) {
return nil, err
}

m.e, err = exporter.New(exporter.Options{Store: m.store})
m.e, err = exporter.New(exporter.Options{Store: m.store, OmitProgLabel: o.OmitProgLabel})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d160163

Please sign in to comment.