-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincoming_bytes.go
More file actions
71 lines (59 loc) · 1.69 KB
/
incoming_bytes.go
File metadata and controls
71 lines (59 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package metric
import (
"context"
_ "embed"
"fmt"
"log/slog"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/tailbits/costwatch/internal/costwatch"
)
const (
IncomingBytesPrice = 50
IncomingBytesUnitsPerPrice = 1e9
)
var _ costwatch.Metric = (*IncomingBytes)(nil)
type IncomingBytes struct {
log *slog.Logger
client *cloudwatch.Client
}
func NewIncomingBytes(log *slog.Logger, client *cloudwatch.Client) *IncomingBytes {
return &IncomingBytes{
log: log,
client: client,
}
}
// UnitsPerPrice implements costwatch.Metric.
func (m *IncomingBytes) UnitsPerPrice() float64 {
return IncomingBytesUnitsPerPrice
}
func (m *IncomingBytes) Datapoints(ctx context.Context, label string, start time.Time, end time.Time) ([]costwatch.Datapoint, error) {
data, err := m.client.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{
MetricName: aws.String(m.Label()),
Namespace: aws.String("AWS/Logs"),
Period: aws.Int32(900),
StartTime: &start,
EndTime: &end,
Statistics: []types.Statistic{types.StatisticSum},
})
if err != nil {
return nil, fmt.Errorf("cloudwatch.GetMetricStatistics: %w", err)
}
m.log.Debug("fetched metric data", "points", len(data.Datapoints))
points := make([]costwatch.Datapoint, 0, len(data.Datapoints))
for _, result := range data.Datapoints {
points = append(points, costwatch.Datapoint{
Timestamp: *result.Timestamp,
Value: *result.Sum,
})
}
return points, nil
}
func (m *IncomingBytes) Label() string {
return "IncomingBytes"
}
func (m *IncomingBytes) Price() float64 {
return IncomingBytesPrice
}