Skip to content

Commit

Permalink
Reflattening
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Philipose committed Jan 23, 2023
1 parent 30f643b commit 5cfaa3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
43 changes: 37 additions & 6 deletions src/get_hashicorp_vault_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 15 additions & 14 deletions src/plugin.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
],
Expand All @@ -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"
}
]
}
Expand Down

0 comments on commit 5cfaa3f

Please sign in to comment.