-
Notifications
You must be signed in to change notification settings - Fork 63
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
[Integration][New Relic] - Add support for service level #840
Changes from all commits
5f48f71
71fa389
a897f21
ee63e7c
2dcf84b
dd72146
b1ab418
e9a3753
dd1291b
fcc32ff
b7c8ab9
59f1146
ed04b60
418edbe
7982ea5
ff43d1c
903dd05
eff6699
926cf6c
6f405ee
cd57ac6
917a894
2065027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
GET_SLI_BY_NRQL_QUERY = """ | ||
{ | ||
actor { | ||
account(id: {{ account_id }}) { | ||
nrql(query: "{{ nrql_query }}") { | ||
results | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
LIST_SLOS_QUERY = """ | ||
{ | ||
actor { | ||
entitySearch(query: "type ='SERVICE_LEVEL'") { | ||
count | ||
query | ||
results{{ next_cursor_request }} { | ||
entities { | ||
serviceLevel { | ||
indicators { | ||
resultQueries { | ||
indicator { | ||
nrql | ||
} | ||
} | ||
id | ||
name | ||
description | ||
createdBy { | ||
} | ||
guid | ||
updatedAt | ||
createdAt | ||
updatedBy { | ||
} | ||
objectives { | ||
description | ||
target | ||
name | ||
timeWindow { | ||
rolling { | ||
count | ||
unit | ||
} | ||
} | ||
} | ||
} | ||
} | ||
tags { | ||
key | ||
values | ||
} | ||
} | ||
nextCursor | ||
} | ||
} | ||
} | ||
} | ||
""" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from typing import Any, AsyncIterable, Tuple, Optional | ||
import httpx | ||
from port_ocean.context.ocean import ocean | ||
from newrelic_integration.core.query_templates.service_levels import ( | ||
LIST_SLOS_QUERY, | ||
GET_SLI_BY_NRQL_QUERY, | ||
) | ||
from newrelic_integration.core.utils import send_graph_api_request, format_tags | ||
from newrelic_integration.utils import ( | ||
render_query, | ||
) | ||
from newrelic_integration.core.paging import send_paginated_graph_api_request | ||
|
||
SLI_OBJECT = "__SLI" | ||
BATCH_SIZE = 50 | ||
|
||
|
||
class ServiceLevelsHandler: | ||
def __init__(self, http_client: httpx.AsyncClient): | ||
self.http_client = http_client | ||
|
||
async def get_service_level_indicator_value( | ||
self, http_client: httpx.AsyncClient, nrql: str | ||
) -> dict[Any, Any]: | ||
query = await render_query( | ||
GET_SLI_BY_NRQL_QUERY, | ||
nrql_query=nrql, | ||
account_id=ocean.integration_config.get("new_relic_account_id"), | ||
) | ||
response = await send_graph_api_request( | ||
http_client, query, request_type="get_service_level_indicator_value" | ||
) | ||
service_levels = ( | ||
response.get("data", {}) | ||
.get("actor", {}) | ||
.get("account", {}) | ||
.get("nrql", {}) | ||
.get("results", []) | ||
) | ||
if service_levels: | ||
return service_levels[0] | ||
return {} | ||
|
||
async def enrich_slo_with_sli_and_tags( | ||
self, service_level: dict[str, Any] | ||
) -> dict[str, Any]: | ||
# Get the NRQL which is used to build the actual SLI result | ||
nrql = ( | ||
service_level.get("serviceLevel", {}) | ||
.get("indicators", [])[0] | ||
.get("resultQueries", {}) | ||
.get("indicator", {}) | ||
.get("nrql") | ||
) | ||
service_level[SLI_OBJECT] = await self.get_service_level_indicator_value( | ||
self.http_client, nrql | ||
) | ||
format_tags(service_level) | ||
return service_level | ||
|
||
async def list_service_levels(self) -> AsyncIterable[list[dict[str, Any]]]: | ||
batch = [] | ||
async for service_level in send_paginated_graph_api_request( | ||
self.http_client, | ||
Comment on lines
+63
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you look inside the |
||
LIST_SLOS_QUERY, | ||
request_type="list_service_levels", | ||
extract_data=self._extract_service_levels, | ||
): | ||
batch.append(service_level) | ||
|
||
if len(batch) >= BATCH_SIZE: | ||
yield batch | ||
batch = [] # Clearing the batch for the next set of items | ||
|
||
if batch: | ||
yield batch | ||
|
||
@staticmethod | ||
async def _extract_service_levels( | ||
response: dict[Any, Any] | ||
) -> Tuple[Optional[str], list[dict[Any, Any]]]: | ||
"""Extract service levels from the response. used by send_paginated_graph_api_request""" | ||
results = ( | ||
response.get("data", {}) | ||
.get("actor", {}) | ||
.get("entitySearch", {}) | ||
.get("results", {}) | ||
) | ||
return results.get("nextCursor"), results.get("entities", []) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "newrelic" | ||
version = "0.1.69" | ||
version = "0.1.70" | ||
description = "New Relic Integration" | ||
authors = ["Tom Tankilevitch <[email protected]>"] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is that query is specific for SLI, can you add an example of the expected query that will be generated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.