Skip to content

Commit 2b7731c

Browse files
committed
feat: add volume metric
1 parent 2dedc15 commit 2b7731c

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

metrics/candlestick.go

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,70 @@ import (
99
)
1010

1111
type Int64Candlestick struct {
12-
metric otelapi.Int64ObservableGauge
12+
gauge otelapi.Int64ObservableGauge
1313

1414
attrOpen, attrClose []attribute.KeyValue
1515
attrHigh, attrLow []attribute.KeyValue
16+
attrVolume []attribute.KeyValue
1617

17-
open, close, count int64
18-
high, low int64
18+
open, close int64
19+
high, low int64
20+
volume int64
1921

2022
mx sync.Mutex
2123
}
2224

2325
func NewInt64Candlestick(
2426
name, description, uom string,
2527
attributes ...attribute.KeyValue,
26-
) (*Int64Candlestick, error) {
27-
var err error
28-
29-
c := &Int64Candlestick{
30-
attrOpen: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("open")}),
31-
attrClose: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("close")}),
32-
attrHigh: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("high")}),
33-
attrLow: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("low")}),
28+
) (c *Int64Candlestick, err error) {
29+
c = &Int64Candlestick{
30+
attrOpen: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("open")}),
31+
attrClose: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("close")}),
32+
attrHigh: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("high")}),
33+
attrLow: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("low")}),
34+
attrVolume: append(attributes, attribute.KeyValue{Key: "type", Value: attribute.StringValue("volume")}),
3435
}
3536

3637
options := []otelapi.Int64ObservableGaugeOption{
3738
otelapi.WithDescription(description),
3839
}
39-
4040
if uom != "" {
4141
options = append(options, otelapi.WithUnit(uom))
4242
}
4343

44-
if c.metric, err = meter.Int64ObservableGauge(name, options...); err != nil {
44+
if c.gauge, err = meter.Int64ObservableGauge(name, options...); err != nil {
4545
return nil, err
4646
}
4747

48-
return c, nil
48+
return
4949
}
5050

5151
func (c *Int64Candlestick) registerCallback(m otelapi.Meter) (otelapi.Registration, error) {
52-
return m.RegisterCallback(c.observe, c.metric)
52+
return m.RegisterCallback(c.observe, c.gauge)
5353
}
5454

5555
func (c *Int64Candlestick) observe(ctx context.Context, o otelapi.Observer) error {
5656
c.mx.Lock()
5757
defer c.mx.Unlock()
5858

59-
if c.count == 0 {
59+
if c.volume == 0 {
6060
return nil
6161
}
6262

63-
c.close = c.close / c.count
63+
c.close = c.close / c.volume
6464

65-
o.ObserveInt64(c.metric, c.open, otelapi.WithAttributes(c.attrOpen...))
66-
o.ObserveInt64(c.metric, c.close, otelapi.WithAttributes(c.attrClose...))
67-
o.ObserveInt64(c.metric, c.high, otelapi.WithAttributes(c.attrHigh...))
68-
o.ObserveInt64(c.metric, c.low, otelapi.WithAttributes(c.attrLow...))
65+
o.ObserveInt64(c.gauge, c.open, otelapi.WithAttributes(c.attrOpen...))
66+
o.ObserveInt64(c.gauge, c.close, otelapi.WithAttributes(c.attrClose...))
67+
o.ObserveInt64(c.gauge, c.high, otelapi.WithAttributes(c.attrHigh...))
68+
o.ObserveInt64(c.gauge, c.low, otelapi.WithAttributes(c.attrLow...))
69+
o.ObserveInt64(c.gauge, c.volume, otelapi.WithAttributes(c.attrVolume...))
6970

7071
c.open = c.close
7172
c.close = 0
72-
c.count = 0
7373
c.high = 0
7474
c.low = 0
75+
c.volume = 0
7576

7677
return nil
7778
}
@@ -80,19 +81,15 @@ func (c *Int64Candlestick) Record(ctx context.Context, value int64) {
8081
c.mx.Lock()
8182
defer c.mx.Unlock()
8283

83-
if c.count == 0 {
84-
c.high = value
85-
c.low = value
86-
}
84+
c.close += value
8785

88-
if value > c.high {
86+
if c.volume == 0 || value > c.high {
8987
c.high = value
9088
}
9189

92-
if value < c.low {
90+
if c.volume == 0 || value < c.low {
9391
c.low = value
9492
}
9593

96-
c.close += value
97-
c.count++
94+
c.volume++
9895
}

0 commit comments

Comments
 (0)