(prod bool, ca cache, aoe azureEstimator, e estimator, client http.Client, rec recorder)
| 26 | } |
| 27 | |
| 28 | func getRouteHandler(prod bool, ca cache, aoe azureEstimator, e estimator, client http.Client, rec recorder) gin.HandlerFunc { |
| 29 | return func(c *gin.Context) { |
| 30 | log := util.GetLogFromCtx(c) |
| 31 | trueStart := time.Now() |
| 32 | |
| 33 | tags := []string{ |
| 34 | fmt.Sprintf("path:%s", c.FullPath()), |
| 35 | } |
| 36 | |
| 37 | telemetry.Incr("bricksllm.proxy.get_route_handeler.requests", tags, 1) |
| 38 | if c == nil || c.Request == nil { |
| 39 | JSON(c, http.StatusInternalServerError, "[BricksLLM] context is empty") |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | raw, exists := c.Get("key") |
| 44 | kc, ok := raw.(*key.ResponseKey) |
| 45 | if !exists || !ok { |
| 46 | telemetry.Incr("bricksllm.proxy.get_route_handeler.api_key_not_registered", tags, 1) |
| 47 | JSON(c, http.StatusUnauthorized, "[BricksLLM] api key is not registered") |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | raw, exists = c.Get("route_config") |
| 52 | rc, ok := raw.(*route.Route) |
| 53 | if !exists || !ok { |
| 54 | telemetry.Incr("bricksllm.proxy.get_route_handeler.route_config_not_found", tags, 1) |
| 55 | JSON(c, http.StatusNotFound, "[BricksLLM] route config not found") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | cacheKey := c.GetString("cache_key") |
| 60 | shouldCache := len(cacheKey) != 0 |
| 61 | |
| 62 | if shouldCache { |
| 63 | bytes, err := ca.GetBytes(cacheKey) |
| 64 | |
| 65 | if err == nil && len(bytes) != 0 { |
| 66 | telemetry.Incr("bricksllm.proxy.get_route_handeler.success", nil, 1) |
| 67 | telemetry.Timing("bricksllm.proxy.get_route_handeler.success_latency", time.Since(trueStart), nil, 1) |
| 68 | |
| 69 | c.Set("provider", "cached") |
| 70 | c.Data(http.StatusOK, "application/json", bytes) |
| 71 | return |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | raw, exists = c.Get("settings") |
| 76 | settings, ok := raw.([]*provider.Setting) |
| 77 | if !exists || !ok { |
| 78 | telemetry.Incr("bricksllm.proxy.get_route_handeler.provider_settings_not_found", tags, 1) |
| 79 | JSON(c, http.StatusNotFound, "[BricksLLM] provider settings not found") |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | settingsMap := map[string]*provider.Setting{} |
| 84 | for _, setting := range settings { |
| 85 | settingsMap[setting.Id] = setting |
no test coverage detected