Skip to content

Commit 6856b74

Browse files
authored
Telemetry client id generation (#802)
* feat: telemetry client id generation * feat: add tests
1 parent 8ccdec1 commit 6856b74

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/xpk/core/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
CFG_BUCKET_KEY = 'cluster-state-gcs-bucket'
3131
CLUSTER_NAME_KEY = 'cluster-name'
3232
PROJECT_KEY = 'project-id'
33+
CLIENT_ID_KEY = 'client-id'
3334
ZONE_KEY = 'zone'
3435
KJOB_BATCH_IMAGE = 'batch-image'
3536
KJOB_BATCH_WORKING_DIRECTORY = 'batch-working-directory'
@@ -45,6 +46,7 @@
4546
CFG_BUCKET_KEY,
4647
CLUSTER_NAME_KEY,
4748
PROJECT_KEY,
49+
CLIENT_ID_KEY,
4850
ZONE_KEY,
4951
GKE_ENDPOINT_KEY,
5052
DEPENDENCIES_KEY,

src/xpk/core/telemetry.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import uuid
18+
from .config import xpk_config, CLIENT_ID_KEY
19+
20+
21+
def generate_client_id():
22+
"""Generates Client ID and stores in configuration if not already present."""
23+
if xpk_config.get(CLIENT_ID_KEY) is None:
24+
xpk_config.set(CLIENT_ID_KEY, str(uuid.uuid4()))

src/xpk/core/telemetry_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from .config import xpk_config, CLIENT_ID_KEY
18+
from .telemetry import generate_client_id
19+
20+
21+
def test_generates_client_id_when_its_not_present():
22+
xpk_config.set(CLIENT_ID_KEY, None)
23+
generate_client_id()
24+
assert xpk_config.get(CLIENT_ID_KEY) is not None
25+
26+
27+
def test_does_not_generate_client_id_when_its_present():
28+
client_id = '1337'
29+
xpk_config.set(CLIENT_ID_KEY, client_id)
30+
generate_client_id()
31+
assert xpk_config.get(CLIENT_ID_KEY) == client_id

src/xpk/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
from .parser.core import set_parser
3939
from .core.updates import print_xpk_hello
40+
from .core.telemetry import generate_client_id
4041
from .utils.console import xpk_print
4142
from .utils.execution_context import set_context
4243
################### Compatibility Check ###################
@@ -71,6 +72,7 @@ def main() -> None:
7172
dry_run_value='dry_run' in main_args and main_args.dry_run,
7273
quiet_value='quiet' in main_args and main_args.quiet,
7374
)
75+
generate_client_id()
7476
print_xpk_hello()
7577
main_args.func(main_args)
7678
xpk_print('XPK Done.', flush=True)

0 commit comments

Comments
 (0)