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

[Backport 5.3.9104] symbols: canceling ParseRequest does not spam logs #61772

Merged
merged 3 commits into from
Apr 12, 2024
Merged
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
8 changes: 8 additions & 0 deletions cmd/symbols/parser/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
Expand Down Expand Up @@ -31,3 +32,10 @@ go_library(
"@io_opentelemetry_go_otel//attribute",
],
)

go_test(
name = "parser_test",
srcs = ["observability_test.go"],
embed = [":parser"],
deps = ["//internal/observation"],
)
33 changes: 28 additions & 5 deletions cmd/symbols/parser/observability.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package parser

import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/sourcegraph/sourcegraph/internal/metrics"
"github.com/sourcegraph/sourcegraph/internal/observation"
"github.com/sourcegraph/sourcegraph/lib/errors"
)

type operations struct {
parsing prometheus.Gauge
parseQueueSize prometheus.Gauge
parseQueueTimeouts prometheus.Counter
parseFailed prometheus.Counter
parseCanceled prometheus.Counter
parse *observation.Operation
handleParseRequest *observation.Operation
}
Expand Down Expand Up @@ -47,6 +50,13 @@ func newOperations(observationCtx *observation.Context) *operations {
})
observationCtx.Registerer.MustRegister(parseFailed)

parseCanceled := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "src",
Name: "codeintel_symbols_parse_canceled_total",
Help: "The total number of parse jobs that are canceled. Seperate to failed since we don't treat these as failed parses.",
})
observationCtx.Registerer.MustRegister(parseCanceled)

operationMetrics := metrics.NewREDMetrics(
observationCtx.Registerer,
"codeintel_symbols_parser",
Expand All @@ -55,20 +65,33 @@ func newOperations(observationCtx *observation.Context) *operations {
metrics.WithDurationBuckets([]float64{1, 5, 10, 60, 300, 1200}),
)

op := func(name string) *observation.Operation {
return observationCtx.Operation(observation.Op{
op := func(name string) observation.Op {
return observation.Op{
Name: fmt.Sprintf("codeintel.symbols.parser.%s", name),
MetricLabelValues: []string{name},
Metrics: operationMetrics,
})
}
}

// HandleParseRequest we run concurrently and when we cancel the request
// they all logspam the error. But in the case of cancelation this is not
// useful signal, but rather higher level operations should log the
// cancelation.
handleParseRequestOp := op("HandleParseRequest")
handleParseRequestOp.ErrorFilter = func(err error) observation.ErrorFilterBehaviour {
if errors.Is(err, context.Canceled) {
return observation.EmitForAllExceptLogs
}
return observation.EmitForDefault
}

return &operations{
parsing: parsing,
parseQueueSize: parseQueueSize,
parseQueueTimeouts: parseQueueTimeouts,
parseFailed: parseFailed,
parse: op("Parse"),
handleParseRequest: op("HandleParseRequest"),
parseCanceled: parseCanceled,
parse: observationCtx.Operation(op("Parse")),
handleParseRequest: observationCtx.Operation(handleParseRequestOp),
}
}
13 changes: 13 additions & 0 deletions cmd/symbols/parser/observability_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package parser

import (
"testing"

"github.com/sourcegraph/sourcegraph/internal/observation"
)

func TestNewOperation(t *testing.T) {
// tiny test that check for the side-effects of registering. EG if we have
// duplicate prometheus metrics.
_ = newOperations(observation.TestContextTB(t))
}
14 changes: 11 additions & 3 deletions cmd/symbols/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,19 @@ func (p *parser) handleParseRequest(
if err == nil {
p.parserPool.Done(parser, source)
} else {
// Close parser and return nil to pool, indicating that the next receiver should create a new parser
log15.Error("Closing failed parser", "error", err)
// If we are canceled we still kill the parser just in case, but
// we do not record as failure nor logspam since this is a more
// expected case.
if errors.Is(err, context.Canceled) {
p.operations.parseCanceled.Inc()
} else {
p.operations.parseFailed.Inc()
log15.Error("Closing failed parser", "error", err)
}
// Close parser and return nil to pool, indicating that the next
// receiver should create a new parser
parser.Close()
p.parserPool.Done(nil, source)
p.operations.parseFailed.Inc()
}
}()

Expand Down
Loading