From 5cfaa3f2a01bb83830ed1944d074548b68a081e1 Mon Sep 17 00:00:00 2001 From: Aaron Philipose Date: Mon, 23 Jan 2023 16:12:10 -0500 Subject: [PATCH] Reflattening --- src/get_hashicorp_vault_metrics.py | 43 +++++++++++++++++++++++++----- src/plugin.json | 29 ++++++++++---------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/get_hashicorp_vault_metrics.py b/src/get_hashicorp_vault_metrics.py index 217155f..79f6d58 100644 --- a/src/get_hashicorp_vault_metrics.py +++ b/src/get_hashicorp_vault_metrics.py @@ -9,22 +9,53 @@ "vault.barrier.get": None, } +METRIC_INGEST_ENDPOINT = "/api/v2/metrics/ingest" + class HashicorpVaultPlugin(RemoteBasePlugin): """_summary_: Core Plugin Structure""" def initialize(self, **kwargs): config = kwargs['config'] self.vault_url = config['vault_url'] + self.dt_tenant_url= config['dt_tenant_url'] + if self.dt_tenant_url.endswith("/"): + self.dt_tenant_url = self.dt_tenant_url[:-1] + self.dt_tenant_token = config['dt_tenant_token'] + self.process_group_id = config['process_group_id'] def query(self, **kwargs): """_summary_: Query Vault API""" vault_response = requests.get(url=self.vault_url, timeout=10) vault_response_json = vault_response.json() - group = self.topology_builder.create_group(12668946693571282165, "Hashicorp Vault") - device = group.create_device("vault1", "Vault 1") + metrics = "" + for gauge in vault_response_json['Gauges']: + if 'Value' in gauge: + metric_line = f"{gauge['Name']},dt.entity.process_group={self.process_group_id} gauge,{gauge['Value']}" + metrics= f"{metrics}{metric_line}\n" + + for counter in vault_response_json['Counters']: + if 'Count' in counter: + metric_line = f"{counter['Name']},dt.entity.process_group={self.process_group_id} count,{counter['Count']}" + metrics= f"{metrics}{metric_line}\n" for sample in vault_response_json['Samples']: - # self.results_builder.add_sample(sample) - logger.info("Sample: %s", sample) - device.absolute(key="vault.barrier.get.count", value=sample['count']) - # logger.info("Vault Response: %s", vault_response.text) + if ( + 'Name' in sample and \ + 'Sum' in sample and \ + 'Min' in sample and \ + 'Max' in sample and \ + 'Count' in sample + ): + metric_line = f"{sample['Name']},dt.entity.process_group={self.process_group_id} gauge,min={sample['Min']},max={sample['Max']},sum={sample['Sum']},count={sample['Count']}" + metrics= f"{metrics}{metric_line}\n" + + push_metrics_response = requests.post( + url=f"{self.dt_tenant_url}{METRIC_INGEST_ENDPOINT}", + headers={'Authorization': f'Api-Token {self.dt_tenant_token}', + 'Content-Type': 'text/plain'}, + data=metrics, + timeout=10, + ) + logger.info("DT_API_RESPONSE: %s", push_metrics_response.status_code) + if 400 <= push_metrics_response.status_code < 600: + logger.info("DT_API_RESPONSE TEXT: %s", push_metrics_response.text) diff --git a/src/plugin.json b/src/plugin.json index bb73c08..a4728cf 100644 --- a/src/plugin.json +++ b/src/plugin.json @@ -1,9 +1,8 @@ { - "version": "1.6", - "requiredAgentVersion": "1.90", - "name": "custom.python.hashicorp.vault.v1", + "version": "1.0", + "name": "custom.remote.python.hashicorp_vault_v1", "type": "python", - "entity": "PROCESS_GROUP_INSTANCE", + "entity": "CUSTOM_DEVICE", "metricGroup": "hashicorpvault.metrics", "technologies": ["PYTHON"], "source": { @@ -22,15 +21,11 @@ }, "metrics": [ { + "entity": "CUSTOM_DEVICE", "timeseries": { - "key": "vault.barrier.get.count", - "unit": "Count", - "aggregation": "Count", - "dimensions": [], - "displayname": "Vault Barrier Get Count" - }, - "source": { - "query": "barrier_get[count]" + "key": "rtt", + "unit": "MilliSecond", + "displayname": "Round-trip time" } } ], @@ -54,8 +49,14 @@ "displayName": "Hashicorp Vault URL", "description": "URL of the Hashicorp Vault", "required": true, - "type": "STRING", - "default": "http://localhost:8200" + "type": "STRING" + }, + { + "key": "process_group_id", + "displayName": "Vault Process Group ID", + "description": "Hashicorp Vault Process Group ID", + "required": true, + "type": "STRING" } ] }