• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tensorchord / openmodelz / 6180409240

14 Sep 2023 03:02AM UTC coverage: 26.069% (-3.9%) from 29.927%
6180409240

push

github

web-flow
feat: Support BYOC (#171)

Signed-off-by: xieydd <xieydd@gmail.com>

443 of 443 new or added lines in 14 files covered. (100.0%)

939 of 3602 relevant lines covered (26.07%)

1.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/agent/pkg/server/server_factory.go
1
package server
2

3
import (
4
        "context"
5
        "net/http"
6

7
        "github.com/dgraph-io/ristretto"
8
        "github.com/gin-contrib/cors"
9
        "github.com/gin-gonic/gin"
10
        "github.com/jackc/pgx/v4/pgxpool"
11
        "github.com/pkg/errors"
12
        "github.com/sirupsen/logrus"
13
        "github.com/tensorchord/openmodelz/agent/client"
14
        "github.com/tensorchord/openmodelz/agent/pkg/query"
15
        ginlogrus "github.com/toorop/gin-logrus"
16

17
        "github.com/tensorchord/openmodelz/agent/pkg/config"
18
        "github.com/tensorchord/openmodelz/agent/pkg/event"
19
        "github.com/tensorchord/openmodelz/agent/pkg/k8s"
20
        "github.com/tensorchord/openmodelz/agent/pkg/log"
21
        "github.com/tensorchord/openmodelz/agent/pkg/metrics"
22
        "github.com/tensorchord/openmodelz/agent/pkg/prom"
23
        "github.com/tensorchord/openmodelz/agent/pkg/runtime"
24
        "github.com/tensorchord/openmodelz/agent/pkg/scaling"
25
        "github.com/tensorchord/openmodelz/agent/pkg/server/validator"
26
)
27

28
type Server struct {
29
        router        *gin.Engine
30
        metricsRouter *gin.Engine
31
        logger        *logrus.Entry
32
        validator     *validator.Validator
33

34
        runtime runtime.Runtime
35

36
        // endpointResolver resolves the requests from the client to the
37
        // corresponding inference kubernetes service.
38
        endpointResolver       k8s.Resolver
39
        buildLogRequester      log.Requester
40
        deploymentLogRequester log.Requester
41

42
        // prometheusClient is the client to query the prometheus server.
43
        // It is used in inference list.
44
        prometheusClient prom.PrometheusQuery
45
        metricsOptions   metrics.MetricOptions
46

47
        // scaler scales the inference from 0 to 1.
48
        scaler *scaling.InferenceScaler
49

50
        config config.Config
51

52
        eventRecorder event.Interface
53

54
        modelzCloudClient *client.Client
55

56
        cache ristretto.Cache
57
}
58

59
func New(c config.Config) (Server, error) {
×
60
        router := gin.New()
×
61
        router.Use(ginlogrus.Logger(logrus.StandardLogger(), "/healthz"))
×
62
        router.Use(gin.Recovery())
×
63

×
64
        // metrics server
×
65
        metricsRouter := gin.New()
×
66
        metricsRouter.Use(gin.Recovery())
×
67

×
68
        if gin.Mode() == gin.DebugMode {
×
69
                logrus.SetLevel(logrus.DebugLevel)
×
70
                logrus.Debug("Allow CORS")
×
71
                router.Use(cors.New(cors.Config{
×
72
                        AllowOrigins: []string{"*"},
×
73
                        AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
×
74
                        AllowHeaders: []string{"*"},
×
75
                }))
×
76
        }
×
77

78
        promCli := prom.NewPrometheusQuery(c.Metrics.PrometheusHost, c.Metrics.PrometheusPort, http.DefaultClient)
×
79

×
80
        logger := logrus.WithField("component", "server")
×
81

×
82
        s := Server{
×
83
                router:           router,
×
84
                metricsRouter:    metricsRouter,
×
85
                config:           c,
×
86
                logger:           logger,
×
87
                validator:        validator.New(),
×
88
                prometheusClient: promCli,
×
89
        }
×
90

×
91
        cache, err := ristretto.NewCache(&ristretto.Config{
×
92
                NumCounters: 1e7,
×
93
                MaxCost:     1 << 28,
×
94
                BufferItems: 64,
×
95
        })
×
96
        if err != nil {
×
97
                return s, err
×
98
        }
×
99
        s.cache = *cache
×
100

×
101
        if s.config.DB.EventEnabled {
×
102
                logrus.Info("Event recording is enabled")
×
103
                // Connect to database
×
104
                conn, err := pgxpool.Connect(context.Background(), c.DB.URL)
×
105
                if err != nil {
×
106
                        return s, errors.Wrap(err, "failed to connect to database")
×
107
                }
×
108
                queries := query.New(conn)
×
109
                s.eventRecorder = event.NewEventRecorder(queries)
×
110
        } else {
×
111
                s.eventRecorder = event.NewFake()
×
112
        }
×
113

114
        s.registerRoutes()
×
115
        s.registerMetricsRoutes()
×
116
        if err := s.initKubernetesResources(); err != nil {
×
117
                return s, err
×
118
        }
×
119

120
        if c.ModelZCloud.Enabled {
×
121
                err := s.initModelZCloud(c.ModelZCloud.URL, c.ModelZCloud.AgentToken, c.ModelZCloud.Region)
×
122
                if err != nil {
×
123
                        return s, err
×
124
                }
×
125
        }
126
        if err := s.initMetrics(); err != nil {
×
127
                return s, err
×
128
        }
×
129
        s.initLogs()
×
130
        return s, nil
×
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc