Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Phstsai/prometheus client #29

Merged
merged 22 commits into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ install:
- make pre-build

script:
- docker run --name prometheus -d -p 9090:9090 prom/prometheus
- make build
- sudo -E PATH=$PATH TEST_GRPC=1 make src.test-coverage
- docker build --tag sdnvortex/vortex --file ./dockerfiles/Dockerfile .
Expand All @@ -36,5 +37,6 @@ deploy:
branch: develop

after_success:
- docker rm -f prometheus
- cp build/src/coverage.txt coverage.txt
- bash <(curl -s https://codecov.io/bash)
5 changes: 4 additions & 1 deletion config/k8s.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"url": "mongodb://35.189.177.37:31717/votex"
}
},
"prometheus": {
"url": "http://prometheus.monitoring.svc.cluster.local:30003"
},
"logger": {
"dir": "./logs",
"level": "debug",
Expand All @@ -37,4 +40,4 @@
"version": "<GIT_COMMIT>",
"logFileName": "access_log"
}
}
}
5 changes: 4 additions & 1 deletion config/local.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"mongo": {
"url": "mongodb://localhost:27017/vortex_test"
},
"prometheus": {
"url": "http://localhost:30003"
},
"logger": {
"dir": "./logs",
"level": "debug",
Expand All @@ -26,4 +29,4 @@
"version": "local",
"logFileName": "access_log"
}
}
}
5 changes: 4 additions & 1 deletion config/testing.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"mongo": {
"url": "mongodb://localhost:27017/vortex_test"
},
"prometheus": {
"url": "http://localhost:9090"
},
"logger": {
"dir": "./logs",
"level": "debug",
Expand All @@ -26,4 +29,4 @@
"version": "local",
"logFileName": "access_log"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ spec:
ports:
- name: http-metrics
containerPort: 8080
- name: telemetry
containerPort: 8081
readinessProbe:
httpGet:
path: /healthz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ spec:
port: 8080
targetPort: http-metrics
protocol: TCP
- name: telemetry
port: 8081
targetPort: telemetry
protocol: TCP
selector:
k8s-app: kube-state-metrics

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ data:
scrape_timeout: 15s
scrape_configs:


- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']


- job_name: 'kubernetes-service-endpoints'
kubernetes_sd_configs:
- role: endpoints
Expand Down
2 changes: 0 additions & 2 deletions deploy/kubernetes/service/mongodb/service-external.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
labels:
environment: testing
name: mongo-external
namespace: default
spec:
externalTrafficPolicy: Cluster
ports:
Expand All @@ -15,7 +14,6 @@ spec:
protocol: TCP
targetPort: 27017
selector:
podindex: "0"
service: mongo
sessionAffinity: None
type: NodePort
1 change: 1 addition & 0 deletions jenkins/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ echo 'jenkins:x:'$UID':'$GID':,,,:/home/jenkins:/bin/bash' >> /etc/passwd
sudo service docker start
mkdir -p /home/jenkins/data/mongo
nohup mongod --dbpath=/home/jenkins/data/mongo 2>&1 > /dev/null &
docker run --name prometheus -d -p 9090:9090 prom/prometheus

sudo chown -R jenkins:jenkins /home/jenkins

Expand Down
8 changes: 5 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"github.com/linkernetworks/logger"
"github.com/linkernetworks/mongo"
"github.com/linkernetworks/redis"
"github.com/linkernetworks/vortex/src/prometheus"
)

type Config struct {
Redis *redis.RedisConfig `json:"redis"`
Mongo *mongo.MongoConfig `json:"mongo"`
Logger logger.LoggerConfig `json:"logger"`
Redis *redis.RedisConfig `json:"redis"`
Mongo *mongo.MongoConfig `json:"mongo"`
Prometheus *prometheus.PrometheusConfig `json:"prometheus"`
Logger logger.LoggerConfig `json:"logger"`

// the version settings of the current application
Version string `json:"version"`
Expand Down
36 changes: 36 additions & 0 deletions src/prometheus/promehteus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package prometheus

import (
"github.com/linkernetworks/logger"
"github.com/prometheus/client_golang/api"
prometheus "github.com/prometheus/client_golang/api/prometheus/v1"
)

type PrometheusConfig struct {
Url string `json:"url"`
}

type Service struct {
Url string
API prometheus.API
}

func New(url string) *Service {
conf := api.Config{
Address: url,
RoundTripper: api.DefaultRoundTripper,
}

client, err := api.NewClient(conf)
if err != nil {
// TODO should return error to server
logger.Warnf("error while creating api.NewClient %s", err)
}

newAPI := prometheus.NewAPI(client)

return &Service{
Url: url,
API: newAPI,
}
}
38 changes: 38 additions & 0 deletions src/server/handler_prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server

import (
"fmt"
"net/http"
"time"

restful "github.com/emicklei/go-restful"
response "github.com/linkernetworks/vortex/src/net/http"
"github.com/linkernetworks/vortex/src/net/http/query"
"github.com/linkernetworks/vortex/src/web"
"golang.org/x/net/context"
)

func queryMetrics(ctx *web.Context) {
sp, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response

query := query.New(req.Request.URL.Query())

query_str := ""
if q, ok := query.Str("query"); ok {
query_str = q
}

api := sp.Prometheus.API

testTime := time.Now()
result, err := api.Query(context.Background(), query_str, testTime)

if result == nil {
response.BadRequest(req.Request, resp.ResponseWriter, fmt.Errorf("%v: %v", result, err))
}

resp.WriteJson(map[string]interface{}{
"status": http.StatusOK,
"results": result,
}, restful.MIME_JSON)
}
60 changes: 60 additions & 0 deletions src/server/handler_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package server

import (
"net/http"
"net/http/httptest"
"testing"

restful "github.com/emicklei/go-restful"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/serviceprovider"
prometheus "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type PrometheusTestSuite struct {
suite.Suite
wc *restful.Container
api prometheus.API
}

func (suite *PrometheusTestSuite) SetupSuite() {
cf := config.MustRead("../../config/testing.json")
sp := serviceprovider.NewForTesting(cf)

//init session
suite.api = sp.Prometheus.API

//init restful container
suite.wc = restful.NewContainer()
service := newMonitoringService(sp)
suite.wc.Add(service)
}

func TestPrometheusTestSuite(t *testing.T) {
suite.Run(t, new(PrometheusTestSuite))
}

func (suite *PrometheusTestSuite) TearDownSuite() {
}

func (suite *PrometheusTestSuite) TestQueryMetrics() {

httpRequest, err := http.NewRequest("GET", "http://localhost:7890/v1/monitoring/query?query=prometheus_build_info", nil)
assert.NoError(suite.T(), err)

httpWriter := httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusOK, httpWriter)
}

func (suite *PrometheusTestSuite) TestQueryWrongMetrics() {

httpRequest, err := http.NewRequest("GET", "http://localhost:7890/v1/monitoring/query?query=!@#$", nil)
assert.NoError(suite.T(), err)

httpWriter := httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusBadRequest, httpWriter)
}
8 changes: 8 additions & 0 deletions src/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (a *App) AppRoute() *mux.Router {
container.Add(newNetworkService(a.ServiceProvider))
container.Add(newStorageProviderService(a.ServiceProvider))
container.Add(newVolumeService(a.ServiceProvider))
container.Add(newMonitoringService(a.ServiceProvider))

router.PathPrefix("/v1/").Handler(container)
return router
Expand Down Expand Up @@ -55,3 +56,10 @@ func newVolumeService(sp *serviceprovider.Container) *restful.WebService {
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, createVolume)))
return webService
}

func newMonitoringService(sp *serviceprovider.Container) *restful.WebService {
webService := new(restful.WebService)
webService.Path("/v1/monitoring").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.GET("/query").To(handler.RESTfulServiceHandler(sp, queryMetrics)))
return webService
}
39 changes: 25 additions & 14 deletions src/serviceprovider/serviceprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package serviceprovider

import (
"fmt"
"github.com/linkernetworks/logger"
"github.com/linkernetworks/vortex/src/config"
"os"
"path/filepath"

"github.com/linkernetworks/logger"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/prometheus"

"github.com/linkernetworks/mongo"
"github.com/linkernetworks/redis"
kubeCtl "github.com/linkernetworks/vortex/src/kubernetes"
Expand All @@ -18,10 +20,11 @@ import (
)

type Container struct {
Config config.Config
Redis *redis.Service
Mongo *mongo.Service
KubeCtl *kubeCtl.KubeCtl
Config config.Config
Redis *redis.Service
Mongo *mongo.Service
Prometheus *prometheus.Service
KubeCtl *kubeCtl.KubeCtl
}

type ServiceDiscoverResponse struct {
Expand All @@ -40,6 +43,9 @@ func New(cf config.Config) *Container {
logger.Infof("Connecting to mongodb: %s", cf.Mongo.Url)
mongo := mongo.New(cf.Mongo.Url)

logger.Infof("Connecting to prometheus: %s", cf.Prometheus.Url)
prometheus := prometheus.New(cf.Prometheus.Url)

kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
k8s, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
Expand All @@ -52,10 +58,11 @@ func New(cf config.Config) *Container {
clientset := kubernetes.NewForConfigOrDie(k8s)

sp := &Container{
Config: cf,
Redis: redisService,
Mongo: mongo,
KubeCtl: kubeCtl.New(clientset, "default"),
Config: cf,
Redis: redisService,
Mongo: mongo,
Prometheus: prometheus,
KubeCtl: kubeCtl.New(clientset, "default"),
}

return sp
Expand All @@ -71,13 +78,17 @@ func NewForTesting(cf config.Config) *Container {
logger.Infof("Connecting to mongodb: %s", cf.Mongo.Url)
mongo := mongo.New(cf.Mongo.Url)

logger.Infof("Connecting to prometheus: %s", cf.Prometheus.Url)
prometheus := prometheus.New(cf.Prometheus.Url)

clientset := fakeclientset.NewSimpleClientset()

sp := &Container{
Config: cf,
Redis: redisService,
Mongo: mongo,
KubeCtl: kubeCtl.New(clientset, "default"),
Config: cf,
Redis: redisService,
Mongo: mongo,
Prometheus: prometheus,
KubeCtl: kubeCtl.New(clientset, "default"),
}

return sp
Expand Down
12 changes: 12 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,18 @@
"revision": "faf4ec335fe01ae5a6a0eaa34a5a9333bfbd1a30",
"revisionTime": "2018-06-07T12:36:07Z"
},
{
"checksumSHA1": "4VppKBbzCSmoFfQxGNUm0TYiFCA=",
"path": "github.com/prometheus/client_golang/api",
"revision": "82f5ff156b29e276022b1a958f7d385870fb9814",
"revisionTime": "2018-04-16T23:38:56Z"
},
{
"checksumSHA1": "83pGB3cRG5uqx9O5d+7MCB+TFT4=",
"path": "github.com/prometheus/client_golang/api/prometheus/v1",
"revision": "77e8f2ddcfed59ece3a8151879efb2304b5cbbcf",
"revisionTime": "2018-06-23T15:59:54Z"
},
{
"checksumSHA1": "WVgL9pNO2RZCCcaXfSYSNEPgtCo=",
"path": "github.com/prometheus/client_golang/prometheus",
Expand Down