Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Cleaner interface and better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Dec 15, 2016
1 parent fe98f48 commit 21c5bdd
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 131 deletions.
38 changes: 0 additions & 38 deletions metric/metric.go

This file was deleted.

37 changes: 11 additions & 26 deletions metric/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

package metric

import "time"

// Type is a metric type
type Type int8

Expand Down Expand Up @@ -57,41 +55,28 @@ type Gauge struct {
Value float64
}

// RawMetric represents an unaggregated raw metric, which consists of a union
// of different types of metrics, only one of which is valid at any given time.
// The actual type of the metric depends on the type field, which determines
// which value field is valid. We intentionally do not use pointers to avoid
// the GC overhead of marking and sweeping the raw metrics.
type RawMetric struct {
// OneOf is a union of different types of metrics, only one of which is valid
// at any given time. The actual type of the metric depends on the type field,
// which determines which value field is valid. We intentionally do not use
// pointers to avoid the GC overhead of marking and sweeping the metrics.
type OneOf struct {
Type Type
ID IDType
CounterVal int64
BatchTimerVal []float64
GaugeVal float64
}

var emptyRawMetric RawMetric
var emptyOneOf OneOf

// Reset resets the raw metric
func (m *RawMetric) Reset() { *m = emptyRawMetric }
// Reset resets the metric
func (m *OneOf) Reset() { *m = emptyOneOf }

// Counter returns the counter metric
func (m *RawMetric) Counter() Counter { return Counter{ID: m.ID, Value: m.CounterVal} }
func (m *OneOf) Counter() Counter { return Counter{ID: m.ID, Value: m.CounterVal} }

// BatchTimer returns the batch timer metric
func (m *RawMetric) BatchTimer() BatchTimer { return BatchTimer{ID: m.ID, Values: m.BatchTimerVal} }
func (m *OneOf) BatchTimer() BatchTimer { return BatchTimer{ID: m.ID, Values: m.BatchTimerVal} }

// Gauge returns the gauge metric
func (m *RawMetric) Gauge() Gauge { return Gauge{ID: m.ID, Value: m.GaugeVal} }

// Metric is the metric interface
type Metric interface {
// ID is the metric id
ID() IDType

// Timestamp is the metric timestamp
Timestamp() time.Time

// Value is the metric value
Value() float64
}
func (m *OneOf) Gauge() Gauge { return Gauge{ID: m.ID, Value: m.GaugeVal} }
50 changes: 25 additions & 25 deletions protocol/msgpack/raw_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package msgpack

import (
"fmt"

"github.com/m3db/m3metrics/metric"
"github.com/m3db/m3metrics/policy"
)
Expand Down Expand Up @@ -64,41 +62,43 @@ func (enc *rawEncoder) Reset(encoder BufferedEncoder) {
enc.encoder = encoder
}

func (enc *rawEncoder) Encode(m *metric.RawMetric, p policy.VersionedPolicies) error {
func (enc *rawEncoder) EncodeCounter(c metric.Counter, p policy.VersionedPolicies) error {
if enc.err != nil {
return enc.err
}
enc.encodeVersion(supportedVersion)
enc.encodeType(m.Type)
enc.encodeID(m.ID)
switch m.Type {
case metric.CounterType:
enc.encodeCounterValue(m.CounterVal)
case metric.BatchTimerType:
enc.encodeBatchTimerValue(m.BatchTimerVal)
case metric.GaugeType:
enc.encodeGaugeValue(m.GaugeVal)
default:
enc.err = fmt.Errorf("unrecognized metric type %v", m.Type)
return enc.err
}
enc.encodeType(metric.CounterType)
enc.encodeID(c.ID)
enc.encodeVarintFn(int64(c.Value))
enc.encodeVersionedPolicies(p)
return enc.err
}

func (enc *rawEncoder) encodeCounterValue(value int64) {
enc.encodeVarintFn(value)
}

func (enc *rawEncoder) encodeBatchTimerValue(values []float64) {
enc.encodeArrayLenFn(len(values))
for _, v := range values {
func (enc *rawEncoder) EncodeBatchTimer(bt metric.BatchTimer, p policy.VersionedPolicies) error {
if enc.err != nil {
return enc.err
}
enc.encodeVersion(supportedVersion)
enc.encodeType(metric.BatchTimerType)
enc.encodeID(bt.ID)
enc.encodeArrayLenFn(len(bt.Values))
for _, v := range bt.Values {
enc.encodeFloat64Fn(v)
}
enc.encodeVersionedPolicies(p)
return enc.err
}

func (enc *rawEncoder) encodeGaugeValue(value float64) {
enc.encodeFloat64Fn(value)
func (enc *rawEncoder) EncodeGauge(g metric.Gauge, p policy.VersionedPolicies) error {
if enc.err != nil {
return enc.err
}
enc.encodeVersion(supportedVersion)
enc.encodeType(metric.GaugeType)
enc.encodeID(g.ID)
enc.encodeFloat64Fn(g.Value)
enc.encodeVersionedPolicies(p)
return enc.err
}

func (enc *rawEncoder) encodeVersionedPolicies(p policy.VersionedPolicies) {
Expand Down
52 changes: 26 additions & 26 deletions protocol/msgpack/raw_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func testGoodCapturingRawEncoder(t *testing.T) (RawEncoder, *[]interface{}) {
return rawEncoder, &result
}

func getExpectedResults(t *testing.T, m *metric.RawMetric, p policy.VersionedPolicies) []interface{} {
func getExpectedResults(t *testing.T, m *metric.OneOf, p policy.VersionedPolicies) []interface{} {
results := []interface{}{
int64(supportedVersion),
int64(m.Type),
Expand Down Expand Up @@ -100,23 +100,23 @@ func getExpectedResults(t *testing.T, m *metric.RawMetric, p policy.VersionedPol
func TestRawEncodeCounterWithDefaultPolicies(t *testing.T) {
policies := policy.DefaultVersionedPolicies
encoder, results := testGoodCapturingRawEncoder(t)
require.NoError(t, encoder.Encode(&testCounter, policies))
require.NoError(t, testEncode(t, encoder, &testCounter, policies))
expected := getExpectedResults(t, &testCounter, policies)
require.Equal(t, expected, *results)
}

func TestRawEncodeBatchTimerWithDefaultPolicies(t *testing.T) {
policies := policy.DefaultVersionedPolicies
encoder, results := testGoodCapturingRawEncoder(t)
require.NoError(t, encoder.Encode(&testBatchTimer, policies))
require.NoError(t, testEncode(t, encoder, &testBatchTimer, policies))
expected := getExpectedResults(t, &testBatchTimer, policies)
require.Equal(t, expected, *results)
}

func TestRawEncodeGaugeWithDefaultPolicies(t *testing.T) {
policies := policy.DefaultVersionedPolicies
encoder, results := testGoodCapturingRawEncoder(t)
require.NoError(t, encoder.Encode(&testGauge, policies))
require.NoError(t, testEncode(t, encoder, &testGauge, policies))
expected := getExpectedResults(t, &testGauge, policies)
require.Equal(t, expected, *results)
}
Expand All @@ -125,7 +125,7 @@ func TestRawEncodeAllTypesWithDefaultPolicies(t *testing.T) {
var expected []interface{}
encoder, results := testGoodCapturingRawEncoder(t)
for _, input := range testInputWithAllTypesAndDefaultPolicies {
require.NoError(t, encoder.Encode(&input.metric, input.policies))
require.NoError(t, testEncode(t, encoder, &input.metric, input.policies))
expected = append(expected, getExpectedResults(t, &input.metric, input.policies)...)
}

Expand All @@ -136,7 +136,7 @@ func TestRawEncodeAllTypesWithCustomPolicies(t *testing.T) {
var expected []interface{}
encoder, results := testGoodCapturingRawEncoder(t)
for _, input := range testInputWithAllTypesAndCustomPolicies {
require.NoError(t, encoder.Encode(&input.metric, input.policies))
require.NoError(t, testEncode(t, encoder, &input.metric, input.policies))
expected = append(expected, getExpectedResults(t, &input.metric, input.policies)...)
}

Expand All @@ -148,50 +148,50 @@ func TestRawEncodeCounterError(t *testing.T) {
policies := policy.DefaultVersionedPolicies

// Intentionally return an error when encoding varint
rawEncoder := testRawEncoder(t).(*rawEncoder)
rawEncoder.encodeVarintFn = func(value int64) {
rawEncoder.err = errTestVarint
encoder := testRawEncoder(t).(*rawEncoder)
encoder.encodeVarintFn = func(value int64) {
encoder.err = errTestVarint
}

// Assert the error is expected
require.Equal(t, errTestVarint, rawEncoder.Encode(&counter, policies))
require.Equal(t, errTestVarint, testEncode(t, encoder, &counter, policies))

// Assert re-encoding doesn't change the error
require.Equal(t, errTestVarint, rawEncoder.Encode(&counter, policies))
require.Equal(t, errTestVarint, testEncode(t, encoder, &counter, policies))
}

func TestRawEncodeBatchTimerError(t *testing.T) {
timer := testBatchTimer
policies := policy.DefaultVersionedPolicies

// Intentionally return an error when encoding array length
rawEncoder := testRawEncoder(t).(*rawEncoder)
rawEncoder.encodeArrayLenFn = func(value int) {
rawEncoder.err = errTestArrayLen
encoder := testRawEncoder(t).(*rawEncoder)
encoder.encodeArrayLenFn = func(value int) {
encoder.err = errTestArrayLen
}

// Assert the error is expected
require.Equal(t, errTestArrayLen, rawEncoder.Encode(&timer, policies))
require.Equal(t, errTestArrayLen, testEncode(t, encoder, &timer, policies))

// Assert re-encoding doesn't change the error
require.Equal(t, errTestArrayLen, rawEncoder.Encode(&timer, policies))
require.Equal(t, errTestArrayLen, testEncode(t, encoder, &timer, policies))
}

func TestRawEncodeGaugeError(t *testing.T) {
gauge := testGauge
policies := policy.DefaultVersionedPolicies

// Intentionally return an error when encoding float64
rawEncoder := testRawEncoder(t).(*rawEncoder)
rawEncoder.encodeFloat64Fn = func(value float64) {
rawEncoder.err = errTestFloat64
encoder := testRawEncoder(t).(*rawEncoder)
encoder.encodeFloat64Fn = func(value float64) {
encoder.err = errTestFloat64
}

// Assert the error is expected
require.Equal(t, errTestFloat64, rawEncoder.Encode(&gauge, policies))
require.Equal(t, errTestFloat64, testEncode(t, encoder, &gauge, policies))

// Assert re-encoding doesn't change the error
require.Equal(t, errTestFloat64, rawEncoder.Encode(&gauge, policies))
require.Equal(t, errTestFloat64, testEncode(t, encoder, &gauge, policies))
}

func TestRawEncodePolicyError(t *testing.T) {
Expand All @@ -207,14 +207,14 @@ func TestRawEncodePolicyError(t *testing.T) {
}

// Intentionally return an error when encoding array length
rawEncoder := testRawEncoder(t).(*rawEncoder)
rawEncoder.encodeArrayLenFn = func(value int) {
rawEncoder.err = errTestArrayLen
encoder := testRawEncoder(t).(*rawEncoder)
encoder.encodeArrayLenFn = func(value int) {
encoder.err = errTestArrayLen
}

// Assert the error is expected
require.Equal(t, errTestArrayLen, rawEncoder.Encode(&gauge, policies))
require.Equal(t, errTestArrayLen, testEncode(t, encoder, &gauge, policies))

// Assert re-encoding doesn't change the error
require.Equal(t, errTestArrayLen, rawEncoder.Encode(&gauge, policies))
require.Equal(t, errTestArrayLen, testEncode(t, encoder, &gauge, policies))
}
4 changes: 2 additions & 2 deletions protocol/msgpack/raw_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type rawIterator struct {
decoder *msgpack.Decoder // internal decoder that does the actual decoding
floatsPool xpool.FloatsPool // pool for float slices
policiesPool pool.PoliciesPool // pool for policies
metric metric.RawMetric // current raw metric
metric metric.OneOf // current raw metric
policies policy.VersionedPolicies // current policies
err error // error encountered during decoding

Expand Down Expand Up @@ -83,7 +83,7 @@ func (it *rawIterator) Reset(reader io.Reader) {
it.err = nil
}

func (it *rawIterator) Value() (*metric.RawMetric, policy.VersionedPolicies) {
func (it *rawIterator) Value() (*metric.OneOf, policy.VersionedPolicies) {
return &it.metric, it.policies
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/msgpack/raw_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testGoodRawIterator(

func getMockValuesFor(
t *testing.T,
m *metric.RawMetric,
m *metric.OneOf,
p policy.VersionedPolicies,
) ([]int64, []float64, [][]byte, []int) {
var (
Expand Down
Loading

0 comments on commit 21c5bdd

Please sign in to comment.