Skip to content

Commit b787f70

Browse files
committed
refactor: Make Discovery a DataClass.
And move it out of the fsm.py file. Signed-off-by: Paulo Vital <[email protected]>
1 parent aeb187c commit b787f70

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

src/instana/agent/host.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@
99
import json
1010
import os
1111
from datetime import datetime
12-
from typing import Any, Dict, List, Optional, Union
12+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
1313

1414
import requests
1515
import urllib3
1616
from requests import Response
1717

1818
from instana.agent.base import BaseAgent
1919
from instana.collector.host import HostCollector
20-
from instana.fsm import Discovery, TheMachine
20+
from instana.fsm import TheMachine
2121
from instana.log import logger
2222
from instana.options import StandardOptions
2323
from instana.util import to_json
2424
from instana.util.runtime import get_py_source, log_runtime_env_info
2525
from instana.util.span_utils import get_operation_specifiers
2626
from instana.version import VERSION
2727

28+
if TYPE_CHECKING:
29+
from instana.util.process_discovery import Discovery
30+
2831

2932
class AnnounceData(object):
3033
"""The Announce Payload"""
@@ -176,7 +179,7 @@ def is_agent_listening(
176179

177180
def announce(
178181
self,
179-
discovery: Discovery,
182+
discovery: "Discovery",
180183
) -> Optional[Dict[str, Any]]:
181184
"""
182185
With the passed in Discovery class, attempt to announce to the host agent.

src/instana/fsm.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,23 @@
88
import subprocess
99
import sys
1010
import threading
11-
from typing import TYPE_CHECKING, Any, Callable, Optional
11+
from typing import TYPE_CHECKING, Any, Callable
1212

1313
from fysom import Fysom
1414

1515
from instana.log import logger
1616
from instana.util import get_default_gateway
17+
from instana.util.process_discovery import Discovery
1718
from instana.version import VERSION
1819

1920
if TYPE_CHECKING:
2021
from instana.agent.host import HostAgent
2122

2223

23-
class Discovery:
24-
pid: int = 0
25-
name: Optional[str] = None
26-
args: Optional[List[str]] = None
27-
fd: int = -1
28-
inode: str = ""
29-
30-
def __init__(self, **kwds: Any) -> None:
31-
self.__dict__.update(kwds)
32-
33-
def to_dict(self) -> Dict[str, Any]:
34-
kvs: Dict[str, Any] = dict()
35-
kvs["pid"] = self.pid
36-
kvs["name"] = self.name
37-
kvs["args"] = self.args
38-
kvs["fd"] = self.fd
39-
kvs["inode"] = self.inode
40-
return kvs
41-
42-
4324
class TheMachine:
4425
RETRY_PERIOD = 30
4526
THREAD_NAME = "Instana Machine"
4627

47-
agent: Optional["HostAgent"] = None
48-
fsm = None
49-
timer = None
50-
5128
warnedPeriodic = False
5229

5330
def __init__(self, agent: "HostAgent") -> None:

src/instana/util/process_discovery.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# (c) Copyright IBM Corp. 2025
2+
3+
from dataclasses import dataclass
4+
from typing import List, Optional
5+
6+
7+
@dataclass
8+
class Discovery:
9+
pid: int = 0 # the PID of this process
10+
name: Optional[str] = None # the name of the executable
11+
args: Optional[List[str]] = None # the command line arguments
12+
fd: int = -1 # the file descriptor of the socket associated with the connection to the agent for this HTTP request
13+
inode: str = "" # the inode of the socket associated with the connection to the agent for this HTTP request

tests/agent/test_host.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
from instana.agent.host import AnnounceData, HostAgent
1616
from instana.collector.host import HostCollector
17-
from instana.fsm import Discovery, TheMachine
17+
from instana.fsm import TheMachine
1818
from instana.options import StandardOptions
1919
from instana.recorder import StanRecorder
2020
from instana.singletons import get_agent
2121
from instana.span.span import InstanaSpan
2222
from instana.span_context import SpanContext
23+
from instana.util.process_discovery import Discovery
2324
from instana.util.runtime import is_windows
2425

2526

@@ -715,3 +716,4 @@ def test_is_service_or_endpoint_ignored(self) -> None:
715716

716717
# don't ignore other services
717718
assert not self.agent._HostAgent__is_endpoint_ignored("service3")
719+
assert not self.agent._HostAgent__is_endpoint_ignored("service3")

0 commit comments

Comments
 (0)