Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added http request duration #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,28 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
log.Printf("\n%s\n", string(dump))
}

start := time.Now()

resp, err := c.cfg.HTTPClient.Do(request)
if err != nil {
return resp, err

duration := time.Since(start).Milliseconds()

attrs := map[*telemetry.Attribute]string{
telemetry.HTTPRequestMethod: request.Method,
telemetry.URLFull: request.URL.String(),
telemetry.HTTPHost: request.URL.Host,
telemetry.URLScheme: request.URL.Scheme,
telemetry.UserAgent: request.Header.Get("User-Agent"),
}

if c.cfg.Telemetry != nil {
telemetryParams := telemetry.TelemetryFactoryParameters{
Configuration: c.cfg.Telemetry,
}
telemetryInstance := telemetry.Get(telemetryParams)
if telemetryInstance != nil && telemetryInstance.Metrics != nil {
_, _ = telemetryInstance.Metrics.HTTPRequestDuration(float64(duration), attrs)
}
}

if c.cfg.Debug {
Expand Down
964 changes: 482 additions & 482 deletions api_open_fga.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions telemetry/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type MetricConfiguration struct {
}

type MetricsConfiguration struct {
METRIC_COUNTER_CREDENTIALS_REQUEST *MetricConfiguration `json:"fga_client_credentials_request,omitempty"`
METRIC_HISTOGRAM_REQUEST_DURATION *MetricConfiguration `json:"fga_client_request_duration,omitempty"`
METRIC_HISTOGRAM_QUERY_DURATION *MetricConfiguration `json:"fga_client_query_duration,omitempty"`
METRIC_COUNTER_CREDENTIALS_REQUEST *MetricConfiguration `json:"fga_client_credentials_request,omitempty"`
METRIC_HISTOGRAM_REQUEST_DURATION *MetricConfiguration `json:"fga_client_request_duration,omitempty"`
METRIC_HISTOGRAM_QUERY_DURATION *MetricConfiguration `json:"fga_client_query_duration,omitempty"`
METRIC_HISTOGRAM_HTTP_REQUEST_DURATION *MetricConfiguration `json:"fga_client_http_request_duration,omitempty"`
}

type Configuration struct {
Expand Down
11 changes: 9 additions & 2 deletions telemetry/histograms.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package telemetry

const (
METRIC_HISTOGRAM_REQUEST_DURATION string = "fga-client.request.duration"
METRIC_HISTOGRAM_QUERY_DURATION string = "fga-client.query.duration"
METRIC_HISTOGRAM_REQUEST_DURATION string = "fga-client.request.duration"
METRIC_HISTOGRAM_QUERY_DURATION string = "fga-client.query.duration"
METRIC_HISTOGRAM_HTTP_REQUEST_DURATION string = "fga-client.http_request.duration"
)

var (
Expand All @@ -17,4 +18,10 @@ var (
Unit: "milliseconds",
Description: "The total time it took (in milliseconds) for the FGA server to process and evaluate the request.",
}

HTTPRequestDuration = &Histogram{
Name: METRIC_HISTOGRAM_HTTP_REQUEST_DURATION,
Unit: "milliseconds",
Description: "The duration of each HTTP request sent by the SDK.",
}
)
46 changes: 23 additions & 23 deletions telemetry/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package telemetry
/*
CheckRequestTupleKeyInterface is a simplified interface that defines the methods the CheckRequestTupleKey struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestTupleKeyInterface interface {
GetUser() *string
}
/*
CheckRequestInterface is a simplified interface that defines the methods the CheckRequest struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestInterface interface {
GetTupleKey() CheckRequestTupleKeyInterface
RequestAuthorizationModelIdInterface
}
/*
RequestAuthorizationModelIdInterface is a generic interface that defines the GetAuthorizationModelId() method a Request struct implements, relevant to the context of the telemetry package.
*/
type RequestAuthorizationModelIdInterface interface {
GetAuthorizationModelId() *string
}
package telemetry

/*
CheckRequestTupleKeyInterface is a simplified interface that defines the methods the CheckRequestTupleKey struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestTupleKeyInterface interface {
GetUser() *string
}

/*
CheckRequestInterface is a simplified interface that defines the methods the CheckRequest struct implements, relevant to the context of the telemetry package.
*/
type CheckRequestInterface interface {
GetTupleKey() CheckRequestTupleKeyInterface
RequestAuthorizationModelIdInterface
}

/*
RequestAuthorizationModelIdInterface is a generic interface that defines the GetAuthorizationModelId() method a Request struct implements, relevant to the context of the telemetry package.
*/
type RequestAuthorizationModelIdInterface interface {
GetAuthorizationModelId() *string
}
15 changes: 15 additions & 0 deletions telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type MetricsInterface interface {
RequestDuration(value float64, attrs map[*Attribute]string) (metric.Float64Histogram, error)
QueryDuration(value float64, attrs map[*Attribute]string) (metric.Float64Histogram, error)
BuildTelemetryAttributes(requestMethod string, methodParameters map[string]interface{}, req *http.Request, res *http.Response, requestStarted time.Time, resendCount int) (map[*Attribute]string, float64, float64, error)
HTTPRequestDuration(value float64, attrs map[*Attribute]string) (metric.Float64Histogram, error)
}

func (m *Metrics) GetCounter(name string, description string) (metric.Int64Counter, error) {
Expand Down Expand Up @@ -85,3 +86,17 @@ func (m *Metrics) QueryDuration(value float64, attrs map[*Attribute]string) (met

return histogram, err
}

func (m *Metrics) HTTPRequestDuration(value float64, attrs map[*Attribute]string) (metric.Float64Histogram, error) {
var histogram, err = m.GetHistogram(HTTPRequestDuration.Name, HTTPRequestDuration.Description, HTTPRequestDuration.Unit)

if err == nil {
attrs, err := m.PrepareAttributes(HTTPRequestDuration, attrs, m.Configuration)

if err == nil {
histogram.Record(context.Background(), value, metric.WithAttributeSet(attrs))
}
}

return histogram, err
}
5 changes: 5 additions & 0 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func TestUnbind(t *testing.T) {
}
}

func (m *MockMetrics) HTTPRequestDuration(value float64, attrs map[*Attribute]string) (metric.Float64Histogram, error) {
histogram, _ := m.GetHistogram("http_request_duration", "HTTP request duration", "ms")
return histogram, nil
}

func TestCredentialsRequestMetric(t *testing.T) {
config := &Configuration{}
metrics := &MockMetrics{
Expand Down