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 metrics and refactor SPKS #81

Merged
merged 3 commits into from
Jul 5, 2024
Merged

Add metrics and refactor SPKS #81

merged 3 commits into from
Jul 5, 2024

Conversation

wejdross
Copy link
Member

  • adding prometheus metrics to catch issues when sending data to providers and Odoo
  • reffactoring SPKS module to fix bug and improve reliabiality and performance

Checklist

  • The PR has a meaningful title. It will be used to auto generate the
    changelog.
    The PR has a meaningful description that sums up the change. It will be
    linked in the changelog.
  • PR contains a single logical change (to build a better changelog).
  • Update the documentation.
  • Categorize the PR by adding one of the labels:
    bug, enhancement, documentation, change, breaking, dependency
    as they show up in the changelog.
  • Link this PR to related issues or PRs.

@wejdross wejdross force-pushed the addMetrics branch 2 times, most recently from c6c341e to 7f99373 Compare June 24, 2024 12:20
Copy link
Contributor

@zugao zugao left a comment

Choose a reason for hiding this comment

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

LGTM. Check the bugs in lab cluster, also don't forget to see if the prometheus metrics are exposed.

@wejdross wejdross requested review from a team, Kidswiss, TheBigLee and zugao and removed request for a team July 3, 2024 07:42
pkg/cmd/spks.go Outdated
logger.Error(err, "can't export data to Odoo")
return err
}
firstTime := make(chan bool, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

You could just call runSPKSBilling() here instead of creating an additional channel.

Copy link
Member Author

@wejdross wejdross Jul 4, 2024

Choose a reason for hiding this comment

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

actually this is leftover from way another approach :D I have no idea why it survived, it was part of retry mechanism etc. switching to simple function call

@@ -199,11 +201,13 @@ func QueryPrometheus(ctx context.Context, v1api v1.API, query string, logger log
case model.ValVector:
vectorVal := result.(model.Vector)
if len(vectorVal) != 1 {
return 0, fmt.Errorf("expected 1 result, got %d", len(vectorVal))
logger.Error(err, "Result vector length is not 1, prometheus query failed and returned: ", "result", vectorVal)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we increase the failure metric here?

}
default:
return 0, fmt.Errorf("result type is not Vector: %s", result.Type())
logger.Error(err, "result type is not Vector: ", "result", result)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here as well?

pkg/cmd/spks.go Outdated
logger.Error(err, "Error querying Prometheus")
return 0, err
return -1
Copy link
Contributor

Choose a reason for hiding this comment

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

Why aren't you returning (int, error) for this function? That would make it much clearer.

Also, instead of increasing the metrics here, you could do it in runSPKSBilling(). It would simplify the logic, as you'd only have to catch one error case and not all three as in this function.

pkg/cmd/spks.go Outdated
logger.Error(err, "Error querying Prometheus")
return err
}
now := time.Now().In(location)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you check with tobru if the timestamps are now really correct?

Copy link
Contributor

@zugao zugao Jul 3, 2024

Choose a reason for hiding this comment

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

We had to change a bit the code on Monday because the timestamp was showing 00:00Z instead of 22:00Z UTC. Now data is arriving properly.

Copy link
Member

Choose a reason for hiding this comment

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

Yes it's correct as it is now. We tested and verified it in Odoo directly.

Copy link
Contributor

@zugao zugao left a comment

Choose a reason for hiding this comment

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

Just fix Simon's comments and the rest is fine. We tested on Monday and data arrives ok.

Copy link
Member

@TheBigLee TheBigLee left a comment

Choose a reason for hiding this comment

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

LGTM. Just one small nitpick and Simon's fixes

pkg/cmd/spks.go Outdated
logger.Error(err, "Error querying Prometheus")
return err
}
now := time.Now().In(location)
Copy link
Member

Choose a reason for hiding this comment

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

Yes it's correct as it is now. We tested and verified it in Odoo directly.

pkg/cmd/spks.go Outdated
Comment on lines 114 to 149
{
ProductID: "appcat-spks-mariadb-standard",
InstanceID: "mariadb-standard",
SalesOrder: salesOrder,
UnitID: UnitID,
ConsumedUnits: float64(mariadbStandard),
TimeRange: odoo.TimeRange{
From: startYesterdayAbsolute,
To: endYesterdayAbsolute,
},
},
{
ProductID: "appcat-spks-mariadb-premium",
InstanceID: "mariadb-premium",
SalesOrder: salesOrder,
UnitID: UnitID,
ConsumedUnits: float64(mariadbPremium),
TimeRange: odoo.TimeRange{
From: startYesterdayAbsolute,
To: endYesterdayAbsolute,
},
},
{
ProductID: "appcat-spks-redis-standard",
InstanceID: "redis-standard",
SalesOrder: salesOrder,
UnitID: UnitID,
ConsumedUnits: float64(redisStandard),
TimeRange: odoo.TimeRange{
From: startYesterdayAbsolute,
To: endYesterdayAbsolute,
},
},
{
ProductID: "appcat-spks-redis-premium",
InstanceID: "redis-premium",
SalesOrder: salesOrder,
UnitID: UnitID,
ConsumedUnits: float64(redisPremium),
TimeRange: odoo.TimeRange{
From: startYesterdayAbsolute,
To: endYesterdayAbsolute,
},
},
Copy link
Member

Choose a reason for hiding this comment

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

Small nitpick: Lot's of copy&paste. This can probably be compacted

Copy link
Member Author

Choose a reason for hiding this comment

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

extracted timerange to separate variable, but generation of those provides even more code :D

@wejdross wejdross requested review from Kidswiss, TheBigLee and zugao July 5, 2024 09:24
@wejdross
Copy link
Member Author

wejdross commented Jul 5, 2024

 go run . spks (...) --prometheus-url http://127.0.0.1:9090 --days 4






2024-07-05T11:16:10.130+0200 | INFO | billing-collector-cloudservices | billing-collector-cloudservices/main.go:164 | Starting up billing-collector-cloudservices | {"date": "2024-07-05", "commit": "-dirty-", "go_os": "linux", "go_arch": "amd64", "go_version": "go1.22.3", "uid": 1000, "gid": 1000}
2024-07-05T11:16:10.130+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:63 | starting spks data collector
2024-07-05T11:16:10.130+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-01T00:00:00.000+0200", "startYesterdayAbsolute": "2024-06-30T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-01T00:00:00.000+0200"}
2024-07-05T11:16:10.130+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-02T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-01T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-02T00:00:00.000+0200"}
2024-07-05T11:16:10.131+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-03T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-02T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-03T00:00:00.000+0200"}
2024-07-05T11:16:10.131+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-04T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-03T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-04T00:00:00.000+0200"}
2024-07-05T11:16:10.131+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-05T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-04T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-05T00:00:00.000+0200"}


2024-07-05T11:16:15.131+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-05T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-04T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-05T00:00:00.000+0200"}
2024-07-05T11:16:20.134+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-05T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-04T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-05T00:00:00.000+0200"}
2024-07-05T11:16:25.134+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:115 | Running SPKS billing with such timeranges:  | {"startOfToday": "2024-07-05T00:00:00.000+0200", "startYesterdayAbsolute": "2024-07-04T00:00:00.000+0200", "endYesterdayAbsolute": "2024-07-05T00:00:00.000+0200"}
^C2024-07-05T11:16:29.888+0200 | INFO | billing-collector-cloudservices.spks | cmd/spks.go:77 | Received Context cancellation, exiting...

@wejdross wejdross merged commit 34d006e into master Jul 5, 2024
3 checks passed
@wejdross wejdross deleted the addMetrics branch July 5, 2024 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants