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

Fix trailing slash requirement for prometheus url and add tests #14

Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ dmypy.json

# Pyre type checker
.pyre/

.DS_Store
12 changes: 8 additions & 4 deletions src/autometrics/prometheus_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@


class Generator:
def __init__(self, functionName, moduleName):
def __init__(self, functionName, moduleName, baseUrl=None):
load_dotenv()
self.functionName = functionName
self.moduleName = moduleName
self.baseUrl = os.getenv("PROMETHEUS_URL")
self.baseUrl = baseUrl if baseUrl is not None else os.getenv("PROMETHEUS_URL")
Copy link
Contributor

Choose a reason for hiding this comment

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

you can just do:

self.baseUrl = baseUrl or os.getenv("PROMETHEUS_URL")

if self.baseUrl is None:
self.baseUrl = "http://localhost:9090/"
self.baseUrl = "http://localhost:9090"
elif self.baseUrl[-1] == "/":
self.baseUrl = self.baseUrl[
:-1
] # Remove the trailing slash if there is one

def createURLs(self):
requestRateQuery = f'sum by (function, module) (rate (function_calls_count_total{{function="{self.functionName}",module="{self.moduleName}"}}[5m]))'
Expand All @@ -30,5 +34,5 @@ def createURLs(self):

def createPrometheusUrl(self, query):
urlEncode = urllib.parse.quote(query)
url = f"{self.baseUrl}graph?g0.expr={urlEncode}&g0.tab=0"
url = f"{self.baseUrl}/graph?g0.expr={urlEncode}&g0.tab=0"
return url
34 changes: 34 additions & 0 deletions src/test_prometheus_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest
from autometrics.prometheus_url import Generator


# Defaults to localhost:9090
class TestPrometheusUrlGeneratorDefault(unittest.TestCase):
def setUp(self):
self.generator = Generator("myFunction", "myModule")

def test_createPrometheusUrl(self):
url = self.generator.createPrometheusUrl("myQuery")
self.assertTrue(
url.startswith("http://localhost:9090/graph?g0.expr=")
) # Make sure the base URL is correct
self.assertIn("myQuery", url) # Make sure the query is included in the URL


# Creates proper urls when given a custom base URL
class TestPrometheusUrlGeneratorCustomUrl(unittest.TestCase):
def setUp(self):
self.generator = Generator(
"myFunction", "myModule", baseUrl="http://localhost:9091"
)

def test_createPrometheusUrl(self):
url = self.generator.createPrometheusUrl("myQuery")
self.assertTrue(
url.startswith("http://localhost:9091/graph?g0.expr=")
) # Make sure the base URL is correct
self.assertIn("myQuery", url) # Make sure the query is included in the URL


if __name__ == "__main__":
unittest.main()