Skip to content

feat(metrics): collect the DOCKER_HOST environment variable path #8007

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samcli/cli/global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class GlobalConfig(metaclass=Singleton):
# Env var for injecting dir in integration tests
_DIR_INJECTION_ENV_VAR: str = "__SAM_CLI_APP_DIR"

# Env var used by docker client to specify which socket to use
DOCKER_HOST_ENV_VAR: str = "DOCKER_HOST"

# Static singleton instance

_access_lock: threading.RLock
Expand All @@ -71,6 +74,7 @@ class GlobalConfig(metaclass=Singleton):
_config_data: Optional[Dict[str, Any]]
# config_keys that should be flushed to file
_persistent_fields: List[str]
docker_host: str

def __init__(self):
"""__init__ should only be called once due to Singleton metaclass"""
Expand All @@ -79,6 +83,7 @@ def __init__(self):
self._config_filename = None
self._config_data = None
self._persistent_fields = list()
self.docker_host = os.environ.get(GlobalConfig.DOCKER_HOST_ENV_VAR, "")

@property
def config_dir(self) -> Path:
Expand Down
25 changes: 25 additions & 0 deletions samcli/lib/telemetry/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"""

import logging
import os
import platform
import uuid
from dataclasses import dataclass
from functools import reduce, wraps
from pathlib import Path
from timeit import default_timer
from typing import Any, Dict, Optional
from urllib.parse import urlparse

import click

Expand Down Expand Up @@ -436,6 +438,7 @@ def _add_common_metric_attributes(self):
self._data["installationId"] = self._gc.installation_id
self._data["sessionId"] = self._session_id
self._data["executionEnvironment"] = self._get_execution_environment()
self._data["dockerHost"] = self._get_docker_host()
self._data["ci"] = bool(self._cicd_detector.platform())
self._data["pyversion"] = platform.python_version()
self._data["samcliVersion"] = samcli_version
Expand Down Expand Up @@ -476,6 +479,28 @@ def _get_execution_environment(self) -> str:
return cicd_platform.name
return "CLI"

def _get_docker_host(self) -> str:
"""
Returns the last part of a DOCKER_HOST string. Has conditional logic to properly parse
URLs and local file system paths. If the DOCKER_HOST is not set, returns an empty string.

Examples:
- unix:///var/run/docker.sock -> docker.sock
- tcp://localhost:1234 -> localhost:1234
- /var/run/docker.sock -> docker.sock
"""

parsed = urlparse(self._gc.docker_host)
if not parsed.scheme == '':
if os.path.exists():
# self._gc.docker_host is a file path
return os.path.basename(os.path.normpath(self._gc.docker_host))
else:
# self._gc.docker_host is a URI
return os.path.basename(parsed.path)

return ""


class MetricDataNotList(Exception):
pass
Loading