@@ -11,14 +11,22 @@ import (
11
11
"sort"
12
12
"time"
13
13
14
+ "github.com/google/uuid"
15
+ "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
14
16
"go.uber.org/zap"
17
+ "go.uber.org/zap/zapcore"
15
18
16
19
"github.com/e2b-dev/infra/packages/orchestrator/cmd/clean-nfs-cache/pkg"
17
20
"github.com/e2b-dev/infra/packages/shared/pkg/env"
18
21
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
22
+ "github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
19
23
)
20
24
21
- const serviceName = "clean-nfs-cache"
25
+ const (
26
+ serviceName = "clean-nfs-cache"
27
+ commitSHA = ""
28
+ serviceVersion = "0.1.0"
29
+ )
22
30
23
31
func main () {
24
32
ctx := context .Background ()
@@ -29,11 +37,25 @@ func main() {
29
37
}
30
38
31
39
func cleanNFSCache (ctx context.Context ) error {
40
+ path , opts , err := parseArgs ()
41
+ if err != nil {
42
+ return fmt .Errorf ("invalid arguments: %w" , err )
43
+ }
44
+
45
+ var cores []zapcore.Core
46
+ if opts .otelCollectorEndpoint != "" {
47
+ otelCore , err := newOtelCore (ctx , opts )
48
+ if err != nil {
49
+ return fmt .Errorf ("failed to create otel logger: %w" , err )
50
+ }
51
+ cores = append (cores , otelCore )
52
+ }
53
+
32
54
globalLogger := zap .Must (logger .NewLogger (ctx , logger.LoggerConfig {
33
55
ServiceName : serviceName ,
34
56
IsInternal : true ,
35
57
IsDebug : env .IsDebug (),
36
- Cores : nil ,
58
+ Cores : cores ,
37
59
EnableConsole : true ,
38
60
}))
39
61
defer func (l * zap.Logger ) {
@@ -44,11 +66,6 @@ func cleanNFSCache(ctx context.Context) error {
44
66
}(globalLogger )
45
67
zap .ReplaceGlobals (globalLogger )
46
68
47
- path , opts , err := parseArgs ()
48
- if err != nil {
49
- return fmt .Errorf ("invalid arguments: %w" , err )
50
- }
51
-
52
69
// get free space information for path
53
70
zap .L ().Info ("starting" ,
54
71
zap .Bool ("dry_run" , opts .dryRun ),
@@ -64,10 +81,6 @@ func cleanNFSCache(ctx context.Context) error {
64
81
}
65
82
targetDiskUsage := int64 (float64 (opts .targetDiskUsagePercent ) / 100 * float64 (diskInfo .Total ))
66
83
areWeDone := func () bool {
67
- currentUsedPercentage := (float64 (diskInfo .Used ) / float64 (diskInfo .Total )) * 100
68
- zap .L ().Info ("current usage" ,
69
- zap .Float64 ("percent" , currentUsedPercentage ),
70
- zap .String ("size" , formatBytes (diskInfo .Used )))
71
84
return diskInfo .Used < targetDiskUsage
72
85
}
73
86
@@ -102,7 +115,7 @@ func cleanNFSCache(ctx context.Context) error {
102
115
zap .Int64 ("count" , results .deletedFiles ),
103
116
zap .Int64 ("bytes" , results .deletedBytes ))
104
117
})
105
- allResults = allResults .union (results )
118
+ allResults = allResults .sum (results )
106
119
if err != nil {
107
120
return fmt .Errorf ("failed to delete files: %w" , err )
108
121
}
@@ -111,6 +124,27 @@ func cleanNFSCache(ctx context.Context) error {
111
124
return nil
112
125
}
113
126
127
+ func newOtelCore (ctx context.Context , opts opts ) (zapcore.Core , error ) {
128
+ nodeID := env .GetNodeID ()
129
+ serviceInstanceID := uuid .NewString ()
130
+
131
+ resource , err := telemetry .GetResource (ctx , nodeID , serviceName , commitSHA , serviceVersion , serviceInstanceID )
132
+ if err != nil {
133
+ return nil , fmt .Errorf ("failed to create resource: %w" , err )
134
+ }
135
+
136
+ logsExporter , err := telemetry .NewLogExporter (ctx ,
137
+ otlploggrpc .WithEndpoint (opts .otelCollectorEndpoint ),
138
+ )
139
+ if err != nil {
140
+ return nil , fmt .Errorf ("failed to create logs exporter: %w" , err )
141
+ }
142
+
143
+ loggerProvider := telemetry .NewLogProvider (ctx , logsExporter , resource )
144
+ otelCore := logger .GetOTELCore (loggerProvider , serviceName )
145
+ return otelCore , nil
146
+ }
147
+
114
148
func printSummary (r results , opts opts ) {
115
149
if r .deletedFiles == 0 {
116
150
zap .L ().Info ("no files deleted" )
@@ -185,7 +219,7 @@ type results struct {
185
219
createdDurations []time.Duration
186
220
}
187
221
188
- func (r results ) union (other results ) results {
222
+ func (r results ) sum (other results ) results {
189
223
return results {
190
224
deletedFiles : r .deletedFiles + other .deletedFiles ,
191
225
deletedBytes : r .deletedBytes + other .deletedBytes ,
@@ -197,17 +231,8 @@ func (r results) union(other results) results {
197
231
func deleteOldestFiles (cache * pkg.ListingCache , files []pkg.File , opts opts , diskInfo * pkg.DiskInfo , areWeDone func () bool , deleteCount int64 ) (results , error ) {
198
232
now := time .Now ()
199
233
var results results
200
- for index , file := range files {
201
- if opts .dryRun {
202
- zap .L ().Debug ("would delete" ,
203
- zap .String ("path" , file .Path ),
204
- zap .Int64 ("bytes" , file .Size ),
205
- zap .Duration ("last_access" , time .Since (file .ATime ).Round (time .Minute )))
206
- } else {
207
- zap .L ().Debug ("deleting" ,
208
- zap .Int ("index" , index + 1 ),
209
- zap .String ("path" , file .Path ),
210
- zap .Int64 ("bytes" , file .Size ))
234
+ for _ , file := range files {
235
+ if ! opts .dryRun {
211
236
if err := os .Remove (file .Path ); err != nil {
212
237
zap .L ().Error ("failed to delete" ,
213
238
zap .String ("path" , file .Path ),
@@ -291,6 +316,7 @@ type opts struct {
291
316
dryRun bool
292
317
filesPerLoop int
293
318
filesToDeletePerLoop int64
319
+ otelCollectorEndpoint string
294
320
}
295
321
296
322
var (
@@ -306,6 +332,7 @@ func parseArgs() (string, opts, error) {
306
332
flags .BoolVar (& opts .dryRun , "dry-run" , true , "dry run" )
307
333
flags .IntVar (& opts .filesPerLoop , "files-per-loop" , 10000 , "number of files to gather metadata for per loop" )
308
334
flags .Int64Var (& opts .filesToDeletePerLoop , "deletions-per-loop" , 100 , "maximum number of files to delete per loop" )
335
+ flags .StringVar (& opts .otelCollectorEndpoint , "otel-collector-endpoint" , "" , "endpoint of the otel collector" )
309
336
310
337
args := os .Args [1 :] // skip the command name
311
338
if err := flags .Parse (args ); err != nil {
@@ -327,17 +354,3 @@ func timeit(message string, fn func()) {
327
354
328
355
zap .L ().Debug (message , zap .Duration ("duration" , done ))
329
356
}
330
-
331
- func formatBytes (b int64 ) string {
332
- const unit = 1024
333
- if b < unit {
334
- return fmt .Sprintf ("%d B" , b )
335
- }
336
- div , exp := int64 (unit ), 0
337
- for n := b / unit ; n >= unit ; n /= unit {
338
- div *= unit
339
- exp ++
340
- }
341
- return fmt .Sprintf ("%.1f %ciB" ,
342
- float64 (b )/ float64 (div ), "KMGTPE" [exp ])
343
- }
0 commit comments