Skip to content

Commit

Permalink
[refactor] Move sampling http handler to internal/sampling/http (#6545)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Towards #6411

## Description of the changes
- Move sampling http handler from pkg/clientcfg/clientcfghttp to
internal/sampling/http

## How was this change tested?
- Covered by existing

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [ ] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `npm run lint` and `npm run test`

Signed-off-by: Aryan Goyal <[email protected]>
  • Loading branch information
ary82 authored Jan 14, 2025
1 parent b759887 commit 46e7b25
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/agent/app/httpserver/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"go.uber.org/zap/zapcore"

"github.com/jaegertracing/jaeger/cmd/agent/app/configmanager"
"github.com/jaegertracing/jaeger/pkg/clientcfg/clientcfghttp"
samplinghttp "github.com/jaegertracing/jaeger/internal/sampling/http"
"github.com/jaegertracing/jaeger/pkg/metrics"
)

// NewHTTPServer creates a new server that hosts an HTTP/JSON endpoint for clients
// to query for sampling strategies.
func NewHTTPServer(hostPort string, manager configmanager.ClientConfigManager, mFactory metrics.Factory, logger *zap.Logger) *http.Server {
handler := clientcfghttp.NewHTTPHandler(clientcfghttp.HTTPHandlerParams{
handler := samplinghttp.NewHandler(samplinghttp.HandlerParams{
ConfigManager: manager,
MetricsFactory: mFactory,
LegacySamplingEndpoint: true,
Expand Down
6 changes: 3 additions & 3 deletions cmd/collector/app/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/jaegertracing/jaeger/cmd/collector/app/handler"
"github.com/jaegertracing/jaeger/cmd/collector/app/sampling/samplingstrategy"
clientcfgHandler "github.com/jaegertracing/jaeger/pkg/clientcfg/clientcfghttp"
samplinghttp "github.com/jaegertracing/jaeger/internal/sampling/http"
"github.com/jaegertracing/jaeger/pkg/healthcheck"
"github.com/jaegertracing/jaeger/pkg/httpmetrics"
"github.com/jaegertracing/jaeger/pkg/metrics"
Expand Down Expand Up @@ -59,8 +59,8 @@ func serveHTTP(server *http.Server, listener net.Listener, params *HTTPServerPar
apiHandler := handler.NewAPIHandler(params.Handler)
apiHandler.RegisterRoutes(r)

cfgHandler := clientcfgHandler.NewHTTPHandler(clientcfgHandler.HTTPHandlerParams{
ConfigManager: &clientcfgHandler.ConfigManager{
cfgHandler := samplinghttp.NewHandler(samplinghttp.HandlerParams{
ConfigManager: &samplinghttp.ConfigManager{
SamplingProvider: params.SamplingProvider,
},
MetricsFactory: params.MetricsFactory,
Expand Down
6 changes: 3 additions & 3 deletions cmd/jaeger/internal/extension/remotesampling/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage"
"github.com/jaegertracing/jaeger/internal/metrics/otelmetrics"
samplinggrpc "github.com/jaegertracing/jaeger/internal/sampling/grpc"
"github.com/jaegertracing/jaeger/pkg/clientcfg/clientcfghttp"
samplinghttp "github.com/jaegertracing/jaeger/internal/sampling/http"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/plugin/sampling/leaderelection"
"github.com/jaegertracing/jaeger/plugin/sampling/strategyprovider/adaptive"
Expand Down Expand Up @@ -229,8 +229,8 @@ func (ext *rsExtension) startAdaptiveStrategyProvider(host component.Host) error
func (ext *rsExtension) startHTTPServer(ctx context.Context, host component.Host) error {
mf := otelmetrics.NewFactory(ext.telemetry.MeterProvider)
mf = mf.Namespace(metrics.NSOptions{Name: "jaeger_remote_sampling"})
handler := clientcfghttp.NewHTTPHandler(clientcfghttp.HTTPHandlerParams{
ConfigManager: &clientcfghttp.ConfigManager{
handler := samplinghttp.NewHandler(samplinghttp.HandlerParams{
ConfigManager: &samplinghttp.ConfigManager{
SamplingProvider: ext.strategyProvider,
},
MetricsFactory: mf,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package clientcfghttp
package http

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package clientcfghttp
package http

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package clientcfghttp
package http

import (
"encoding/json"
Expand All @@ -24,8 +24,8 @@ const mimeTypeApplicationJSON = "application/json"

var errBadRequest = errors.New("bad request")

// HTTPHandlerParams contains parameters that must be passed to NewHTTPHandler.
type HTTPHandlerParams struct {
// HandlerParams contains parameters that must be passed to NewHTTPHandler.
type HandlerParams struct {
ConfigManager configmanager.ClientConfigManager // required
MetricsFactory metrics.Factory // required

Expand All @@ -37,10 +37,10 @@ type HTTPHandlerParams struct {
LegacySamplingEndpoint bool
}

// HTTPHandler implements endpoints for used by Jaeger clients to retrieve client configuration,
// Handler implements endpoints for used by Jaeger clients to retrieve client configuration,
// such as sampling strategies.
type HTTPHandler struct {
params HTTPHandlerParams
type Handler struct {
params HandlerParams
metrics struct {
// Number of good sampling requests
SamplingRequestSuccess metrics.Counter `metric:"http-server.requests" tags:"type=sampling"`
Expand All @@ -65,15 +65,15 @@ type HTTPHandler struct {
}
}

// NewHTTPHandler creates new HTTPHandler.
func NewHTTPHandler(params HTTPHandlerParams) *HTTPHandler {
handler := &HTTPHandler{params: params}
// NewHandler creates new HTTPHandler.
func NewHandler(params HandlerParams) *Handler {
handler := &Handler{params: params}
metrics.MustInit(&handler.metrics, params.MetricsFactory, nil)
return handler
}

// RegisterRoutes registers configuration handlers with Gorilla Router.
func (h *HTTPHandler) RegisterRoutes(router *mux.Router) {
func (h *Handler) RegisterRoutes(router *mux.Router) {
prefix := h.params.BasePath
if h.params.LegacySamplingEndpoint {
router.HandleFunc(
Expand All @@ -92,7 +92,7 @@ func (h *HTTPHandler) RegisterRoutes(router *mux.Router) {
}

// RegisterRoutes registers configuration handlers with HTTP Router.
func (h *HTTPHandler) RegisterRoutesWithHTTP(router *http.ServeMux) {
func (h *Handler) RegisterRoutesWithHTTP(router *http.ServeMux) {
prefix := h.params.BasePath
router.HandleFunc(
prefix+"/",
Expand All @@ -102,7 +102,7 @@ func (h *HTTPHandler) RegisterRoutesWithHTTP(router *http.ServeMux) {
)
}

func (h *HTTPHandler) serviceFromRequest(w http.ResponseWriter, r *http.Request) (string, error) {
func (h *Handler) serviceFromRequest(w http.ResponseWriter, r *http.Request) (string, error) {
services := r.URL.Query()["service"]
if len(services) != 1 {
h.metrics.BadRequest.Inc(1)
Expand All @@ -112,7 +112,7 @@ func (h *HTTPHandler) serviceFromRequest(w http.ResponseWriter, r *http.Request)
return services[0], nil
}

func (h *HTTPHandler) writeJSON(w http.ResponseWriter, jsonData []byte) error {
func (h *Handler) writeJSON(w http.ResponseWriter, jsonData []byte) error {
w.Header().Add("Content-Type", mimeTypeApplicationJSON)
if _, err := w.Write(jsonData); err != nil {
h.metrics.WriteFailures.Inc(1)
Expand All @@ -121,7 +121,7 @@ func (h *HTTPHandler) writeJSON(w http.ResponseWriter, jsonData []byte) error {
return nil
}

func (h *HTTPHandler) serveSamplingHTTP(
func (h *Handler) serveSamplingHTTP(
w http.ResponseWriter,
r *http.Request,
encoder func(strategy *api_v2.SamplingStrategyResponse) ([]byte, error),
Expand All @@ -146,7 +146,7 @@ func (h *HTTPHandler) serveSamplingHTTP(
}
}

func (h *HTTPHandler) encodeThriftLegacy(strategy *api_v2.SamplingStrategyResponse) ([]byte, error) {
func (h *Handler) encodeThriftLegacy(strategy *api_v2.SamplingStrategyResponse) ([]byte, error) {
tStrategy, err := t2p.ConvertSamplingResponseFromDomain(strategy)
if err != nil {
h.metrics.BadThriftFailures.Inc(1)
Expand All @@ -162,7 +162,7 @@ func (h *HTTPHandler) encodeThriftLegacy(strategy *api_v2.SamplingStrategyRespon
return jsonBytes, nil
}

func (h *HTTPHandler) encodeProto(strategy *api_v2.SamplingStrategyResponse) ([]byte, error) {
func (h *Handler) encodeProto(strategy *api_v2.SamplingStrategyResponse) ([]byte, error) {
str, err := p2json.SamplingStrategyResponseToJSON(strategy)
if err != nil {
h.metrics.BadProtoFailures.Inc(1)
Expand All @@ -187,7 +187,7 @@ var samplingStrategyTypes = []api_v2.SamplingStrategyType{
//
// Thrift 0.9.3 classes generate this JSON:
// {"strategyType":"PROBABILISTIC","probabilisticSampling":{"samplingRate":0.5}}
func (*HTTPHandler) encodeThriftEnums092(jsonData []byte) []byte {
func (*Handler) encodeThriftEnums092(jsonData []byte) []byte {
str := string(jsonData)
for _, strategyType := range samplingStrategyTypes {
str = strings.Replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2017 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package clientcfghttp
package http

import (
"encoding/json"
Expand All @@ -18,16 +18,16 @@ import (
"github.com/stretchr/testify/require"

"github.com/jaegertracing/jaeger/internal/metricstest"
tSampling092 "github.com/jaegertracing/jaeger/internal/sampling/http/thrift-0.9.2"
p2json "github.com/jaegertracing/jaeger/model/converter/json"
tSampling092 "github.com/jaegertracing/jaeger/pkg/clientcfg/clientcfghttp/thrift-0.9.2"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
)

type testServer struct {
metricsFactory *metricstest.Factory
samplingProvider *mockSamplingProvider
server *httptest.Server
handler *HTTPHandler
handler *Handler
}

func withServer(
Expand All @@ -41,7 +41,7 @@ func withServer(
cfgMgr := &ConfigManager{
SamplingProvider: samplingProvider,
}
handler := NewHTTPHandler(HTTPHandlerParams{
handler := NewHandler(HandlerParams{
ConfigManager: cfgMgr,
MetricsFactory: metricsFactory,
BasePath: basePath,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2023 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package clientcfghttp
package http

import (
"testing"
Expand Down

0 comments on commit 46e7b25

Please sign in to comment.