Skip to content

Commit

Permalink
Add /api/sampling endpoint in collector
Browse files Browse the repository at this point in the history
Return metricsFactory from test server init

fix import structure

fix unwanted change in agent server

Add license on new file

Signed-off-by: RickyRajinder <singh.sangh@gmail.com>

Add tests for errors

Refactor collector API handler to extend agent handler

Remove unused fields

Unexport fields in HttpHandler

Update callers of NewAPIHandler

Run go fmt

Fix lint issues

Fix spelling error in comment

Refactor HTTP Handler to shared location

Implement clientConfigManager interface for collector

Run gofmt

Resolve prior dependencies

Rename HTTPHandler

Update comments and fix failing test in agent

Change mux version

Add empty test
  • Loading branch information
RickyRajinder committed Dec 31, 2019
1 parent 3fa8a08 commit d06b515
Show file tree
Hide file tree
Showing 11 changed files with 811 additions and 736 deletions.
15 changes: 15 additions & 0 deletions cmd/agent/app/httpserver/empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2019 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpserver
158 changes: 6 additions & 152 deletions cmd/agent/app/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,165 +16,19 @@
package httpserver

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

"github.com/uber/jaeger-lib/metrics"

"github.com/jaegertracing/jaeger/cmd/agent/app/configmanager"
tSampling "github.com/jaegertracing/jaeger/thrift-gen/sampling"
)

const mimeTypeApplicationJSON = "application/json"

var (
errBadRequest = errors.New("bad request")
h "github.com/jaegertracing/jaeger/pkg/clientcfg/http"
)

// NewHTTPServer creates a new server that hosts an HTTP/JSON endpoint for clients
// to query for sampling strategies and baggage restrictions.
func NewHTTPServer(hostPort string, manager configmanager.ClientConfigManager, mFactory metrics.Factory) *http.Server {
handler := newHTTPHandler(manager, mFactory)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler.serveSamplingHTTP(w, r, true /* thriftEnums092 */)
})
mux.HandleFunc("/sampling", func(w http.ResponseWriter, r *http.Request) {
handler.serveSamplingHTTP(w, r, false /* thriftEnums092 */)
})
mux.HandleFunc("/baggageRestrictions", func(w http.ResponseWriter, r *http.Request) {
handler.serveBaggageHTTP(w, r)
})
return &http.Server{Addr: hostPort, Handler: mux}
}

func newHTTPHandler(manager configmanager.ClientConfigManager, mFactory metrics.Factory) *httpHandler {
handler := &httpHandler{manager: manager}
metrics.Init(&handler.metrics, mFactory, nil)
return handler
}

type httpHandler struct {
manager configmanager.ClientConfigManager
metrics struct {
// Number of good sampling requests
SamplingRequestSuccess metrics.Counter `metric:"http-server.requests" tags:"type=sampling"`

// Number of good sampling requests against the old endpoint / using Thrift 0.9.2 enum codes
LegacySamplingRequestSuccess metrics.Counter `metric:"http-server.requests" tags:"type=sampling-legacy"`

// Number of good baggage requests
BaggageRequestSuccess metrics.Counter `metric:"http-server.requests" tags:"type=baggage"`

// Number of bad requests (400s)
BadRequest metrics.Counter `metric:"http-server.errors" tags:"status=4xx,source=all"`

// Number of collector proxy failures
CollectorProxyFailures metrics.Counter `metric:"http-server.errors" tags:"status=5xx,source=collector-proxy"`

// Number of bad responses due to malformed thrift
BadThriftFailures metrics.Counter `metric:"http-server.errors" tags:"status=5xx,source=thrift"`

// Number of failed response writes from http server
WriteFailures metrics.Counter `metric:"http-server.errors" tags:"status=5xx,source=write"`
}
}

func (h *httpHandler) serviceFromRequest(w http.ResponseWriter, r *http.Request) (string, error) {
services := r.URL.Query()["service"]
if len(services) != 1 {
h.metrics.BadRequest.Inc(1)
http.Error(w, "'service' parameter must be provided once", http.StatusBadRequest)
return "", errBadRequest
}
return services[0], nil
}

func (h *httpHandler) writeJSON(w http.ResponseWriter, json []byte) error {
w.Header().Add("Content-Type", mimeTypeApplicationJSON)
if _, err := w.Write(json); err != nil {
h.metrics.WriteFailures.Inc(1)
return err
}
return nil
}

func (h *httpHandler) serveSamplingHTTP(w http.ResponseWriter, r *http.Request, thriftEnums092 bool) {
service, err := h.serviceFromRequest(w, r)
if err != nil {
return
}
resp, err := h.manager.GetSamplingStrategy(service)
if err != nil {
h.metrics.CollectorProxyFailures.Inc(1)
http.Error(w, fmt.Sprintf("collector error: %+v", err), http.StatusInternalServerError)
return
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
h.metrics.BadThriftFailures.Inc(1)
http.Error(w, "Cannot marshall Thrift to JSON", http.StatusInternalServerError)
return
}
if thriftEnums092 {
jsonBytes = h.encodeThriftEnums092(jsonBytes)
}
if err = h.writeJSON(w, jsonBytes); err != nil {
return
}
if thriftEnums092 {
h.metrics.LegacySamplingRequestSuccess.Inc(1)
} else {
h.metrics.SamplingRequestSuccess.Inc(1)
}
}

func (h *httpHandler) serveBaggageHTTP(w http.ResponseWriter, r *http.Request) {
service, err := h.serviceFromRequest(w, r)
if err != nil {
return
}
resp, err := h.manager.GetBaggageRestrictions(service)
if err != nil {
h.metrics.CollectorProxyFailures.Inc(1)
http.Error(w, fmt.Sprintf("collector error: %+v", err), http.StatusInternalServerError)
return
}
// NB. it's literally impossible for this Marshal to fail
jsonBytes, _ := json.Marshal(resp)
if err = h.writeJSON(w, jsonBytes); err != nil {
return
}
h.metrics.BaggageRequestSuccess.Inc(1)
}

var samplingStrategyTypes = []tSampling.SamplingStrategyType{
tSampling.SamplingStrategyType_PROBABILISTIC,
tSampling.SamplingStrategyType_RATE_LIMITING,
}

// Replace string enum values produced from Thrift 0.9.3 generated classes
// with integer codes produced from Thrift 0.9.2 generated classes.
//
// For example:
//
// Thrift 0.9.2 classes generate this JSON:
// {"strategyType":0,"probabilisticSampling":{"samplingRate":0.5},"rateLimitingSampling":null,"operationSampling":null}
//
// Thrift 0.9.3 classes generate this JSON:
// {"strategyType":"PROBABILISTIC","probabilisticSampling":{"samplingRate":0.5}}
func (h *httpHandler) encodeThriftEnums092(json []byte) []byte {
str := string(json)
for _, strategyType := range samplingStrategyTypes {
str = strings.Replace(
str,
fmt.Sprintf(`"strategyType":"%s"`, strategyType.String()),
fmt.Sprintf(`"strategyType":%d`, strategyType),
1,
)
}
return []byte(str)
func NewHTTPServer(hostPort string, configManager configmanager.ClientConfigManager, mFactory metrics.Factory) *http.Server {
handler := h.NewHTTPHandler(nil, configManager, mFactory)
r := http.NewServeMux()
handler.RegisterHandler(r)
return &http.Server{Addr: hostPort, Handler: r}
}
Loading

0 comments on commit d06b515

Please sign in to comment.