# OneAgent Plugin for browser process metrics

**URL:** <https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928>\
**Category:** Extensions\
**Tags:** extensions\
**Created:** [July 31, 2024, 4:44pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928 "2024-07-31T16:44:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rseibert1](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rseibert1](https://community.dynatrace.com/u/rseibert1)\
**Post date:** [July 31, 2024, 4:44pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/1 "2024-07-31T16:44:09Z")

</div>

I am trying to make my first python extension. I am having trouble with the below plugin.py (keeps getting KeyError: ‘results\_builder’. I have not had much luck finding examples of what I want to do. Not really finding any doc out there on the ruxit.api&nbsp; I have easily built a similar script that uses the metrics API to ingest but trying to convert that to Oneagent Extension has been challenging. You can see I have been trying several iterations of invoking the self.results\_builder\*.

import psutilfrom ruxit.api.base\_plugin import BasePluginfrom ruxit.api.data import PluginMeasurementimport loggingimport os  
# Set up logging for Windowslog\_file\_path = os.path.join(os.getenv(‘PROGRAMDATA’), ‘dynatrace’, ‘custom\_browser\_metrics.log’)logging.basicConfig(level=logging.DEBUG, filename=log\_file\_path, filemode=‘w’)logger = logging.getLogger()  
class PythonPluginHost(BasePlugin​😞&nbsp; &nbsp; def initialize(self, \*\*kwargs​😞&nbsp; &nbsp; &nbsp; &nbsp; logger.debug(“Plugin initialized”)&nbsp; &nbsp; def query(self, \*\*kwargs​😞&nbsp; &nbsp; &nbsp; &nbsp; #pgi = self.find\_single\_process\_group(pgi\_name(‘plugin\_sdk.demo\_app’))&nbsp; &nbsp; &nbsp; &nbsp; #pgi\_id = pgi.group\_instance\_id&nbsp; &nbsp; &nbsp; &nbsp; #stats\_url = “[http://localhost:8769](http://localhost:8769)”&nbsp; &nbsp; &nbsp; &nbsp; #stats = requests.get(stats\_url, timeout=10).json()&nbsp; &nbsp; &nbsp; &nbsp; results\_builder = self.results\_builder&nbsp; &nbsp; &nbsp; &nbsp; if not results\_builder:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.error(“results\_builder not found in kwargs”)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; browsers = [“chrome”, “msedge”]&nbsp; &nbsp; &nbsp; &nbsp; for browser in browsers:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; metrics = get\_process\_metrics(browser)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if metrics:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpu = metrics[“CPU”]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory = metrics[“Memory”]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.debug(f"Sending metrics for {browser}: CPU={cpu}, Memory={memory}“)  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #self.results\_builder.absolute(key=“custom.browser.cpu”, value=cpu, dimensions={“browser”: browser})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.results\_builder.add\_absolute\_result(PluginMeasurement(key=“custom.browser.cpu”, value=cpu))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #self.results\_builder.absolute(key=“custom.browser.{browser}.cpu”, value=metrics[“CPU”])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #self.results\_builder.absolute(key=“custom.browser.memory\_percent”, value=memory, dimensions={“browser”: browser})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.results\_builder.add\_absolute\_result(PluginMeasurement(key=“custom.browser.memory\_percent”, value=memory))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #self.results\_builder.absolute(key=“custom.browser.{browser}.memory”,value=metrics[“Memory”])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.debug(f"No metrics found for {browser}”)def get\_process\_metrics(process\_name​😞&nbsp; &nbsp; total\_memory = psutil.virtual\_memory().total # Total memory in bytes&nbsp; &nbsp; cpu = 0&nbsp; &nbsp; memory = 0  
&nbsp; &nbsp; for proc in psutil.process\_iter([‘name’, ‘cpu\_percent’, ‘memory\_info’]):&nbsp; &nbsp; &nbsp; &nbsp; if proc.info[‘name’] and process\_name.lower() in proc.info[‘name’].lower():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cpu += proc.cpu\_percent(interval=1) # Allow psutil to measure CPU usage over a short interval&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory += proc.info[‘memory\_info’].rss # Resident Set Size in bytes memory\_percent = (memory / total\_memory) \* 100&nbsp; &nbsp; memory\_percent = (memory / total\_memory) \* 100 &nbsp;# Convert bytes to percentage&nbsp; &nbsp; logger.debug(f"Process: {process\_name}, CPU: {cpu}, Memory: {memory\_percent}")&nbsp; &nbsp; return {“CPU”: round(cpu, 1), “Memory”: round(memory\_percent, 1)} if cpu or memory else None# If running as a script, instantiate and queryif \_\_name\_\_ == “\_\_main\_\_”:&nbsp; &nbsp; plugin = PythonPluginHost()&nbsp; &nbsp; plugin.initialize()&nbsp; &nbsp; plugin.query()The plugin.json file for this:{&nbsp; &nbsp; “name”: “custom.python.browser\_metrics”,&nbsp; &nbsp; “version”: “1.0.0”,&nbsp; &nbsp; “metricGroup”: “custom\_browser.metrics”,&nbsp; &nbsp; “technologies”: [“PYTHON”],&nbsp; &nbsp; “type”: “python”,&nbsp; &nbsp; “entity”: “HOST”,&nbsp; &nbsp; “metrics”: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; “timeseries”: {&nbsp; &nbsp; &nbsp; &nbsp; “key”: “custom.browser.cpu”,&nbsp; &nbsp; &nbsp; &nbsp; “displayname”: “Browser CPU Usage”,&nbsp; &nbsp; &nbsp; &nbsp; “unit”: “Percent”&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; “timeseries”: {&nbsp; &nbsp; &nbsp; &nbsp; “key”: “custom.browser.memory\_percent”,&nbsp; &nbsp; &nbsp; &nbsp; “displayname”: “Browser Memory Usage”,&nbsp; &nbsp; &nbsp; &nbsp; “unit”: “Percent”&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;],&nbsp; &nbsp; “interval”: 60&nbsp; }

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [July 31, 2024, 8:38pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/2 "2024-07-31T20:38:29Z")

</div>

[@rseibert1](https://community.dynatrace.com/user/77155)&nbsp;First of all - and that’s most important, your example uses Extensions 1.0 which will be removed from Dynatrace this October.&nbsp;  
Secondly, you are fetching regular process metrics (CPU/memory) which are available out of the box with OneAgent, also in Infrastructure mode. You might want to setup&nbsp;[Declarative process grouping](https://docs.dynatrace.com/docs/shortlink/process-groups#declare)&nbsp;if your processes are “glued” together. If those metrics do not fit your use case or you want to have another data and this was just an example, have a look at [Extension 2.0](https://docs.dynatrace.com/docs/shortlink/extensions20)&nbsp;, especially at [WMI data source](https://docs.dynatrace.com/docs/shortlink/wmi-extension) or at [Python Data source.](https://docs.dynatrace.com/docs/shortlink/python-extension)  
For Python, I’d recommend starting with this [observability clinic - A Practical Guide to Building Python Based Extensions with Dynatrace.](https://www.youtube.com/watch?v=g-lPIZx66BA)

---

<div class="post-metadata">

**Author:** ![rseibert1](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rseibert1](https://community.dynatrace.com/u/rseibert1)\
**Post date:** [August 6, 2024, 12:49pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/3 "2024-08-06T12:49:01Z")

</div>

Thanks Julius for pointing me down the 2.0 path, I had looked into that earlier but for some reason I thought I could just convert to 2.0 later.

I have built a python extension in 2.0 format and it runs fine with the simulator, I am developing on a system that also has oneagent installed as well as the EEC enabled. When I upload and try to add a configuration for a specific host, I am lost on what to do for an endpoint as I want this to run locally so there is no endpoint/user/passwd configuration. Would I remove remote part of the activation from extension.yaml? Currently it has:

activation:&nbsp; &nbsp; remote:&nbsp; &nbsp; &nbsp; path: activationSchema.json&nbsp; &nbsp; local:&nbsp; &nbsp; &nbsp; path: activationSchema.jsonwhich came from default extension creation.

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [August 6, 2024, 2:29pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/4 "2024-08-06T14:29:35Z")

</div>

You need to have activation in the extension. If this is a OneAgent extension only, you can remove the remote part - this is valid only for extensions executed by the ActiveGate. The extension must have some activationSchema which includes the extension configuration - you can remove all the configuration options in your activationSchema.json (when you generate the project, it generates something for you).

---

<div class="post-metadata">

**Author:** ![rseibert1](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rseibert1](https://community.dynatrace.com/u/rseibert1)\
**Post date:** [August 6, 2024, 8:06pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/5 "2024-08-06T20:06:23Z")

</div>

I think if I am following you I can modify the activationSchema such that it will not require configuration after uploading?  
In attempting to remove the config options I am getting the below error:  
dtcli.utils.ExtensionValidationError: {“error”:{“code”:400,“message”:“Activation schema error: Schema validation failed with Exception: ‘No properties specified for complex type dynatrace.datasource.python:python\_browser\_metrics-localhost’ in Schema ‘template’.”}}

The activationSchema.json is attached let me know what is missing?

 ![ActivationSchema.PNG](https://global.discourse-cdn.com/dynatrace/original/2X/9/9c2fc2b547bb0771cd85320f569f11e17e7e4ee8.png)

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [August 6, 2024, 9:41pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/6 "2024-08-06T21:41:17Z")

</div>

TBH I’m not sure if the schema allows such an empty configuration. Unfortunately, there is no public doc for the activation schema. Maybe&nbsp;[@Mike\_L](https://community.dynatrace.com/user/11520)&nbsp;or&nbsp;[@david\_lopes](https://community.dynatrace.com/user/4044)&nbsp;can help here.

---

<div class="post-metadata">

**Author:** ![Mike\_L](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mike_l/32/11717_2.png) [@Mike\_L](https://community.dynatrace.com/u/Mike_L)\
**Post date:** [August 15, 2024, 10:28am UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/7 "2024-08-15T10:28:26Z")

</div>

It indeed doesn’t support an empty schema. Whenever we need that we just add one dropdown for logging to make it easier to change the logging of the extension if you run into issues.

---

<div class="post-metadata">

**Author:** ![duey\_hoang](https://avatars.discourse-cdn.com/v4/letter/d/a6a055/32.png) [@duey\_hoang](https://community.dynatrace.com/u/duey_hoang)\
**Post date:** [August 21, 2024, 10:16pm UTC](https://community.dynatrace.com/t/oneagent-plugin-for-browser-process-metrics/46928/8 "2024-08-21T22:16:21Z")

</div>

i normally leave the default alone, just add your own configuration after the default in activationschema. If doing local oneagent extension, add your logic in \_main\_.py. After deployed, they are on the configuration page, and you can enter garbage there as endpoint, and it just does not do anything.
