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

Add missing fields in overrides API merged response #3018

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 0 additions & 36 deletions modules/overrides/userconfigurable/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import (
"github.com/opentracing/opentracing-go/ext"
ot_log "github.com/opentracing/opentracing-go/log"

"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/modules/overrides/userconfigurable/client"
"github.com/grafana/tempo/pkg/api"
"github.com/grafana/tempo/pkg/spanfilter/config"
"github.com/grafana/tempo/pkg/util/tracing"
"github.com/grafana/tempo/tempodb/backend"
)
Expand Down Expand Up @@ -195,37 +193,3 @@ func (a *UserConfigOverridesAPI) logRequest(ctx context.Context, handler string,
span.Finish()
}
}

func limitsFromOverrides(overrides overrides.Interface, userID string) *client.Limits {
return &client.Limits{
Forwarders: strArrPtr(overrides.Forwarders(userID)),
MetricsGenerator: &client.LimitsMetricsGenerator{
Processors: overrides.MetricsGeneratorProcessors(userID),
DisableCollection: boolPtr(overrides.MetricsGeneratorDisableCollection(userID)),
Processor: &client.LimitsMetricsGeneratorProcessor{
ServiceGraphs: &client.LimitsMetricsGeneratorProcessorServiceGraphs{
Dimensions: strArrPtr(overrides.MetricsGeneratorProcessorServiceGraphsDimensions(userID)),
EnableClientServerPrefix: boolPtr(overrides.MetricsGeneratorProcessorServiceGraphsEnableClientServerPrefix(userID)),
PeerAttributes: strArrPtr(overrides.MetricsGeneratorProcessorServiceGraphsPeerAttributes(userID)),
},
SpanMetrics: &client.LimitsMetricsGeneratorProcessorSpanMetrics{
Dimensions: strArrPtr(overrides.MetricsGeneratorProcessorSpanMetricsDimensions(userID)),
EnableTargetInfo: boolPtr(overrides.MetricsGeneratorProcessorSpanMetricsEnableTargetInfo(userID)),
FilterPolicies: filterPoliciesPtr(overrides.MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID)),
},
},
},
}
}

func boolPtr(b bool) *bool {
return &b
}

func strArrPtr(s []string) *[]string {
return &s
}

func filterPoliciesPtr(p []config.FilterPolicy) *[]config.FilterPolicy {
return &p
}
56 changes: 56 additions & 0 deletions modules/overrides/userconfigurable/api/limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package api

import (
"time"

"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/modules/overrides/userconfigurable/client"
"github.com/grafana/tempo/pkg/spanfilter/config"
)

// limitsFromOverrides will reconstruct a client.Limits from the overrides module
func limitsFromOverrides(overrides overrides.Interface, userID string) *client.Limits {
return &client.Limits{
Forwarders: strArrPtr(overrides.Forwarders(userID)),
MetricsGenerator: &client.LimitsMetricsGenerator{
Processors: overrides.MetricsGeneratorProcessors(userID),
DisableCollection: boolPtr(overrides.MetricsGeneratorDisableCollection(userID)),
CollectionInterval: timePtr(overrides.MetricsGeneratorCollectionInterval(userID)),
Processor: &client.LimitsMetricsGeneratorProcessor{
ServiceGraphs: &client.LimitsMetricsGeneratorProcessorServiceGraphs{
Dimensions: strArrPtr(overrides.MetricsGeneratorProcessorServiceGraphsDimensions(userID)),
EnableClientServerPrefix: boolPtr(overrides.MetricsGeneratorProcessorServiceGraphsEnableClientServerPrefix(userID)),
PeerAttributes: strArrPtr(overrides.MetricsGeneratorProcessorServiceGraphsPeerAttributes(userID)),
HistogramBuckets: floatArrPtr(overrides.MetricsGeneratorProcessorServiceGraphsHistogramBuckets(userID)),
},
SpanMetrics: &client.LimitsMetricsGeneratorProcessorSpanMetrics{
Dimensions: strArrPtr(overrides.MetricsGeneratorProcessorSpanMetricsDimensions(userID)),
EnableTargetInfo: boolPtr(overrides.MetricsGeneratorProcessorSpanMetricsEnableTargetInfo(userID)),
FilterPolicies: filterPoliciesPtr(overrides.MetricsGeneratorProcessorSpanMetricsFilterPolicies(userID)),
HistogramBuckets: floatArrPtr(overrides.MetricsGeneratorProcessorSpanMetricsHistogramBuckets(userID)),
TargetInfoExcludedDimensions: strArrPtr(overrides.MetricsGeneratorProcessorSpanMetricsTargetInfoExcludedDimensions(userID)),
},
},
},
}
}

func boolPtr(b bool) *bool {
return &b
}

func timePtr(t time.Duration) *client.Duration {
return &client.Duration{t}
}

func strArrPtr(s []string) *[]string {
return &s
}

func floatArrPtr(f []float64) *[]float64 {
return &f
}

func filterPoliciesPtr(p []config.FilterPolicy) *[]config.FilterPolicy {
return &p
}
119 changes: 119 additions & 0 deletions modules/overrides/userconfigurable/api/limits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package api

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/grafana/tempo/modules/overrides"
filterconfig "github.com/grafana/tempo/pkg/spanfilter/config"
)

func Test_limitsFromOverrides(t *testing.T) {
userID := "foo"

cfg := overrides.Config{
Defaults: overrides.Overrides{
Forwarders: []string{"my-forwarder"},
MetricsGenerator: overrides.MetricsGeneratorOverrides{
Processors: map[string]struct{}{"service-graphs": {}},
CollectionInterval: 15 * time.Second,
DisableCollection: true,
Processor: overrides.ProcessorOverrides{
ServiceGraphs: overrides.ServiceGraphsOverrides{
HistogramBuckets: []float64{0.1, 0.2, 0.5},
Dimensions: []string{"my-dim1", "my-dim2"},
PeerAttributes: []string{"db.name"},
EnableClientServerPrefix: true,
},
SpanMetrics: overrides.SpanMetricsOverrides{
Dimensions: []string{"your-dim1", "your-dim2"},
EnableTargetInfo: true,
FilterPolicies: []filterconfig.FilterPolicy{
{
Exclude: &filterconfig.PolicyMatch{
MatchType: filterconfig.Regex,
Attributes: []filterconfig.MatchPolicyAttribute{
{
Key: "resource.service.name",
Value: "unknown_service:myservice",
},
},
},
},
},
HistogramBuckets: []float64{1, 2, 5},
TargetInfoExcludedDimensions: []string{"no"},
},
},
},
},
}
overridesInt, err := overrides.NewOverrides(cfg)
assert.NoError(t, err)

limits := limitsFromOverrides(overridesInt, userID)
limitsJson, err := json.MarshalIndent(limits, "", " ")
assert.NoError(t, err)

expectedJson := `{
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test pick up if we forget to add a new field to limitsFromOverrides ? I think it does since the output json will get a null but just checking.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does because overrides.MetricsGeneratorOverrides will set the new value to the zero value which will be omitted in the json and you get a diff.

This what happens if I remove collection_interval from both the input overrides and the expected result:

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -9,2 +9,3 @@
        	            	     "disable_collection": true,
        	            	+    "collection_interval": "0s",
        	            	     "processor": {
        	Test:       	Test_limitsFromOverrides
--- FAIL: Test_limitsFromOverrides (0.00s)

"forwarders": [
"my-forwarder"
],
"metrics_generator": {
"processors": [
"service-graphs"
],
"disable_collection": true,
"collection_interval": "15s",
"processor": {
"service_graphs": {
"dimensions": [
"my-dim1",
"my-dim2"
],
"enable_client_server_prefix": true,
"peer_attributes": [
"db.name"
],
"histogram_buckets": [
0.1,
0.2,
0.5
]
},
"span_metrics": {
"dimensions": [
"your-dim1",
"your-dim2"
],
"enable_target_info": true,
"filter_policies": [
{
"exclude": {
"match_type": "regex",
"attributes": [
{
"key": "resource.service.name",
"value": "unknown_service:myservice"
}
]
}
}
],
"histogram_buckets": [
1,
2,
5
],
"target_info_excluded_dimensions": [
"no"
]
}
}
}
}`
assert.Equal(t, expectedJson, string(limitsJson))
}