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

[Integration][New Relic] - Add support for service level #840

Merged
merged 23 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
54 changes: 54 additions & 0 deletions integrations/newrelic/.port/resources/blueprints.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,59 @@
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
},
{
"identifier": "newRelicServiceLevel",
"description": "This blueprint represents a New Relic Service Level",
"title": "New Relic Service Level",
"icon": "NewRelic",
"schema": {
"properties": {
"description": {
"title": "Description",
"type": "string"
},
"targetThreshold": {
"icon": "DefaultProperty",
"title": "Target Threshold",
"type": "number"
},
"createdAt": {
"title": "Created At",
"type": "string",
"format": "date-time"
},
"updatedAt": {
"title": "Updated At",
"type": "string",
"format": "date-time"
},
"createdBy": {
"title": "Creator",
"type": "string",
"format": "user"
},
"serviceLevelIndicator": {
"type": "number",
"title": "Service Level Indicator"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"serviceLevelIndicator": {
"type": "number",
"title": "Service Level Indicator"
"sli": {
"type": "number",
"title": "SLI"

},
"tags": {
"type": "object",
"title": "Tags"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"aggregationProperties": {},
"relations": {
"newRelicService": {
"title": "New Relic service",
"target": "newRelicService",
"required": false,
"many": false
}
}
}
]
19 changes: 19 additions & 0 deletions integrations/newrelic/.port/resources/port-app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,22 @@ resources:
activatedAt: .activatedAt
relations:
newRelicService: ".__APPLICATION.entity_guids + .__SERVICE.entity_guids"
- kind: newRelicServiceLevel
selector:
query: 'true'
port:
entity:
mappings:
blueprint: '"newRelicServiceLevel"'
identifier: .serviceLevel.indicators[0].id
title: .serviceLevel.indicators[0].name
properties:
description: .serviceLevel.indicators[0].description
targetThreshold: .serviceLevel.indicators[0].objectives[0].target
createdAt: if .serviceLevel.indicators[0].createdAt != null then (.serviceLevel.indicators[0].createdAt | tonumber / 1000 | todate) else null end
updatedAt: .serviceLevel.indicators[0].updatedAt
createdBy: .serviceLevel.indicators[0].createdBy.email
serviceLevelIndicator: .__SLI.SLI
tags: .tags
relations:
newRelicService: .serviceLevel.indicators[0].guid
1 change: 1 addition & 0 deletions integrations/newrelic/.port/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ features:
resources:
- kind: newRelicService
- kind: newRelicAlert
- kind: newRelicServiceLevel
configurations:
- name: newRelicAPIKey
required: true
Expand Down
7 changes: 7 additions & 0 deletions integrations/newrelic/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

# Port_Ocean 0.1.62 (2024-07-24)

### Improvements

- Added support for service level indicators and objectives


# Port_Ocean 0.1.61 (2024-07-24)

### Improvements
Expand Down
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
Comment on lines +3 to +6
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"indicator":{
                  "nrql":"SELECT clamp_max((sum(newrelic.sli.valid) - sum(newrelic.sli.bad)) / sum(newrelic.sli.valid) * 100, 100) AS 'SLI' FROM Metric WHERE entity.guid = 'NDM2OTY4MHxFWFR8U0VSVklDRV9MRVZFTHw1OTk0MzM' UNTIL 2 minutes AGO"
               }

}
}
}
}
"""

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 {
email
}
guid
updatedAt
createdAt
updatedBy {
email
}
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,80 @@
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
from newrelic_integration.utils import (
render_query,
)
from newrelic_integration.core.paging import send_paginated_graph_api_request

SLI_OBJECT = "__SLI"


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 list_service_levels(self) -> AsyncIterable[dict[str, Any]]:

async for service_level in send_paginated_graph_api_request(
self.http_client,
Comment on lines +63 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

if you look inside the send_paginated_graph_api_request it actually already fetched a batch, but returns it item by item, so to make it return faster, and maybe async gather the SLIs for multiple SLOs I would suggest either adjusting the send_paginated_graph_api_request or gathering a size of a batch and then bringing the SLI for each one, and using the stream_async_iterators_tasks to return the results for each one.

LIST_SLOS_QUERY,
request_type="list_service_levels",
extract_data=self._extract_service_levels,
):
nrql = (
service_level.get("serviceLevel", {})
Copy link
Contributor

Choose a reason for hiding this comment

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

add comment about that you are taking the nrql that the SLI is actually built from and query it to get the result

.get("indicators", [])[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible that they will have more than one indicator and would want to have more than one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I confirmed this and it is not possible to have more than 1

.get("resultQueries", {})
.get("indicator", {})
.get("nrql")
)
service_level[SLI_OBJECT] = await self.get_service_level_indicator_value(
self.http_client, nrql
)
self._format_tags(service_level)
yield service_level

@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", [])

@staticmethod
def _format_tags(entity: dict[Any, Any]) -> dict[Any, Any]:
entity["tags"] = {tag["key"]: tag["values"] for tag in entity.get("tags", [])}
return entity
Copy link
Contributor

Choose a reason for hiding this comment

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

this is repeating code we already have in core/entities.py I would move it to core/utils and re-use it in all places

12 changes: 12 additions & 0 deletions integrations/newrelic/newrelic_integration/ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from newrelic_integration.core.entities import EntitiesHandler
from newrelic_integration.core.issues import IssuesHandler, IssueState, IssueEvent
from newrelic_integration.core.service_levels import ServiceLevelsHandler

from newrelic_integration.utils import (
get_port_resource_configuration_by_newrelic_entity_type,
get_port_resource_configuration_by_port_kind,
Expand Down Expand Up @@ -63,6 +65,16 @@ async def resync_issues(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
yield await IssuesHandler(http_client).list_issues()


@ocean.on_resync(kind="newRelicServiceLevel")
async def resync_service_levels(kind: str) -> ASYNC_GENERATOR_RESYNC_TYPE:
with logger.contextualize(resource_kind=kind):
async with httpx.AsyncClient() as http_client:
async for service_levels in ServiceLevelsHandler(
http_client
).list_service_levels():
yield [service_levels]


@ocean.router.post("/events")
async def handle_issues_events(issue: IssueEvent) -> dict[str, bool]:
with logger.contextualize(issue_id=issue.id, issue_state=issue.state):
Expand Down
2 changes: 1 addition & 1 deletion integrations/newrelic/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "newrelic"
version = "0.1.61"
version = "0.1.62"
description = "New Relic Integration"
authors = ["Tom Tankilevitch <[email protected]>"]

Expand Down