-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_otlp.py
180 lines (157 loc) · 7.08 KB
/
test_otlp.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import logging
import os
from posixpath import basename, splitext
import random
import time
from prometheus_client import start_http_server
from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
# Service name is required for most backends
resource = Resource(attributes={
SERVICE_NAME: "test_prom"
})
script_name = splitext(basename(__file__))[0]
loglevel = os.environ.get("LOGLEVEL", "INFO").upper()
logging.basicConfig(level=loglevel)
logger = logging.getLogger(script_name)
# Start Prometheus client
#For client to be hosted on a port with particular address
#start_http_server(port=8001, addr="0.0.0.0")
#For client to be hosted on a port with address as node ip
start_http_server(port=8001)
# Initialize PrometheusMetricReader which pulls metrics from the SDK
# on-demand to respond to scrape requests
reader = PrometheusMetricReader()
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)
meter = metrics.get_meter(script_name, provider)
requests_counter = meter.create_counter(
name="requests",
description="number of requests",
unit="1"
)
requests_size = meter.create_histogram(
name="request_size_bytes",
description="size of requests",
unit="byte"
)
# for (number, percent) in enumerate(print(temp)):
# attributes = {"cpu_number": str(number)}
system_cpu_time_gauge = meter.create_observable_gauge(
name="system.cpu.time",
callbacks=[SystemMetricsInstrumentor()._get_system_cpu_time],
description="system cpu time",
unit="seconds",
)
system_cpu_utilization_gauge = meter.create_observable_gauge(
name="system.cpu.utilization",
callbacks=[SystemMetricsInstrumentor()._get_system_cpu_utilization],
description=" system cpu utilization",
unit="seconds",
)
system_memory_usage_gauge = meter.create_observable_gauge(
name="system.memory.usage",
callbacks=[SystemMetricsInstrumentor()._get_system_memory_usage],
description="System memory usage",
unit="seconds",
)
system_memory_utilization_gauge = meter.create_observable_gauge(
name="system.memory.utilization",
callbacks=[SystemMetricsInstrumentor()._get_system_memory_utilization],
description="System memory usage",
unit="seconds",
)
system_swap_usage_gauge = meter.create_observable_gauge(
name="system.swap.usage",
callbacks=[SystemMetricsInstrumentor()._get_system_swap_usage],
description="System memory usage",
unit="seconds",
)
system_swap_utilization_gauge = meter.create_observable_gauge(
name="system.swap.utilization",
callbacks=[SystemMetricsInstrumentor()._get_system_swap_utilization],
description="System memory usage",
unit="seconds",
)
system_disk_io_gauge = meter.create_observable_gauge(
name="system.disk.io",
callbacks=[SystemMetricsInstrumentor()._get_system_disk_io],
description="System memory usage",
unit="seconds",
)
system_disk_operations_gauge = meter.create_observable_gauge(
name="system.disk.operations",
callbacks=[SystemMetricsInstrumentor()._get_system_disk_operations],
description="System memory usage",
unit="seconds",
)
system_disk_time_gauge = meter.create_observable_gauge(
name="system.disk.time",
callbacks=[SystemMetricsInstrumentor()._get_system_disk_time],
description="System disk time",
unit="seconds",
)
system_network_dropped_packets_gauge = meter.create_observable_gauge(
name="system.network.dropped.packets",
callbacks=[SystemMetricsInstrumentor()._get_system_network_dropped_packets],
description="system network dropped packets",
unit="seconds",
)
system_network_packets_gauge = meter.create_observable_gauge(
name="system.network.packets",
callbacks=[SystemMetricsInstrumentor()._get_system_network_packets],
description="",
unit="seconds",
)
system_network_errors_gauge = meter.create_observable_gauge(
name="system.network.errors",
callbacks=[SystemMetricsInstrumentor()._get_system_network_errors],
description="",
unit="seconds",
)
system_network_io_gauge = meter.create_observable_gauge(
name="system.network.io",
callbacks=[SystemMetricsInstrumentor()._get_system_network_io],
description="",
unit="seconds",
)
system_network_connections_gauge = meter.create_observable_gauge(
name="system.network.connections",
callbacks=[SystemMetricsInstrumentor()._get_system_network_connections],
description="",
unit="seconds",
)
runtime_memory_gauge = meter.create_observable_gauge(
name="runtime.memory",
callbacks=[SystemMetricsInstrumentor()._get_runtime_memory],
description="",
unit="seconds",
)
runtime_cpu_time_gauge = meter.create_observable_gauge(
name="runtime.cpu.time",
callbacks=[SystemMetricsInstrumentor()._get_runtime_cpu_time],
description="",
unit="seconds",
)
runtime_gc_count_gauge = meter.create_observable_gauge(
name="runtime.gc_count",
callbacks=[SystemMetricsInstrumentor()._get_runtime_gc_count],
description="",
unit="seconds",
)
staging_attributes = {"environment": "staging"}
testing_attributes = {"environment": "testing"}
logger.info("starting instrumented application...")
try:
while True:
requests_counter.add(random.randint(0, 25), staging_attributes)
requests_size.record(random.randint(0, 300), staging_attributes)
requests_counter.add(random.randint(0, 35), testing_attributes)
requests_size.record(random.randint(0, 100), testing_attributes)
time.sleep(5)
except KeyboardInterrupt:
logger.info("shutting down...")