Skip to content

Commit

Permalink
[Go] make googlecloud.Init conform (#348)
Browse files Browse the repository at this point in the history
Change the signature of googlecloud.Init to make it like the other
plugins.
  • Loading branch information
jba authored Jun 7, 2024
1 parent b7be7fb commit e9f5a97
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions go/plugins/googlecloud/googlecloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package googlecloud

import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"time"
Expand All @@ -35,7 +37,10 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

type Options struct {
// Config provides configuration options for the Init function.
type Config struct {
// ID of the project to use. Required.
ProjectID string
// Export to Google Cloud even in the dev environment.
ForceExport bool

Expand All @@ -50,25 +55,31 @@ type Options struct {

// Init initializes all telemetry in this package.
// In the dev environment, this does nothing unless [Options.ForceExport] is true.
func Init(ctx context.Context, projectID string, opts *Options) error {
if opts == nil {
opts = &Options{}
func Init(ctx context.Context, cfg Config) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("googlecloud.Init: %w", err)
}
}()

if cfg.ProjectID == "" {
return errors.New("config missing ProjectID")
}
shouldExport := opts.ForceExport || os.Getenv("GENKIT_ENV") != "dev"
shouldExport := cfg.ForceExport || os.Getenv("GENKIT_ENV") != "dev"
if !shouldExport {
return nil
}
// Add a SpanProcessor for tracing.
texp, err := texporter.New(texporter.WithProjectID(projectID))
texp, err := texporter.New(texporter.WithProjectID(cfg.ProjectID))
if err != nil {
return err
}
aexp := &adjustingTraceExporter{texp}
core.RegisterSpanProcessor(sdktrace.NewBatchSpanProcessor(aexp))
if err := setMeterProvider(projectID, opts.MetricInterval); err != nil {
if err := setMeterProvider(cfg.ProjectID, cfg.MetricInterval); err != nil {
return err
}
return setLogHandler(projectID, opts.LogLevel)
return setLogHandler(cfg.ProjectID, cfg.LogLevel)
}

func setMeterProvider(projectID string, interval time.Duration) error {
Expand Down

0 comments on commit e9f5a97

Please sign in to comment.