Skip to content

Commit 409d5f3

Browse files
committed
Add small refactoring
1 parent d9d4315 commit 409d5f3

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,17 @@ func NewDataCollectorImpl(
133133

134134
// Collect collects and returns telemetry Data.
135135
func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
136+
g := c.cfg.GraphGetter.GetLatestGraph()
137+
if g == nil {
138+
return Data{}, errors.New("failed to collect telemetry data: latest graph cannot be nil")
139+
}
140+
136141
clusterInfo, err := collectClusterInformation(ctx, c.cfg.K8sClientReader)
137142
if err != nil {
138143
return Data{}, fmt.Errorf("failed to collect cluster information: %w", err)
139144
}
140145

141-
graphResourceCount, err := collectGraphResourceCount(c.cfg.GraphGetter, c.cfg.ConfigurationGetter)
146+
graphResourceCount, err := collectGraphResourceCount(g, c.cfg.ConfigurationGetter)
142147
if err != nil {
143148
return Data{}, fmt.Errorf("failed to collect NGF resource counts: %w", err)
144149
}
@@ -158,12 +163,7 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
158163
return Data{}, fmt.Errorf("failed to get NGF deploymentID: %w", err)
159164
}
160165

161-
snippetsFiltersDirectiveContexts,
162-
snippetsFiltersDirectiveContextsCount,
163-
err := collectSnippetsFilterSnippetsInfo(c.cfg.GraphGetter)
164-
if err != nil {
165-
return Data{}, fmt.Errorf("failed to collect snippet filter directive info: %w", err)
166-
}
166+
snippetsFiltersDirectiveContexts, snippetsFiltersDirectiveContextsCount := collectSnippetsFilterSnippetsInfo(g)
167167

168168
data := Data{
169169
Data: tel.Data{
@@ -190,16 +190,12 @@ func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
190190
}
191191

192192
func collectGraphResourceCount(
193-
graphGetter GraphGetter,
193+
g *graph.Graph,
194194
configurationGetter ConfigurationGetter,
195195
) (NGFResourceCounts, error) {
196196
ngfResourceCounts := NGFResourceCounts{}
197-
g := graphGetter.GetLatestGraph()
198197
cfg := configurationGetter.GetLatestConfiguration()
199198

200-
if g == nil {
201-
return ngfResourceCounts, errors.New("latest graph cannot be nil")
202-
}
203199
if cfg == nil {
204200
return ngfResourceCounts, errors.New("latest configuration cannot be nil")
205201
}
@@ -406,16 +402,11 @@ func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (cl
406402
}
407403

408404
type sfDirectiveContext struct {
409-
context string
410405
directive string
406+
context string
411407
}
412408

413-
func collectSnippetsFilterSnippetsInfo(graphGetter GraphGetter) ([]string, []int64, error) {
414-
g := graphGetter.GetLatestGraph()
415-
if g == nil {
416-
return nil, nil, errors.New("latest graph cannot be nil")
417-
}
418-
409+
func collectSnippetsFilterSnippetsInfo(g *graph.Graph) ([]string, []int64) {
419410
directiveContextMap := make(map[sfDirectiveContext]int)
420411

421412
for _, sf := range g.SnippetsFilters {
@@ -442,17 +433,15 @@ func collectSnippetsFilterSnippetsInfo(graphGetter GraphGetter) ([]string, []int
442433
directives := parseSnippetValueIntoDirectives(snippetValue)
443434
for _, directive := range directives {
444435
directiveContext := sfDirectiveContext{
445-
context: parsedContext,
446436
directive: directive,
437+
context: parsedContext,
447438
}
448439
directiveContextMap[directiveContext]++
449440
}
450441
}
451442
}
452443

453-
directiveContextList, countList := parseDirectiveContextMapIntoLists(directiveContextMap)
454-
455-
return directiveContextList, countList, nil
444+
return parseDirectiveContextMapIntoLists(directiveContextMap)
456445
}
457446

458447
func parseSnippetValueIntoDirectives(snippetValue string) []string {

internal/mode/static/telemetry/collector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ var _ = Describe("Collector", Ordered, func() {
722722
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
723723
})
724724
It("should error on nil latest graph", func(ctx SpecContext) {
725-
expectedError := errors.New("latest graph cannot be nil")
725+
expectedError := errors.New("failed to collect telemetry data: latest graph cannot be nil")
726726
fakeGraphGetter.GetLatestGraphReturns(nil)
727727

728728
_, err := dataCollector.Collect(ctx)

internal/mode/static/telemetry/data.avdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ at the same index.
4545
Each value is either 'true' or 'false' for boolean flags and 'default' or 'user-defined' for non-boolean flags. */
4646
union {null, array<string>} FlagValues = null;
4747

48-
/** SnippetsFiltersDirectiveContexts contains the context-directive strings of all applied SnippetsFilters.
48+
/** SnippetsFiltersDirectiveContexts contains the directive-context strings of all applied SnippetsFilters.
4949
Both lists are ordered first by count, then by lexicographical order of the context string,
5050
then lastly by directive string. */
5151
union {null, array<string>} SnippetsFiltersDirectiveContexts = null;

0 commit comments

Comments
 (0)