Skip to content

Commit

Permalink
Requires that metrics with the same name have the same Kind.
Browse files Browse the repository at this point in the history
The store add enforces that the first metric added to the store is the
definitive Kind of a metric.  All subsequent metrics added must have that kind,
or the program load fails.

Issue #59
  • Loading branch information
jaqx0r committed Jul 22, 2017
1 parent fc91480 commit 6f9866c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GOTESTFILES=\
exporter/varz_test.go\
metrics/datum/int_test.go\
metrics/metric_test.go\
metrics/store_test.go\
mtail/mtail_test.go\
tailer/tail_test.go\
testdata/reader.go\
Expand Down
10 changes: 9 additions & 1 deletion metrics/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metrics

import (
"encoding/json"
"fmt"
"sync"
)

Expand All @@ -21,10 +22,17 @@ func NewStore() (s *Store) {
}

// Add is used to add one metric to the Store.
func (s *Store) Add(m *Metric) {
func (s *Store) Add(m *Metric) error {
s.Lock()
defer s.Unlock()
if len(s.Metrics[m.Name]) > 0 {
t := s.Metrics[m.Name][0].Kind
if m.Kind != t {
return fmt.Errorf("Metric %s has different kind %s to existing %s.", m.Name, m.Kind, t)
}
}
s.Metrics[m.Name] = append(s.Metrics[m.Name], m)
return nil
}

// ClearMetrics empties the store of all metrics.
Expand Down
20 changes: 20 additions & 0 deletions metrics/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2017 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package metrics

import "testing"

func TestMatchingKind(t *testing.T) {
s := NewStore()
m1 := NewMetric("foo", "prog", Counter, Int)
err := s.Add(m1)
if err != nil {
t.Fatalf("should be nil: %s", err)
}
m2 := NewMetric("foo", "prog1", Gauge, Int)
err = s.Add(m2)
if err == nil {
t.Fatal("should be err")
}
}
27 changes: 21 additions & 6 deletions vm/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,46 @@ func (l *Loader) CompileAndRun(name string, input io.Reader) error {
ProgLoadErrors.Add(name, 1)
return fmt.Errorf("Internal error: Compilation failed for %s: No program returned, but no errors.", name)
}

if l.dumpBytecode {
glog.Info("Dumping program objects and bytecode\n", v.DumpByteCode(name))
}

// Load the metrics from the compilation into the global metric storage for export.
for _, m := range v.m {
if !m.Hidden {
l.ms.Add(m)
err := l.ms.Add(m)
if err != nil {
return err
}
}
}
if l.dumpBytecode {
glog.Info("Dumping program objects and bytecode\n", v.DumpByteCode(name))
}

ProgLoads.Add(name, 1)
glog.Infof("Loaded program %s", name)

if l.compileOnly {
return nil
}
ProgLoads.Add(name, 1)
glog.Infof("Loaded program %s", name)

l.handleMu.Lock()
defer l.handleMu.Unlock()

// Stop any previous VM.
if handle, ok := l.handles[name]; ok {
glog.Infof("Stopping program %s", name)
close(handle.lines)
<-handle.done
glog.Info("Stopped")
}

l.handles[name] = &vmHandle{make(chan *tailer.LogLine), make(chan struct{})}
nameCode := nameToCode(name)
glog.Infof("Program %s has goroutine marker 0x%x", name, nameCode)
go v.Run(nameCode, l.handles[name].lines, l.handles[name].done)

glog.Infof("Program %s running", name)

return nil
}

Expand Down

0 comments on commit 6f9866c

Please sign in to comment.