Skip to content

Commit c7ddc0a

Browse files
committed
cleanup
1 parent ba974c6 commit c7ddc0a

File tree

6 files changed

+154
-264
lines changed

6 files changed

+154
-264
lines changed

cmd/go-cache-plugin/commands.go

Lines changed: 28 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
package main
55

66
import (
7-
"bytes"
87
"context"
9-
"encoding/json"
108
"errors"
119
"fmt"
12-
"github.com/grafana/go-cache-plugin/lib/otel"
1310
"io"
1411
"log"
1512
"net"
@@ -21,14 +18,15 @@ import (
2118
"syscall"
2219
"time"
2320

21+
"github.com/grafana/go-cache-plugin/lib/otel"
22+
2423
"github.com/creachadair/command"
2524
"github.com/creachadair/gocache"
2625
"github.com/creachadair/taskgroup"
2726
)
2827

2928
var flags struct {
3029
CacheDir string `flag:"cache-dir,default=$GOCACHE_DIR,Local cache directory (required)"`
31-
LogFile string `flag:"log-file,default=trace.log,File used for logs"`
3230
S3Bucket string `flag:"bucket,default=$GOCACHE_S3_BUCKET,S3 bucket name (required if no --local flag provided)"`
3331
S3Region string `flag:"region,default=$GOCACHE_S3_REGION,S3 region"`
3432
S3Endpoint string `flag:"s3-endpoint-url,default=$GOCACHE_S3_ENDPOINT_URL,S3 custom endpoint URL (if unset, use AWS default)"`
@@ -44,6 +42,7 @@ var flags struct {
4442
DebugLog int `flag:"debug,default=$GOCACHE_DEBUG,Enable detailed per-request debug logging (noisy)"`
4543
TracingEnabled bool `flag:"tracing,default=$ENABLE_TRACING,Enable tracing (optional)"`
4644
OtelCollectorAddress string `flag:"otel-collector,default=$OTEL_COLLECTOR_ADDRESS,OTEL collector address (optional)"`
45+
LogFile string `flag:"log-file,default=trace.log,File used for logs"`
4746
TraceId string `flag:"traceId,default=$TRACING_TRACE_ID,Trace Id (optional)"`
4847
ParentSpanId string `flag:"parentSpanId,default=$TRACING_PARENT_SPAN_ID,Parent Span Id (optional)"`
4948
RunId string `flag:"runId,default=$RUN_ID,Run ID (optional)"`
@@ -79,7 +78,7 @@ var serveFlags struct {
7978
Plugin string `flag:"plugin,default=$GOCACHE_PLUGIN,Plugin service addr (or port) (required)"`
8079
HTTP string `flag:"http,default=$GOCACHE_HTTP,HTTP service address ([host]:port)"`
8180
ModProxy bool `flag:"modproxy,default=$GOCACHE_MODPROXY,Enable a Go module proxy (requires --http)"`
82-
ModNoCache bool `flag:"mod-nocache,default=$GOCACHE_MODPROXY_NOCACHE,Disable the local module cache (requires --modproxy)"`
81+
ModNoCache bool `flag:"mod-nocache,default=$GOCACHE_MODPROXY_NOCACHE,Disable the module cache (requires --modproxy)"`
8382
RevProxy string `flag:"revproxy,default=$GOCACHE_REVPROXY,Reverse proxy these hosts (comma-separated; requires --http)"`
8483
SumDB string `flag:"sumdb,default=$GOCACHE_SUMDB,SumDB servers to proxy for (comma-separated)"`
8584
}
@@ -145,9 +144,6 @@ func runServe(env *command.Env) error {
145144
if err != nil {
146145
return fmt.Errorf("tracing: %w", err)
147146
}
148-
if err != nil {
149-
return fmt.Errorf("tracing: %w", err)
150-
}
151147

152148
srv := &http.Server{
153149
Addr: serveFlags.HTTP,
@@ -157,9 +153,9 @@ func runServe(env *command.Env) error {
157153
vprintf("HTTP server listening at %q", serveFlags.HTTP)
158154
g.Run(func() {
159155
<-ctx.Done()
160-
_ = otelCleanup(ctx)
156+
otelCleanup(ctx)
161157
vprintf("stopping HTTP service")
162-
_ = srv.Shutdown(context.Background())
158+
srv.Shutdown(context.Background())
163159
})
164160
}
165161

@@ -237,42 +233,44 @@ func initTracing(ctx context.Context, service string) (func(context.Context) err
237233
return func(context.Context) error { return nil }, func([]byte) {}, nil
238234
}
239235

240-
var shutdown func(context.Context) error
241-
var err error
242-
if flags.OtelCollectorAddress != "" {
243-
shutdown, err = otel.SetupOtelTraceProvider(ctx, service, flags.OtelCollectorAddress)
244-
} else if flags.LogFile != "" {
245-
log.Printf("Otel Collector address not specified, starting with the logging reporter, log file: %s", flags.LogFile)
246-
shutdown, err = otel.SetupLoggingProvider(ctx, service, flags.LogFile)
247-
} else {
248-
log.Printf("please specify either --otel-collector or --log-file to setup tracing or disable tracing")
249-
return nil, nil, errors.New("otel exporter not initialized")
250-
}
236+
tracingContext, err := initTracingContext()
251237
if err != nil {
252238
return nil, nil, err
253239
}
254240

255-
tracingContext, err := initTracingContext()
241+
shutdown, err := initTracingProvider(ctx, service)
256242
if err != nil {
257243
return nil, nil, err
258244
}
259-
spanner := otel.NewAwesomeSpanner(tracingContext)
245+
246+
spanner := otel.NewGoCacheSpanner(tracingContext)
260247

261248
spanReporter := func(buffer []byte) {
262-
id, err := parseId(buffer[:])
263-
if err != nil {
264-
return
265-
}
266-
spanner.ProcessId(ctx, id)
249+
_ = spanner.ProcessCacheRequest(ctx, buffer)
267250
}
268251

269252
return shutdown, spanReporter, err
270253
}
254+
271255
func initModTracing(ctx context.Context, service string) (func(context.Context) error, *otel.TracingContext, error) {
272256
if !flags.TracingEnabled {
273257
return func(context.Context) error { return nil }, nil, nil
274258
}
275259

260+
tracingContext, err := initTracingContext()
261+
if err != nil {
262+
return nil, nil, err
263+
}
264+
265+
shutdown, err := initTracingProvider(ctx, service)
266+
if err != nil {
267+
return nil, nil, err
268+
}
269+
270+
return shutdown, tracingContext, err
271+
}
272+
273+
func initTracingProvider(ctx context.Context, service string) (func(context.Context) error, error) {
276274
var shutdown func(context.Context) error
277275
var err error
278276
if flags.OtelCollectorAddress != "" {
@@ -282,15 +280,9 @@ func initModTracing(ctx context.Context, service string) (func(context.Context)
282280
shutdown, err = otel.SetupLoggingProvider(ctx, service, flags.LogFile)
283281
} else {
284282
log.Printf("please specify either --otel-collector or --log-file to setup tracing or disable tracing")
285-
return nil, nil, errors.New("otel exporter not initialized")
283+
return nil, errors.New("otel exporter not initialized")
286284
}
287-
288-
tracingContext, err := initTracingContext()
289-
if err != nil {
290-
return nil, nil, err
291-
}
292-
293-
return shutdown, tracingContext, err
285+
return shutdown, err
294286
}
295287

296288
func initTracingContext() (*otel.TracingContext, error) {
@@ -326,62 +318,3 @@ func copy(w io.Writer, r io.Reader, reportTrace func([]byte)) error {
326318
}
327319
}
328320
}
329-
330-
type name struct {
331-
}
332-
333-
func parseId(buf []byte) (otel.CacheRequest, error) {
334-
var data map[string]any
335-
336-
err := json.Unmarshal(buf, &data)
337-
if err != nil {
338-
slice, _, found := bytes.Cut(buf, []byte{'\n'})
339-
if found {
340-
err2 := json.Unmarshal(slice, &data)
341-
if err2 != nil {
342-
return otel.CacheRequest{}, err
343-
}
344-
}
345-
}
346-
347-
var id string
348-
var miss bool
349-
var command string
350-
var actionId string
351-
352-
id1, ok := data["ID"]
353-
if !ok {
354-
return otel.CacheRequest{}, errors.New("id not found in the request")
355-
} else {
356-
id = fmt.Sprint(id1)
357-
}
358-
359-
command1, ok := data["Command"]
360-
if !ok {
361-
command = ""
362-
} else {
363-
command = fmt.Sprint(command1)
364-
}
365-
366-
_, ok = data["Miss"]
367-
if !ok {
368-
miss = false
369-
} else {
370-
miss = true
371-
}
372-
373-
actionId1, ok := data["ActionID"]
374-
if !ok {
375-
actionId = ""
376-
} else {
377-
actionId = fmt.Sprint(actionId1)
378-
}
379-
380-
data2 := otel.CacheRequest{
381-
Id: id,
382-
ActionId: actionId,
383-
Miss: miss,
384-
Command: command,
385-
}
386-
return data2, nil
387-
}

cmd/go-cache-plugin/setup.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
"errors"
1212
"expvar"
1313
"fmt"
14-
"github.com/grafana/go-cache-plugin/lib/otel"
15-
"log"
1614
"net/http"
1715
"os"
1816
"path"
1917
"path/filepath"
2018
"strings"
2119
"time"
2220

21+
"github.com/grafana/go-cache-plugin/lib/otel"
22+
2323
"github.com/aws/aws-sdk-go-v2/aws"
2424
"github.com/aws/aws-sdk-go-v2/config"
2525
"github.com/aws/aws-sdk-go-v2/service/s3"
@@ -140,7 +140,7 @@ func initModProxy(env *command.Env, s3c *s3util.Client) (_ http.Handler, cleanup
140140
return nil, nil, env.Usagef("you must set --http to enable --modproxy")
141141
}
142142

143-
if s3c == nil && !flags.LocalCache {
143+
if s3c == nil && !serveFlags.ModNoCache {
144144
return nil, nil, errors.New("s3 client not configured")
145145
}
146146

@@ -333,7 +333,6 @@ func makeHandler(modProxy, revProxy http.Handler, tracingContext *otel.TracingCo
333333
defer span.End()
334334
}
335335

336-
log.Printf("proxying %s %s", r.Method, r.URL.Path)
337336
modProxy.ServeHTTP(w, r)
338337
return
339338
}

lib/gobuild/noop.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/modproxy/noop.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)