Permalink
Browse files

Merge pull request #12 from performancecopilot/string-type-support

add string type support for metrics, closes #4
  • Loading branch information...
2 parents ab516c9 + cd35ca6 commit ed0d7f845c8ef0bafde41f9d2cd50bb1f80157a0 @suyash suyash committed Jul 18, 2016
Showing with 87 additions and 17 deletions.
  1. +42 −0 examples/simple_string_metric/main.go
  2. +14 −0 metrics.go
  3. +7 −3 registry.go
  4. +24 −14 writer.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "flag"
+ "time"
+
+ "github.com/performancecopilot/speed"
+)
+
+var timelimit = flag.Int("time", 60, "number of seconds to run for")
+
+func main() {
+ flag.Parse()
+
+ w, err := speed.NewPCPWriter("stringtest", speed.ProcessFlag)
+ if err != nil {
+ panic(err)
+ }
+
+ names := []string{
+ "Batman",
+ "Robin",
+ "Nightwing",
+ "Batgirl",
+ "Red Robin",
+ "Red Hood",
+ }
+
+ m, err := w.RegisterString("bat.names", names[0], speed.InstantSemantics, speed.StringType, speed.OneUnit)
+ if err != nil {
+ panic(err)
+ }
+
+ w.Start()
+ defer w.Stop()
+
+ metric := m.(speed.SingletonMetric)
+ for i := 0; i < *timelimit; i++ {
+ metric.Set(names[i%len(names)])
+ time.Sleep(time.Second)
+ }
+}
View
@@ -76,6 +76,8 @@ func (m MetricType) IsCompatible(val interface{}) bool {
return m == FloatType
case float64:
return m.isCompatibleFloat(val.(float64))
+ case string:
+ return m == StringType
default:
return false
}
@@ -110,6 +112,13 @@ func (m MetricType) resolveFloat(val interface{}) interface{} {
}
func (m MetricType) resolve(val interface{}) interface{} {
+ if sval, isString := val.(string); isString {
+ if len(sval) > StringLength {
+ sval = sval[:StringLength]
+ }
+ return NewPCPString(sval)
+ }
+
val = m.resolveInt(val)
val = m.resolveFloat(val)
@@ -138,6 +147,11 @@ func (m MetricType) WriteVal(val interface{}, b bytebuffer.Buffer) error {
return b.WriteFloat32(val.(float32))
case float64:
return b.WriteFloat64(val.(float64))
+ case *PCPString:
+ pos := b.Pos()
+ b.Write(make([]byte, StringLength))
+ b.SetPos(pos)
+ return b.WriteString(val.(*PCPString).val)
}
return errors.New("Invalid Type")
View
@@ -188,10 +188,14 @@ func (r *PCPRegistry) AddMetric(m Metric) error {
r.metrics[m.Name()] = pcpm
+ currentValues := 1
if pcpm.Indom() != nil {
- r.valueCount += pcpm.Indom().InstanceCount()
- } else {
- r.valueCount++
+ currentValues = pcpm.Indom().InstanceCount()
+ }
+
+ r.valueCount += currentValues
+ if pcpm.Type() == StringType {
+ r.stringcount += currentValues
}
if pcpm.ShortDescription().String() != "" {
View
@@ -383,29 +383,39 @@ func (w *PCPWriter) writeMetricDesc(m PCPMetric, pos int) {
}
}
-func (w *PCPWriter) writeSingletonMetric(m *PCPSingletonMetric) {
- w.writeMetricDesc(m, m.descoffset)
+func (w *PCPWriter) writeInstance(t MetricType, val interface{}, valueoffset int) updateClosure {
+ offset := valueoffset
+
+ if t == StringType {
+ w.buffer.SetPos(offset)
+ w.buffer.WriteUint64(StringLength - 1)
+ offset = val.(*PCPString).offset
+ w.buffer.WriteUint64(uint64(offset))
+ }
- m.update = newupdateClosure(m.valueoffset, w.buffer, m.t)
- m.update(m.val)
+ update := newupdateClosure(offset, w.buffer, t)
+ update(val)
- w.buffer.MustSetPos(m.valueoffset + MaxDataValueSize)
- w.buffer.MustWriteInt64(int64(m.descoffset))
- w.buffer.MustWriteInt64(0)
+ w.buffer.SetPos(valueoffset + MaxDataValueSize)
+
+ return update
+}
+
+func (w *PCPWriter) writeSingletonMetric(m *PCPSingletonMetric) {
+ w.writeMetricDesc(m, m.descoffset)
+ m.update = w.writeInstance(m.t, m.val, m.valueoffset)
+ w.buffer.WriteInt64(int64(m.descoffset))
+ w.buffer.WriteInt64(0)
}
func (w *PCPWriter) writeInstanceMetric(m *PCPInstanceMetric) {
w.writeMetricDesc(m, m.descoffset)
for name, i := range m.indom.instances {
ival := m.vals[name]
-
- ival.update = newupdateClosure(ival.offset, w.buffer, m.t)
- ival.update(ival.val)
-
- w.buffer.MustSetPos(ival.offset + MaxDataValueSize)
- w.buffer.MustWriteInt64(int64(m.descoffset))
- w.buffer.MustWriteInt64(int64(i.offset))
+ ival.update = w.writeInstance(m.t, ival.val, ival.offset)
+ w.buffer.WriteInt64(int64(m.descoffset))
+ w.buffer.WriteInt64(int64(i.offset))
}
}

0 comments on commit ed0d7f8

Please sign in to comment.