Add way to track env variables.

Bug:b/265929888
Change-Id: Ibf24e9aae7bff1d9af36c15fc7f9bdad9dbba526
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4209320
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Joanna Wang <jojwang@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
diff --git a/metrics.py b/metrics.py
index 11e43c6..9d1b585 100644
--- a/metrics.py
+++ b/metrics.py
@@ -32,6 +32,9 @@
 
 DEFAULT_COUNTDOWN = 10
 
+# TODO(b/265929888): Remove this variable when dogfood is over.
+DEPOT_TOOLS_ENV = ['DOGFOOD_STACKED_CHANGES']
+
 INVALID_CONFIG_WARNING = (
     'WARNING: Your metrics.cfg file was invalid or nonexistent. A new one will '
     'be created.'
@@ -225,6 +228,12 @@
     self._collecting_metrics = True
     self.add('metrics_version', metrics_utils.CURRENT_VERSION)
     self.add('command', command_name)
+    environment_variables = [
+        '%s=%s' % (env, os.environ.get(env)) for env in DEPOT_TOOLS_ENV
+        if env in os.environ
+    ]
+    if environment_variables:
+      self.add('env_variables', ','.join(environment_variables))
     try:
       start = time.time()
       result = func(*args, **kwargs)