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

SLO - http request external duration and error source #989

Merged
merged 10 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
95 changes: 95 additions & 0 deletions experimental/slo/slo_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package slo

import (
"errors"
"fmt"
"net/http"
"strings"
"syscall"
"time"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var duration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since histogram don't you need to specify buckets? Have you verified subtracting histogram from histogram works as you imagine?

Copy link
Contributor Author

@scottlepp scottlepp May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grabbed the code from here as it looked similar. I'm no expert here but from my understanding here we need a histogram, or we have to have shorter scrape times. Since that code isn't using buckets, I assumed it would work.

Copy link
Contributor Author

@scottlepp scottlepp May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xnyo suggested subtracting the values, but that was with metrics captured from tempo which appear to be histogram. Not sure if he ever confirmed that subtraction would work.

Span metrics generate two metrics: a counter that computes requests, and a histogram that computes operation’s durations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But wasn't he thinking taking the full latency of a trace and subtracting downstream span latencies and then update latency histogram, e.g. it's not prometheus doing the subtraction and you get one metric.

So my question is more, given you have two different latency histogram metrics how easy is it to reliably subtract one from the other and how does that scale/perform?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested if it subtracting histograms works reliably, but the way this histogram is set up should replicate what Tempo does with the metrics generator. I think @svennergr mentioned he was doing something similar with histograms in the ElasticSearch datasource as well, so he may have some guidance on whether this will be accurate or not

Namespace: "grafana",
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
Name: "plugin_external_requests_duration_seconds",
Help: "Duration of requests to external services",
}, []string{"datasource_name", "datasource_type", "error_source"})

const DataSourceSLOMiddlewareName = "slo"

// Middleware captures duration of requests to external services and the source of errors
func Middleware(plugin string) httpclient.Middleware {
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
return httpclient.NamedMiddlewareFunc(DataSourceSLOMiddlewareName, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
return RoundTripper(plugin, opts, next)
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
})
}

// RoundTripper captures duration of requests to external services and the source of errors
func RoundTripper(plugin string, opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
return httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
start := time.Now()
var errorSource = "none"

defer func() {
if opts.Labels == nil {
return
}

datasourceName, exists := opts.Labels["datasource_name"]
if !exists {
return
}

datasourceLabelName, err := SanitizeLabelName(datasourceName)
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
// if the datasource named cannot be turned into a prometheus
// label we will skip instrumenting these metrics.
if err != nil {
return
}

datasourceType, exists := opts.Labels["datasource_type"]
if !exists {
return
}
duration.WithLabelValues(datasourceLabelName, datasourceType, errorSource).Observe(time.Since(start).Seconds())
}()

res, err := next.RoundTrip(req)
if res != nil && res.StatusCode >= 400 {
errorSource = string(backend.ErrorSourceFromHTTPStatus(res.StatusCode))
}
if errors.Is(err, syscall.ECONNREFUSED) {
scottlepp marked this conversation as resolved.
Show resolved Hide resolved
errorSource = string(backend.ErrorSourceDownstream)
}
return res, err
})
}

// SanitizeLabelName removes all invalid chars from the label name.
// If the label name is empty or contains only invalid chars, it
// will return an error.
func SanitizeLabelName(name string) (string, error) {
if len(name) == 0 {
return "", errors.New("label name cannot be empty")
}

out := strings.Builder{}
for i, b := range name {
if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0) {
out.WriteRune(b)
} else if b == ' ' {
out.WriteRune('_')
}
}

if out.Len() == 0 {
return "", fmt.Errorf("label name only contains invalid chars: %q", name)
}

return out.String(), nil
}
4 changes: 2 additions & 2 deletions experimental/testdata/folder.golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Frame[0] {
"pathSeparator": "/"
}
Name:
Dimensions: 2 Fields by 20 Rows
Dimensions: 2 Fields by 21 Rows
+----------------+------------------+
| Name: name | Name: media-type |
| Labels: | Labels: |
Expand All @@ -29,4 +29,4 @@ Dimensions: 2 Fields by 20 Rows


====== TEST DATA RESPONSE (arrow base64) ======
FRAME=QVJST1cxAAD/////yAEAABAAAAAAAAoADgAMAAsABAAKAAAAFAAAAAAAAAEEAAoADAAAAAgABAAKAAAACAAAALgAAAADAAAATAAAACgAAAAEAAAAwP7//wgAAAAMAAAAAAAAAAAAAAAFAAAAcmVmSWQAAADg/v//CAAAAAwAAAAAAAAAAAAAAAQAAABuYW1lAAAAAAD///8IAAAAUAAAAEQAAAB7InR5cGUiOiJkaXJlY3RvcnktbGlzdGluZyIsInR5cGVWZXJzaW9uIjpbMCwwXSwicGF0aFNlcGFyYXRvciI6Ii8ifQAAAAAEAAAAbWV0YQAAAAACAAAAeAAAAAQAAACi////FAAAADwAAAA8AAAAAAAABTgAAAABAAAABAAAAJD///8IAAAAEAAAAAYAAABzdHJpbmcAAAYAAAB0c3R5cGUAAAAAAACI////CgAAAG1lZGlhLXR5cGUAAAAAEgAYABQAAAATAAwAAAAIAAQAEgAAABQAAABEAAAASAAAAAAAAAVEAAAAAQAAAAwAAAAI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