-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from linkernetworks/phstsai/prometheus-client
DEV: Phstsai/prometheus client
- Loading branch information
Showing
16 changed files
with
205 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters