Skip to content

Commit

Permalink
Add small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Oct 11, 2024
1 parent d9d4315 commit 409d5f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
35 changes: 12 additions & 23 deletions internal/mode/static/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ func NewDataCollectorImpl(

// Collect collects and returns telemetry Data.
func (c DataCollectorImpl) Collect(ctx context.Context) (Data, error) {
g := c.cfg.GraphGetter.GetLatestGraph()
if g == nil {
return Data{}, errors.New("failed to collect telemetry data: latest graph cannot be nil")
}

clusterInfo, err := collectClusterInformation(ctx, c.cfg.K8sClientReader)
if err != nil {
return Data{}, fmt.Errorf("failed to collect cluster information: %w", err)
}

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

snippetsFiltersDirectiveContexts,
snippetsFiltersDirectiveContextsCount,
err := collectSnippetsFilterSnippetsInfo(c.cfg.GraphGetter)
if err != nil {
return Data{}, fmt.Errorf("failed to collect snippet filter directive info: %w", err)
}
snippetsFiltersDirectiveContexts, snippetsFiltersDirectiveContextsCount := collectSnippetsFilterSnippetsInfo(g)

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

func collectGraphResourceCount(
graphGetter GraphGetter,
g *graph.Graph,
configurationGetter ConfigurationGetter,
) (NGFResourceCounts, error) {
ngfResourceCounts := NGFResourceCounts{}
g := graphGetter.GetLatestGraph()
cfg := configurationGetter.GetLatestConfiguration()

if g == nil {
return ngfResourceCounts, errors.New("latest graph cannot be nil")
}
if cfg == nil {
return ngfResourceCounts, errors.New("latest configuration cannot be nil")
}
Expand Down Expand Up @@ -406,16 +402,11 @@ func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (cl
}

type sfDirectiveContext struct {
context string
directive string
context string
}

func collectSnippetsFilterSnippetsInfo(graphGetter GraphGetter) ([]string, []int64, error) {
g := graphGetter.GetLatestGraph()
if g == nil {
return nil, nil, errors.New("latest graph cannot be nil")
}

func collectSnippetsFilterSnippetsInfo(g *graph.Graph) ([]string, []int64) {
directiveContextMap := make(map[sfDirectiveContext]int)

for _, sf := range g.SnippetsFilters {
Expand All @@ -442,17 +433,15 @@ func collectSnippetsFilterSnippetsInfo(graphGetter GraphGetter) ([]string, []int
directives := parseSnippetValueIntoDirectives(snippetValue)
for _, directive := range directives {
directiveContext := sfDirectiveContext{
context: parsedContext,
directive: directive,
context: parsedContext,
}
directiveContextMap[directiveContext]++
}
}
}

directiveContextList, countList := parseDirectiveContextMapIntoLists(directiveContextMap)

return directiveContextList, countList, nil
return parseDirectiveContextMapIntoLists(directiveContextMap)
}

func parseSnippetValueIntoDirectives(snippetValue string) []string {
Expand Down
2 changes: 1 addition & 1 deletion internal/mode/static/telemetry/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ var _ = Describe("Collector", Ordered, func() {
fakeConfigurationGetter.GetLatestConfigurationReturns(&dataplane.Configuration{})
})
It("should error on nil latest graph", func(ctx SpecContext) {
expectedError := errors.New("latest graph cannot be nil")
expectedError := errors.New("failed to collect telemetry data: latest graph cannot be nil")
fakeGraphGetter.GetLatestGraphReturns(nil)

_, err := dataCollector.Collect(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/mode/static/telemetry/data.avdl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ at the same index.
Each value is either 'true' or 'false' for boolean flags and 'default' or 'user-defined' for non-boolean flags. */
union {null, array<string>} FlagValues = null;

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

0 comments on commit 409d5f3

Please sign in to comment.