File size: 2,606 Bytes
1c4c66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package httpdriver

import (
	"context"
	"errors"
	"net/http"

	"github.com/openmeterio/openmeter/openmeter/debug"
	"github.com/openmeterio/openmeter/openmeter/namespace/namespacedriver"
	"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
	"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
	"github.com/openmeterio/openmeter/pkg/models"
)

type DebugHandler interface {
	GetMetrics() GetMetricsHandler
}

type debugHandler struct {
	namespaceDecoder namespacedriver.NamespaceDecoder
	debugConnector   debug.DebugConnector
	options          []httptransport.HandlerOption
}

func NewDebugHandler(
	namespaceDecoder namespacedriver.NamespaceDecoder,
	debugConnector debug.DebugConnector,
	options ...httptransport.HandlerOption,
) DebugHandler {
	return &debugHandler{
		namespaceDecoder: namespaceDecoder,
		debugConnector:   debugConnector,
		options:          options,
	}
}

type GetMetricsHandlerRequestParams struct {
	Namespace string
}

type GetMetricsHandlerRequest struct {
	params GetMetricsHandlerRequestParams
}
type (
	GetMetricsHandlerResponse = string
	GetMetricsHandlerParams   struct{}
	GetMetricsHandler         httptransport.HandlerWithArgs[GetMetricsHandlerRequest, GetMetricsHandlerResponse, GetMetricsHandlerParams]
)

func (h *debugHandler) GetMetrics() GetMetricsHandler {
	return httptransport.NewHandlerWithArgs[GetMetricsHandlerRequest, string, GetMetricsHandlerParams](
		func(ctx context.Context, r *http.Request, params GetMetricsHandlerParams) (GetMetricsHandlerRequest, error) {
			ns, err := h.resolveNamespace(ctx)
			if err != nil {
				return GetMetricsHandlerRequest{}, err
			}

			return GetMetricsHandlerRequest{
				params: GetMetricsHandlerRequestParams{
					Namespace: ns,
				},
			}, nil
		},
		func(ctx context.Context, request GetMetricsHandlerRequest) (string, error) {
			return h.debugConnector.GetDebugMetrics(ctx, request.params.Namespace)
		},
		commonhttp.PlainTextResponseEncoder[string],
		httptransport.AppendOptions(
			h.options,
			httptransport.WithErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter, _ *http.Request) bool {
				if models.IsGenericValidationError(err) {
					commonhttp.NewHTTPError(
						http.StatusBadRequest,
						err,
					).EncodeError(ctx, w)
					return true
				}
				return false
			}),
		)...,
	)
}

func (h *debugHandler) resolveNamespace(ctx context.Context) (string, error) {
	ns, ok := h.namespaceDecoder.GetNamespace(ctx)
	if !ok {
		return "", commonhttp.NewHTTPError(http.StatusInternalServerError, errors.New("internal server error"))
	}

	return ns, nil
}