-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSourcePlusPlus.py
More file actions
164 lines (145 loc) · 6.81 KB
/
Copy pathSourcePlusPlus.py
File metadata and controls
164 lines (145 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import json
import os
import ssl
import sys
import time
import uuid
import yaml
from skywalking import config, agent
from vertx import EventBus
from sourceplusplus import __version__
from .control.LiveInstrumentRemote import LiveInstrumentRemote
from .models.command.LiveInstrumentCommand import LiveInstrumentCommand
from .models.instrument.common.LiveInstrumentType import LiveInstrumentType
class SourcePlusPlus(object):
def get_config_value(self, env, default, true_default):
env_value = os.getenv(env)
if env_value is not None:
return env_value
elif default is not None:
return default
else:
return true_default
def __init__(self, args: dict = None):
if args is None:
args = {}
probe_config_file = os.getenv("SPP_PROBE_CONFIG_FILE", "spp-probe.yml")
probe_config = {}
if os.path.exists(probe_config_file):
probe_config = yaml.full_load(open(probe_config_file, "r"))
# ensure probe_config has required keys
if probe_config.get("spp") is None:
probe_config["spp"] = {}
if probe_config.get("skywalking") is None:
probe_config["skywalking"] = {}
if probe_config["skywalking"].get("collector") is None:
probe_config["skywalking"]["collector"] = {}
if probe_config["skywalking"].get("agent") is None:
probe_config["skywalking"]["agent"] = {}
# set default values
probe_config["spp"]["probe_id"] = self.get_config_value(
"SPP_PROBE_ID", probe_config["spp"].get("probe_id"), str(uuid.uuid4())
)
probe_config["spp"]["platform_host"] = self.get_config_value(
"SPP_PLATFORM_HOST", probe_config["spp"].get("platform_host"), "localhost"
)
probe_config["spp"]["platform_port"] = self.get_config_value(
"SPP_PLATFORM_PORT", probe_config["spp"].get("platform_port"), 5450
)
probe_config["spp"]["verify_host"] = str(self.get_config_value(
"SPP_TLS_VERIFY_HOST", probe_config["spp"].get("verify_host"), True
)).lower() == "true"
probe_config["spp"]["disable_tls"] = str(self.get_config_value(
"SPP_DISABLE_TLS", probe_config["spp"].get("disable_tls"), False
)).lower() == "true"
probe_config["skywalking"]["agent"]["service_name"] = self.get_config_value(
"SPP_SERVICE_NAME", probe_config["skywalking"]["agent"].get("service_name"), "spp"
)
skywalking_host = self.get_config_value("SPP_SKYWALKING_HOST", "localhost", "localhost")
skywalking_port = self.get_config_value("SPP_SKYWALKING_PORT", 11800, 11800)
probe_config["skywalking"]["collector"]["backend_service"] = self.get_config_value(
"SPP_SKYWALKING_BACKEND_SERVICE",
probe_config["skywalking"]["collector"].get("backend_service"),
skywalking_host + ":" + str(skywalking_port)
)
for key, val in args.items():
tmp_config = probe_config
loc = key.split(".")
for i in range(len(loc)):
if tmp_config.get(loc[i]) is None:
tmp_config[loc[i]] = {}
if i == len(loc) - 1:
tmp_config[loc[i]] = val
else:
tmp_config = tmp_config[loc[i]]
self.probe_config = probe_config
self.instrument_remote = None
def attach(self):
config.init(
collector_address=self.probe_config["skywalking"]["collector"]["backend_service"],
service_name=self.probe_config["skywalking"]["agent"]["service_name"],
log_reporter_active=True,
force_tls=self.probe_config["spp"]["disable_tls"] is False,
log_reporter_formatted=False
)
agent.start()
ca_data = None
if self.probe_config["spp"]["disable_tls"] is False \
and self.probe_config["spp"].get("probe_certificate") is not None:
ca_data = "-----BEGIN CERTIFICATE-----\n" + \
self.probe_config["spp"]["probe_certificate"] + \
"\n-----END CERTIFICATE-----"
ssl_ctx = ssl.create_default_context(cadata=ca_data)
ssl_ctx.check_hostname = self.probe_config["spp"]["verify_host"]
if self.probe_config["spp"]["disable_tls"] is True:
ssl_ctx = None
elif ssl_ctx.check_hostname is True:
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
else:
ssl_ctx.verify_mode = ssl.CERT_NONE
eb = EventBus(
host=self.probe_config["spp"]["platform_host"], port=self.probe_config["spp"]["platform_port"],
ssl_context=ssl_ctx
)
eb.connect()
self.__send_connected(eb)
self.instrument_remote = LiveInstrumentRemote(eb)
def __send_connected(self, eb: EventBus):
probe_metadata = {
"language": "python",
"probe_version": __version__,
"python_version": sys.version,
"service": config.service_name,
"service_instance": config.service_instance
}
# add hardcoded probe meta data (if present)
if self.probe_config["spp"].get("probe_metadata") is not None:
for key, val in self.probe_config["spp"].get("probe_metadata").items():
probe_metadata[key] = val
# send probe connected event
reply_address = str(uuid.uuid4())
eb.send(address="spp.platform.status.probe-connected", body={
"probeId": self.probe_config["spp"]["probe_id"],
"connectionTime": round(time.time() * 1000),
"meta": probe_metadata
}, reply_handler=lambda msg: self.__register_remotes(eb, reply_address, msg["body"]["value"]))
def __register_remotes(self, eb, reply_address, status):
eb.unregister_handler(reply_address)
eb.register_handler(
address="spp.probe.command.live-breakpoint-remote:" + self.probe_config["spp"]["probe_id"],
handler=lambda msg: self.instrument_remote.handle_instrument_command(
LiveInstrumentCommand.from_json(json.dumps(msg["body"])), LiveInstrumentType.BREAKPOINT
)
)
eb.register_handler(
address="spp.probe.command.live-log-remote:" + self.probe_config["spp"]["probe_id"],
handler=lambda msg: self.instrument_remote.handle_instrument_command(
LiveInstrumentCommand.from_json(json.dumps(msg["body"])), LiveInstrumentType.LOG
)
)
eb.register_handler(
address="spp.probe.command.live-meter-remote:" + self.probe_config["spp"]["probe_id"],
handler=lambda msg: self.instrument_remote.handle_instrument_command(
LiveInstrumentCommand.from_json(json.dumps(msg["body"])), LiveInstrumentType.METER
)
)