-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(ingest): grafana connector #10891
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
caf861e
feat(ingest): grafana connector which ingests dashboards
anshbansal b17e694
remove TODO
anshbansal 8ee2e0c
better error handling
anshbansal 1246965
remove commented out code
anshbansal f5b30e5
Update metadata-ingestion/src/datahub/ingestion/source/grafana/grafan…
shirshanka 3312b10
move to mcp emission
shirshanka 2e05fb0
add basic integration test, move to mcp
shirshanka 532b655
add test
shirshanka f8060ad
remove dead code, breakpoints
shirshanka 2837ef5
add docker-compose
shirshanka 853bbc1
complain more loudly for extractor failures
hsheth2 478dfd1
fix grafana source
hsheth2 b3f0564
bandaid lint, fixup goldens
shirshanka c084fae
update goldens
hsheth2 b663b5b
undo pipeline changes - moved to https://github.com/datahub-project/d…
hsheth2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
131 changes: 131 additions & 0 deletions
131
metadata-ingestion/src/datahub/ingestion/source/grafana/grafana_source.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,131 @@ | ||||||
from typing import Iterable, List, Optional | ||||||
|
||||||
import requests | ||||||
from pydantic import Field, SecretStr | ||||||
|
||||||
import datahub.emitter.mce_builder as builder | ||||||
from datahub.configuration.source_common import PlatformInstanceConfigMixin | ||||||
from datahub.emitter.mcp import MetadataChangeProposalWrapper | ||||||
from datahub.ingestion.api.common import PipelineContext | ||||||
from datahub.ingestion.api.decorators import ( | ||||||
SupportStatus, | ||||||
config_class, | ||||||
platform_name, | ||||||
support_status, | ||||||
) | ||||||
from datahub.ingestion.api.source import MetadataWorkUnitProcessor | ||||||
from datahub.ingestion.api.source_helpers import auto_workunit | ||||||
from datahub.ingestion.api.workunit import MetadataWorkUnit | ||||||
from datahub.ingestion.source.state.stale_entity_removal_handler import ( | ||||||
StaleEntityRemovalHandler, | ||||||
StaleEntityRemovalSourceReport, | ||||||
StatefulIngestionConfigBase, | ||||||
) | ||||||
from datahub.ingestion.source.state.stateful_ingestion_base import ( | ||||||
StatefulIngestionReport, | ||||||
StatefulIngestionSourceBase, | ||||||
) | ||||||
from datahub.metadata.com.linkedin.pegasus2avro.common import ChangeAuditStamps | ||||||
from datahub.metadata.schema_classes import DashboardInfoClass, StatusClass | ||||||
|
||||||
|
||||||
class GrafanaSourceConfig(StatefulIngestionConfigBase, PlatformInstanceConfigMixin): | ||||||
url: str = Field( | ||||||
default="", | ||||||
description="Grafana URL in the format http://your-grafana-instance with no trailing slash", | ||||||
) | ||||||
service_account_token: SecretStr = Field( | ||||||
description="Service account token for Grafana" | ||||||
) | ||||||
|
||||||
|
||||||
class GrafanaReport(StaleEntityRemovalSourceReport): | ||||||
pass | ||||||
|
||||||
|
||||||
@platform_name("Grafana") | ||||||
@config_class(GrafanaSourceConfig) | ||||||
@support_status(SupportStatus.TESTING) | ||||||
class GrafanaSource(StatefulIngestionSourceBase): | ||||||
""" | ||||||
This is an experimental source for Grafana. | ||||||
Currently only ingests dashboards (no charts) | ||||||
""" | ||||||
|
||||||
def __init__(self, config: GrafanaSourceConfig, ctx: PipelineContext): | ||||||
super().__init__(config, ctx) | ||||||
self.source_config = config | ||||||
self.report = GrafanaReport() | ||||||
self.platform = "grafana" | ||||||
|
||||||
@classmethod | ||||||
def create(cls, config_dict, ctx): | ||||||
config = GrafanaSourceConfig.parse_obj(config_dict) | ||||||
return cls(config, ctx) | ||||||
|
||||||
def get_workunit_processors(self) -> List[Optional[MetadataWorkUnitProcessor]]: | ||||||
return [ | ||||||
*super().get_workunit_processors(), | ||||||
StaleEntityRemovalHandler.create( | ||||||
self, self.source_config, self.ctx | ||||||
).workunit_processor, | ||||||
] | ||||||
|
||||||
def get_report(self) -> StatefulIngestionReport: | ||||||
return self.report | ||||||
|
||||||
def get_workunits_internal(self) -> Iterable[MetadataWorkUnit]: | ||||||
headers = { | ||||||
"Authorization": f"Bearer {self.source_config.service_account_token.get_secret_value()}", | ||||||
"Content-Type": "application/json", | ||||||
} | ||||||
try: | ||||||
response = requests.get( | ||||||
f"{self.source_config.url}/api/search", headers=headers | ||||||
) | ||||||
response.raise_for_status() | ||||||
except requests.exceptions.RequestException as e: | ||||||
self.report.report_failure(f"Failed to fetch dashboards: {str(e)}") | ||||||
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.
Suggested change
|
||||||
return | ||||||
res_json = response.json() | ||||||
for item in res_json: | ||||||
uid = item["uid"] | ||||||
title = item["title"] | ||||||
url_path = item["url"] | ||||||
full_url = f"{self.source_config.url}{url_path}" | ||||||
dashboard_urn = builder.make_dashboard_urn( | ||||||
platform=self.platform, | ||||||
name=uid, | ||||||
platform_instance=self.source_config.platform_instance, | ||||||
) | ||||||
|
||||||
yield from auto_workunit( | ||||||
MetadataChangeProposalWrapper.construct_many( | ||||||
entityUrn=dashboard_urn, | ||||||
aspects=[ | ||||||
DashboardInfoClass( | ||||||
description="", | ||||||
title=title, | ||||||
charts=[], | ||||||
lastModified=ChangeAuditStamps(), | ||||||
externalUrl=full_url, | ||||||
customProperties={ | ||||||
key: str(value) | ||||||
for key, value in { | ||||||
"displayName": title, | ||||||
"id": item["id"], | ||||||
"uid": uid, | ||||||
"title": title, | ||||||
"uri": item["uri"], | ||||||
"type": item["type"], | ||||||
"folderId": item.get("folderId"), | ||||||
"folderUid": item.get("folderUid"), | ||||||
"folderTitle": item.get("folderTitle"), | ||||||
}.items() | ||||||
if value is not None | ||||||
}, | ||||||
), | ||||||
StatusClass(removed=False), | ||||||
], | ||||||
) | ||||||
) |
25 changes: 25 additions & 0 deletions
25
metadata-ingestion/tests/integration/grafana/default-dashboard.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"id": null, | ||
"uid": "default", | ||
"title": "Default Dashboard", | ||
"tags": [], | ||
"timezone": "browser", | ||
"schemaVersion": 16, | ||
"version": 0, | ||
"panels": [ | ||
{ | ||
"type": "text", | ||
"title": "Welcome", | ||
"gridPos": { | ||
"x": 0, | ||
"y": 0, | ||
"w": 24, | ||
"h": 5 | ||
}, | ||
"options": { | ||
"content": "Welcome to your Grafana dashboard!", | ||
"mode": "markdown" | ||
} | ||
} | ||
] | ||
} |
32 changes: 32 additions & 0 deletions
32
metadata-ingestion/tests/integration/grafana/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: '3.7' | ||
|
||
services: | ||
grafana: | ||
image: grafana/grafana:latest | ||
container_name: grafana | ||
ports: | ||
- "3000:3000" | ||
environment: | ||
- GF_SECURITY_ADMIN_PASSWORD=admin | ||
- GF_SECURITY_ADMIN_USER=admin | ||
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning | ||
volumes: | ||
- grafana-storage:/var/lib/grafana | ||
- ./provisioning:/etc/grafana/provisioning | ||
- ./default-dashboard.json:/var/lib/grafana/dashboards/default-dashboard.json | ||
depends_on: | ||
- postgres | ||
|
||
postgres: | ||
image: postgres:13 | ||
container_name: grafana-postgres | ||
environment: | ||
POSTGRES_DB: grafana | ||
POSTGRES_USER: grafana | ||
POSTGRES_PASSWORD: grafana | ||
volumes: | ||
- postgres-storage:/var/lib/postgresql/data | ||
|
||
volumes: | ||
grafana-storage: | ||
postgres-storage: |
18 changes: 18 additions & 0 deletions
18
metadata-ingestion/tests/integration/grafana/grafana_mcps_golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"entityType": "dashboard", | ||
"entityUrn": "urn:li:dashboard:(grafana,default)", | ||
"changeType": "UPSERT", | ||
"aspectName": "status", | ||
"aspect": { | ||
"json": { | ||
"removed": false | ||
} | ||
}, | ||
"systemMetadata": { | ||
"lastObserved": 1720785600000, | ||
"runId": "grafana-test-simple", | ||
"lastRunId": "no-run-id-provided" | ||
} | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
metadata-ingestion/tests/integration/grafana/provisioning/api-keys/api_keys.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
api_keys: | ||
- name: 'example-api-key' | ||
role: 'Admin' |
11 changes: 11 additions & 0 deletions
11
metadata-ingestion/tests/integration/grafana/provisioning/dashboards/dashboard.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: 1 | ||
|
||
providers: | ||
- name: 'default' | ||
orgId: 1 | ||
folder: '' | ||
type: file | ||
disableDeletion: false | ||
updateIntervalSeconds: 10 | ||
options: | ||
path: /var/lib/grafana/dashboards |
12 changes: 12 additions & 0 deletions
12
metadata-ingestion/tests/integration/grafana/provisioning/datasources/datasource.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: 1 | ||
|
||
datasources: | ||
- name: PostgreSQL | ||
type: postgres | ||
access: proxy | ||
url: postgres:5432 | ||
database: grafana | ||
user: grafana | ||
password: grafana | ||
jsonData: | ||
sslmode: disable |
6 changes: 6 additions & 0 deletions
6
...a-ingestion/tests/integration/grafana/provisioning/service_accounts/service_accounts.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
service_accounts: | ||
- name: 'example-service-account' | ||
role: 'Admin' | ||
apiKeys: | ||
- keyName: 'example-api-key' | ||
role: 'Admin' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
general best practice is to create a request.Session and then use that everywhere - should be ok here though since it only makes one request