From c1b4e568d192ddc5548b1ea2e4894b0dfbc301dc Mon Sep 17 00:00:00 2001 From: Pratham Mishra <99235987+Pratham-Mishra04@users.noreply.github.com> Date: Mon, 12 May 2025 23:10:54 +0530 Subject: [PATCH] feat: maxim sdk plugin integration added to bifrost http transport --- transports/Dockerfile | 6 +++++- transports/bifrost-http/main.go | 37 +++++++++++++++++++++++++++++++-- transports/go.mod | 2 ++ transports/go.sum | 4 ++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/transports/Dockerfile b/transports/Dockerfile index 51e57f9012..27f726fcba 100644 --- a/transports/Dockerfile +++ b/transports/Dockerfile @@ -36,11 +36,15 @@ ARG ENV_PATH ARG PORT ARG POOL_SIZE ARG DROP_EXCESS_REQUESTS +ARG PLUGINS +ARG MAXIM_LOG_REPO_ID # Set default values if args are not provided ENV APP_PORT=${PORT:-8080} ENV APP_POOL_SIZE=${POOL_SIZE:-300} ENV APP_DROP_EXCESS_REQUESTS=${DROP_EXCESS_REQUESTS:-false} +ENV APP_PLUGINS=${PLUGINS:-""} +ENV APP_MAXIM_LOG_REPO_ID=${MAXIM_LOG_REPO_ID:-""} # Copy the config and environment files into the image COPY ${CONFIG_PATH} /app/config/config.json @@ -51,7 +55,7 @@ RUN echo '#!/bin/sh' > /app/entrypoint.sh && \ echo 'if [ ! -f /app/config/config.json ]; then echo "Missing config.json"; exit 1; fi' >> /app/entrypoint.sh && \ echo 'if [ ! -f /app/config/.env ]; then echo "Missing .env"; exit 1; fi' >> /app/entrypoint.sh && \ echo 'if [ ! -f /app/main ]; then echo "Missing main binary"; exit 1; fi' >> /app/entrypoint.sh && \ - echo 'exec /app/main -config /app/config/config.json -env /app/config/.env -port "$APP_PORT" -pool-size "$APP_POOL_SIZE" -drop-excess-requests "$APP_DROP_EXCESS_REQUESTS"' >> /app/entrypoint.sh && \ + echo 'exec /app/main -config /app/config/config.json -env /app/config/.env -port "$APP_PORT" -pool-size "$APP_POOL_SIZE" -drop-excess-requests "$APP_DROP_EXCESS_REQUESTS" -plugins "$APP_PLUGINS" -maxim-log-repo-id "$APP_MAXIM_LOG_REPO_ID"' >> /app/entrypoint.sh && \ chmod +x /app/entrypoint.sh # Expose the port defined by argument diff --git a/transports/bifrost-http/main.go b/transports/bifrost-http/main.go index c961d428c3..01983cf01f 100644 --- a/transports/bifrost-http/main.go +++ b/transports/bifrost-http/main.go @@ -22,11 +22,13 @@ import ( "flag" "fmt" "log" + "os" "strings" "github.com/fasthttp/router" bifrost "github.com/maximhq/bifrost/core" schemas "github.com/maximhq/bifrost/core/schemas" + "github.com/maximhq/bifrost/plugins" "github.com/maximhq/bifrost/transports/bifrost-http/integrations" "github.com/maximhq/bifrost/transports/bifrost-http/integrations/genai" "github.com/maximhq/bifrost/transports/bifrost-http/lib" @@ -45,6 +47,8 @@ var ( port string // Port to run the server on configPath string // Path to the config file envPath string // Path to the .env file + pluginsToLoad []string // Path to the plugins + maximLogRepoId string // ID of the Maxim log repo prometheusLabels []string // Labels to add to Prometheus metrics (optional) ) @@ -56,6 +60,7 @@ var ( // - env: Path to .env file (required) // - drop-excess-requests: Whether to drop excess requests func init() { + pluginString := "" var prometheusLabelsString string flag.IntVar(&initialPoolSize, "pool-size", 300, "Initial pool size for Bifrost") @@ -63,9 +68,13 @@ func init() { flag.StringVar(&configPath, "config", "", "Path to the config file") flag.StringVar(&envPath, "env", "", "Path to the .env file") flag.BoolVar(&dropExcessRequests, "drop-excess-requests", false, "Drop excess requests") + flag.StringVar(&pluginString, "plugins", "", "Comma separated list of plugins to load") + flag.StringVar(&maximLogRepoId, "maxim-log-repo-id", "", "ID of the Maxim log repo") flag.StringVar(&prometheusLabelsString, "prometheus-labels", "", "Labels to add to Prometheus metrics") flag.Parse() + pluginsToLoad = strings.Split(pluginString, ",") + if configPath == "" { log.Fatalf("config path is required") } @@ -136,14 +145,38 @@ func main() { log.Printf("warning: failed to read environment variables: %v", err) } - // Instantiate the Prometheus plugin + loadedPlugins := []schemas.Plugin{} + + for _, plugin := range pluginsToLoad { + switch strings.ToLower(plugin) { + case "maxim": + if maximLogRepoId == "" { + log.Println("warning: maxim log repo id is required to initialize maxim plugin") + continue + } + if os.Getenv("MAXIM_API_KEY") == "" { + log.Println("warning: maxim api key is required in environment variable MAXIM_API_KEY to initialize maxim plugin") + continue + } + + maximPlugin, err := plugins.NewMaximLoggerPlugin(os.Getenv("MAXIM_API_KEY"), maximLogRepoId) + if err != nil { + log.Printf("warning: failed to initialize maxim plugin: %v", err) + continue + } + + loadedPlugins = append(loadedPlugins, maximPlugin) + } + } + promPlugin := tracking.NewPrometheusPlugin() + loadedPlugins = append(loadedPlugins, promPlugin) client, err := bifrost.Init(schemas.BifrostConfig{ Account: account, InitialPoolSize: initialPoolSize, DropExcessRequests: dropExcessRequests, - Plugins: []schemas.Plugin{promPlugin}, + Plugins: loadedPlugins, }) if err != nil { log.Fatalf("failed to initialize bifrost: %v", err) diff --git a/transports/go.mod b/transports/go.mod index 9eb1881e36..cc34c0fd60 100644 --- a/transports/go.mod +++ b/transports/go.mod @@ -6,6 +6,7 @@ require ( github.com/fasthttp/router v1.5.4 github.com/joho/godotenv v1.5.1 github.com/maximhq/bifrost/core v1.0.5 + github.com/maximhq/bifrost/plugins v1.0.1 github.com/prometheus/client_golang v1.22.0 github.com/valyala/fasthttp v1.60.0 google.golang.org/genai v1.4.0 @@ -41,6 +42,7 @@ require ( github.com/googleapis/gax-go/v2 v2.14.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/klauspost/compress v1.18.0 // indirect + github.com/maximhq/maxim-go v0.1.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.62.0 // indirect diff --git a/transports/go.sum b/transports/go.sum index a2cfcc4f27..61a8721769 100644 --- a/transports/go.sum +++ b/transports/go.sum @@ -71,6 +71,10 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/maximhq/bifrost/core v1.0.5 h1:JopRRuV2zylwisj+cBonIpB/Dw4p8gEQoW3hrKsPyI8= github.com/maximhq/bifrost/core v1.0.5/go.mod h1:nL2tc1SVJ7EAKxUb1G1yS4nO46ZsCdm1qC3Oc78oYIU= +github.com/maximhq/bifrost/plugins v1.0.1 h1:wu+kEwdEFvnhEiNLsrFHpxVdd/dJ2Uvkk3el3DkFoWc= +github.com/maximhq/bifrost/plugins v1.0.1/go.mod h1:c5L95RCOHGAKGAl9up03/00DHqjdxVG+3MdIUlAnzYM= +github.com/maximhq/maxim-go v0.1.1 h1:69uUQjjDPmUGcKg/M4/3AO0fbD+70Agt66pH/UCsI5M= +github.com/maximhq/maxim-go v0.1.1/go.mod h1:0+UTWM7UZwNNE5VnljLtr/vpRGtYP8r/2q9WDwlLWFw= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=