Permalink
Browse files

metrics, client: fix string type writing

  • Loading branch information...
1 parent 36ada7d commit 84aefedc1c1c2ee6aabf104190757638ee91d7e7 @suyash suyash committed Jul 22, 2016
Showing with 27 additions and 15 deletions.
  1. +11 −1 client.go
  2. +16 −14 metrics.go
View
@@ -184,6 +184,11 @@ func (c *PCPClient) initializeSingletonMetricOffsets(metric *PCPSingletonMetric,
metric.valueoffset = *valuesoffset
*valuesoffset += ValueLength
+ if metric.t == StringType {
+ metric.val.(*PCPString).offset = *stringsoffset
+ *stringsoffset += StringLength
+ }
+
if metric.shortDescription.val != "" {
metric.shortDescription.offset = *stringsoffset
*stringsoffset += StringLength
@@ -202,6 +207,11 @@ func (c *PCPClient) initializeInstanceMetricOffsets(metric *PCPInstanceMetric, m
for name := range metric.indom.instances {
metric.vals[name].offset = *valuesoffset
*valuesoffset += ValueLength
+
+ if metric.t == StringType {
+ metric.vals[name].val.(*PCPString).offset = *stringsoffset
+ *stringsoffset += StringLength
+ }
}
if metric.shortDescription.val != "" {
@@ -388,7 +398,7 @@ func (c *PCPClient) writeInstance(t MetricType, val interface{}, valueoffset int
c.buffer.MustWriteUint64(uint64(offset))
}
- update := newupdateClosure(offset, c.buffer, t)
+ update := newupdateClosure(offset, c.buffer)
_ = update(val)
c.buffer.MustSetPos(valueoffset + MaxDataValueSize)
View
@@ -354,14 +354,12 @@ func (md *PCPMetricDesc) Description() string {
type updateClosure func(interface{}) error
// newupdateClosure creates a new update closure for an offset, type and buffer
-func newupdateClosure(offset int, buffer bytebuffer.Buffer, t MetricType) updateClosure {
+func newupdateClosure(offset int, buffer bytebuffer.Buffer) updateClosure {
return func(val interface{}) error {
- if t == StringType {
+ if _, isString := val.(string); isString {
buffer.MustSetPos(offset)
buffer.MustWrite(make([]byte, StringLength))
- val = val.(*PCPString).val
}
-
buffer.MustSetPos(offset)
return buffer.WriteVal(val)
}
@@ -411,8 +409,6 @@ func (m *PCPSingletonMetric) Set(val interface{}) error {
return errors.New("the value is incompatible with this metrics MetricType")
}
- val = m.t.resolve(val)
-
if val != m.val {
m.Lock()
defer m.Unlock()
@@ -516,22 +512,28 @@ func (m *PCPInstanceMetric) ValInstance(instance string) (interface{}, error) {
// SetInstance sets the value for a particular instance of the metric
func (m *PCPInstanceMetric) SetInstance(instance string, val interface{}) error {
+ if !m.t.IsCompatible(val) {
+ return errors.New("the value is incompatible with this metrics MetricType")
+ }
+
if !m.indom.HasInstance(instance) {
return fmt.Errorf("%v is not an instance of this metric", instance)
}
- m.Lock()
- defer m.Unlock()
+ if m.vals[instance].val != val {
+ m.Lock()
+ defer m.Unlock()
- val = m.t.resolve(val)
- if m.vals[instance].update != nil {
- err := m.vals[instance].update(val)
- if err != nil {
- return err
+ if m.vals[instance].update != nil {
+ err := m.vals[instance].update(val)
+ if err != nil {
+ return err
+ }
}
+
+ m.vals[instance].val = val
}
- m.vals[instance].val = val
return nil
}

0 comments on commit 84aefed

Please sign in to comment.