| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Copyright (c) 2021 The Jaeger Authors. |
| 5 |
// Copyright (c) 2017 Uber Technologies, Inc. |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
// Package testutils provides testing utilities for the jaegerremote sampler |
| 20 |
// package. |
| 21 |
package testutils // import "go.opentelemetry.io/contrib/samplers/jaegerremote/internal/testutils" |
| 22 |
|
| 23 |
import ( |
| 24 |
"encoding/json" |
| 25 |
"fmt" |
| 26 |
"net/http" |
| 27 |
"net/http/httptest" |
| 28 |
) |
| 29 |
|
| 30 |
// MockAgent is a mock representation of Jaeger Agent. |
| 31 |
// It has an HTTP endpoint for sampling strategies. |
| 32 |
type MockAgent struct { |
| 33 |
samplingMgr *samplingManager |
| 34 |
samplingSrv *httptest.Server |
| 35 |
} |
| 36 |
|
| 37 |
// StartMockAgent runs a mock representation of jaeger-agent. |
| 38 |
// This function returns a started server. |
| 39 |
func StartMockAgent() (*MockAgent, error) { |
| 40 |
samplingManager := newSamplingManager() |
| 41 |
samplingHandler := &samplingHandler{manager: samplingManager} |
| 42 |
samplingServer := httptest.NewServer(samplingHandler) |
| 43 |
|
| 44 |
agent := &MockAgent{ |
| 45 |
samplingMgr: samplingManager, |
| 46 |
samplingSrv: samplingServer, |
| 47 |
} |
| 48 |
|
| 49 |
return agent, nil |
| 50 |
} |
| 51 |
|
| 52 |
// Close stops the serving of traffic. |
| 53 |
func (s *MockAgent) Close() { |
| 54 |
s.samplingSrv.Close() |
| 55 |
} |
| 56 |
|
| 57 |
// SamplingServerAddr returns the host:port of HTTP server exposing sampling strategy endpoint. |
| 58 |
func (s *MockAgent) SamplingServerAddr() string { |
| 59 |
return s.samplingSrv.Listener.Addr().String() |
| 60 |
} |
| 61 |
|
| 62 |
// AddSamplingStrategy registers a sampling strategy for a service. |
| 63 |
func (s *MockAgent) AddSamplingStrategy(service string, strategy any) { |
| 64 |
s.samplingMgr.AddSamplingStrategy(service, strategy) |
| 65 |
} |
| 66 |
|
| 67 |
type samplingHandler struct { |
| 68 |
manager *samplingManager |
| 69 |
} |
| 70 |
|
| 71 |
func (h *samplingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 72 |
services := r.URL.Query()["service"] |
| 73 |
if len(services) == 0 { |
| 74 |
http.Error(w, "'service' parameter is empty", http.StatusBadRequest) |
| 75 |
return |
| 76 |
} |
| 77 |
if len(services) > 1 { |
| 78 |
http.Error(w, "'service' parameter must occur only once", http.StatusBadRequest) |
| 79 |
return |
| 80 |
} |
| 81 |
resp, err := h.manager.GetSamplingStrategy(services[0]) |
| 82 |
if err != nil { |
| 83 |
http.Error(w, fmt.Sprintf("Error retrieving strategy: %+v", err), http.StatusInternalServerError) |
| 84 |
return |
| 85 |
} |
| 86 |
data, err := json.Marshal(resp) |
| 87 |
if err != nil { |
| 88 |
http.Error(w, "Cannot marshall Thrift to JSON", http.StatusInternalServerError) |
| 89 |
return |
| 90 |
} |
| 91 |
w.Header().Add("Content-Type", "application/json") |
| 92 |
if _, err := w.Write(data); err != nil { |
| 93 |
return |
| 94 |
} |
| 95 |
} |