generated from sacloud/go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
175 lines (157 loc) · 4.72 KB
/
main.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2022-2023 The sacloud/sacloud-router-usage Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/sacloud/go-otelsetup"
"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/search"
"github.com/sacloud/iaas-api-go/types"
"github.com/sacloud/sacloud-router-usage/version"
usage "github.com/sacloud/sacloud-usage-lib"
"go.opentelemetry.io/otel"
)
func main() {
os.Exit(_main())
}
const appName = "github.com/sacloud/sacloud-router-usage"
func _main() int {
// initialize OTel SDK
otelShutdown, err := otelsetup.InitWithOptions(context.Background(), otelsetup.Options{
ServiceName: "sacloud-router-usage",
ServiceVersion: version.Version,
ServiceNamespace: "sacloud",
})
if err != nil {
log.Println("Error in initializing OTel SDK: " + err.Error())
return usage.ExitUnknown
}
defer func() {
err = errors.Join(err, otelShutdown(context.Background()))
if err != nil {
log.Println("Error in initializing OTel SDK: " + err.Error())
}
}()
// init root span
ctx, span := otel.Tracer(appName).Start(otelsetup.ContextForTrace(context.Background()), "main")
defer span.End()
opts := &commandOpts{
Option: &usage.Option{},
}
if err := usage.ParseOption(opts); err != nil {
log.Println(err)
return usage.ExitUnknown
}
if opts.Version {
usage.PrintVersion(version.Version)
return usage.ExitOk
}
caller, err := usage.SacloudAPICaller("sacloud-router-usage", version.Version)
if err != nil {
log.Println(err)
return usage.ExitUnknown
}
resources, err := fetchResources(ctx, iaas.NewInternetOp(caller), opts)
if err != nil {
log.Println(err)
return usage.ExitUnknown
}
if err := usage.OutputMetrics(os.Stdout, resources.Metrics(), opts.Query); err != nil {
log.Println(err)
return usage.ExitUnknown
}
return usage.ExitOk
}
type commandOpts struct {
*usage.Option
Item string `long:"item" description:"Item name" required:"true" choice:"in" choice:"out" default:"in"`
}
type iaasRouterAPI interface {
Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.InternetFindResult, error)
MonitorRouter(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.RouterActivity, error)
}
func fetchResources(ctx context.Context, client iaasRouterAPI, opts *commandOpts) (*usage.Resources, error) {
ctx, span := otel.Tracer(appName).Start(ctx, "fetchResources")
defer span.End()
rs := &usage.Resources{Label: "routers", Option: opts.Option}
for _, prefix := range opts.Prefix {
for _, zone := range opts.Zones {
condition := &iaas.FindCondition{
Filter: map[search.FilterKey]interface{}{},
}
condition.Filter[search.Key("Name")] = search.PartialMatch(prefix)
result, err := client.Find(
ctx,
zone,
condition,
)
if err != nil {
return nil, err
}
for _, r := range result.Internet {
if !strings.HasPrefix(r.Name, prefix) {
continue
}
monitors, err := fetchRouterActivities(ctx, client, zone, r.ID, opts)
if err != nil {
return nil, err
}
rs.Resources = append(rs.Resources, &usage.Resource{
ID: r.ID,
Name: r.Name,
Zone: zone,
Monitors: monitors,
Label: "traffic",
AdditionalInfo: nil,
})
}
}
}
return rs, nil
}
func fetchRouterActivities(ctx context.Context, client iaasRouterAPI, zone string, id types.ID, opts *commandOpts) ([]usage.MonitorValue, error) {
ctx, span := otel.Tracer(appName).Start(ctx, "fetchRouterActivities")
defer span.End()
b, _ := time.ParseDuration(fmt.Sprintf("-%dm", (opts.Time+3)*5))
condition := &iaas.MonitorCondition{
Start: time.Now().Add(b),
End: time.Now(),
}
activity, err := client.MonitorRouter(ctx, zone, id, condition)
if err != nil {
return nil, err
}
usages := activity.GetValues()
if len(usages) > int(opts.Time) {
usages = usages[len(usages)-int(opts.Time):]
}
var results []usage.MonitorValue
for _, u := range usages {
v := u.Out
if opts.Item == "in" {
v = u.In
}
if v > 0 {
v = v / 1000 / 1000 // 単位変換: bps->Mbps
}
results = append(results, usage.MonitorValue{Time: u.Time, Value: v})
}
return results, nil
}