Skip to content

Commit

Permalink
shared(api): context infrastructure plumbing for shared packages (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno authored Jan 24, 2025
2 parents 98996f2 + c81f702 commit 9fea81c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
8 changes: 6 additions & 2 deletions packages/api/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -121,6 +122,9 @@ func NewGinServer(apiStore *handlers.APIStore, swagger *openapi3.T, port int) *h
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

fmt.Println("Initializing...")

port := flag.Int("port", defaultPort, "Port for test HTTP server")
Expand All @@ -139,8 +143,8 @@ func main() {
}

if !env.IsLocal() {
shutdown := telemetry.InitOTLPExporter(serviceName, swagger.Info.Version)
defer shutdown()
shutdown := telemetry.InitOTLPExporter(ctx, serviceName, swagger.Info.Version)
defer shutdown(context.TODO())
}

// Create an instance of our handler which satisfies the generated interface
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/cmd/mock-snapshot/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func mockSnapshot(

fmt.Println("Snapshotting sandbox")

snapshot, err := sbx.Snapshot(ctx, snapshotTemplateFiles, func() {})
snapshot, err := sbx.Snapshot(ctx, otel.Tracer("orchestrator-mock"), snapshotTemplateFiles, func() {})
if err != nil {
return fmt.Errorf("failed to snapshot sandbox: %w", err)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/orchestrator/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand All @@ -14,14 +15,16 @@ import (
const defaultPort = 5008

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

port := flag.Int("port", defaultPort, "orchestrator server port")

flag.Parse()

if !env.IsLocal() {
shutdown := telemetry.InitOTLPExporter(server.ServiceName, "no")

defer shutdown()
shutdown := telemetry.InitOTLPExporter(ctx, server.ServiceName, "no")
defer shutdown(context.TODO())
}

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/pkg/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func NewClient() (*DB, error) {
return &DB{Client: client}, nil
}

func (db *DB) Close() {
_ = db.Client.Close()
func (db *DB) Close() error {
return db.Client.Close()
}
18 changes: 7 additions & 11 deletions packages/shared/pkg/telemetry/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type client struct {
}

// InitOTLPExporter initializes an OTLP exporter, and configures the corresponding trace providers.
func InitOTLPExporter(serviceName, serviceVersion string) func() {
func InitOTLPExporter(ctx context.Context, serviceName, serviceVersion string) func(ctx context.Context) error {
attributes := []attribute.KeyValue{
semconv.ServiceName(serviceName),
semconv.ServiceVersion(serviceVersion),
Expand All @@ -46,8 +46,6 @@ func InitOTLPExporter(serviceName, serviceVersion string) func() {
attributes = append(attributes, semconv.HostName(hostname))
}

ctx := context.Background()

res, err := resource.New(
ctx,
resource.WithSchemaURL(semconv.SchemaURL),
Expand Down Expand Up @@ -126,23 +124,21 @@ func InitOTLPExporter(serviceName, serviceVersion string) func() {
}()

// Shutdown will flush any remaining spans and shut down the exporter.
return func() {
otelClient.close()
}
return otelClient.close
}

func (c *client) close() {
ctx := context.Background()

func (c *client) close(ctx context.Context) error {
if c.tracerProvider != nil {
if err := c.tracerProvider.Shutdown(ctx); err != nil {
log.Fatal(err)
return err
}
}

if c.meterProvider != nil {
if err := c.meterProvider.Shutdown(ctx); err != nil {
log.Fatal(err)
return err
}
}

return nil
}
8 changes: 6 additions & 2 deletions packages/template-manager/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand All @@ -19,6 +20,9 @@ const (
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

testFlag := flag.String("test", "", "run tests")
templateID := flag.String("template", "", "template id")
buildID := flag.String("build", "", "build id")
Expand All @@ -41,8 +45,8 @@ func main() {
}

if !env.IsLocal() {
shutdown := telemetry.InitOTLPExporter(constants.ServiceName, "no")
defer shutdown()
shutdown := telemetry.InitOTLPExporter(ctx, constants.ServiceName, "no")
defer shutdown(context.TODO())
}

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
Expand Down

0 comments on commit 9fea81c

Please sign in to comment.