-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopentelemetry_gRPC_expoter.py
More file actions
196 lines (152 loc) · 7.97 KB
/
Copy pathopentelemetry_gRPC_expoter.py
File metadata and controls
196 lines (152 loc) · 7.97 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
import time
import argparse
import logging
import sys
import random
import json
# 로깅 설정
# logging.basicConfig(
# level=logging.INFO,
# format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
# handlers=[
# logging.StreamHandler(sys.stdout)
# ]
# )
# logging.basicConfig(
# format='%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
# datefmt='%Y-%m-%dT%H:%M:%S',
# level=logging.INFO
# )
# ''' including path and funtions of code for logging'''
# logging.basicConfig(
# # format='%(asctime)s,%(msecs)d %(levelname)-8s [%(pathname)s:%(lineno)d in ' \
# # 'function %(funcName)s] %(message)s',
# format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s]' \
# ' [%(funcName)s] %(message)s',
# datefmt='%Y-%m-%d:%H:%M:%S',
# level=logging.INFO
# )
class opentelemetry_client:
def __init__(self, otel_host):
logging.basicConfig(
# format='%(asctime)s,%(msecs)d %(levelname)-8s [%(pathname)s:%(lineno)d in ' \
# 'function %(funcName)s] %(message)s',
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s]' \
' [%(funcName)s] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
level=logging.INFO
)
self.otel_host = otel_host
self.resource = Resource.create(
attributes={
"service.name": "my-python-app",
"service.namespace": "production",
}
)
def push_trace_metric_via_grpc(self):
''' Here is an example of how to configure the OTLP gRPC exporter for traces in Python '''
logging.info(f"otel_host : {self.otel_host}")
# Set up the tracer provider
provider = TracerProvider(resource=self.resource)
trace.set_tracer_provider(provider)
# Configure the OTLP gRPC exporter
# By default, it sends to localhost:4317, the standard OTLP gRPC port
# otlp_exporter = OTLPSpanExporter()
otlp_exporter = OTLPSpanExporter(endpoint="{}:4317".format(self.otel_host), insecure=True)
# Register the exporter with the tracer provider
provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
# Get a tracer and create a span
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my_span"):
logging.info("Doing some work in the span...")
time.sleep(1)
def push_prometheus_via_grpc(self):
''' Push a metric to the otel server via gRPC'''
logging.info(f"otel_host : {self.otel_host}")
try:
# 1. OTLP 엑스포터 설정
# Prometheus 직접 전송 시 엔드포인트: http://<prometheus-host>:9090/api/v1/otlp/v1/metrics
'''
# Configure the OTLP exporter
# By default, OTLP uses gRPC on port 4317 or HTTP on port 4318
# The endpoint parameter specifies the collector URL
'''
exporter = OTLPMetricExporter(endpoint="http://{}:14318/v1/metrics".format(self.otel_host))
# 2. MetricReader 및 MeterProvider 설정
# Configure the Metric Reader
# This reader will collect and export metrics every 60 seconds (default interval)
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)
# 3. 메트릭 생성 및 기록
meter = metrics.get_meter("example.meter")
# counter = meter.create_counter(
# name="python_request_total",
# description="Total number of requests",
# unit="1"
# )
# Create a synchronous gauge
'''
# HELP background_noise_level_dB The background noise level
# TYPE background_noise_level_dB gauge
background_noise_level_dB{environment="staging",from="python",job="unknown_service",method="gRPC",otel_scope_name="example.meter",otel_scope_schema_url="",otel_scope_version="",room="server_room"} 697
'''
background_noise_gauge = meter.create_gauge(
name="background_noise_level",
unit="dB",
description="The background noise level",
)
# 메트릭 값 증가시키기
# logging.info("Sending metrics to OTLP endpoint...")
# Set the value directly
# counter.add(1, {"environment": "staging", "from" : "python", "method": "GET"})
# background_noise_gauge.set(75, attributes={"room": "server_room"})
# while True:
# rand_num = random.randint(1, 1000)
# # counter.add(rand_num, {"environment": "staging", "method": "GET"})
# background_noise_gauge.set(rand_num, attributes={"room": "server_room", "environment": "staging", "from" : "python", "method": "gRPC"})
# logging.info("Sending metrics [{}] to OTLP endpoint.. ".format(rand_num))
# time.sleep(30)
rand_num = random.randint(1, 1000)
background_noise_gauge.set(rand_num, attributes={"room": "server_room", "environment": "staging", "from" : "python", "method": "gRPC"})
return {
"func" : sys._getframe().f_code.co_name,
"status" : "Success"
}
except Exception as e:
logging.error(e)
return {
"func" : sys._getframe().f_code.co_name,
"status" : "Failed"
}
if __name__ == '__main__':
''' docker run -p 4317:4317 -p 4318:4318 -v /path/to/your/config.yaml:/etc/otelcol/config.yaml otel/opentelemetry-collector:latest '''
'''
# HTTP 사용 시
pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
# gRPC 사용 시
pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc
'''
''' pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc opentelemetry-exporter-otlp-proto-http --trusted-host pypi.org --trusted-host files.pythonhosted.org'''
''' pip install "urllib3<2" when occuring the issue for OpenSSL'''
# I'm currently exporting metrics from my application via OTLP to an OpenTelemetry collector. On the OpenTelemetry collector I am using the Prometheus exporter to expose a /metrics endpoint on the collector.
# Ensure your OpenTelemetry Collector is running and configured to receive OTLP gRPC metrics. The default endpoint for gRPC reception is 0.0.0.0:4317.
parser = argparse.ArgumentParser(description="Running this service allows us to send and expose the metrics using this script with otel gRPC")
parser.add_argument('-otel_host', '--otel_host', dest='otel_host', default="localhost", help='otel_host')
args = parser.parse_args()
if args.otel_host:
otel_host = args.otel_host
''' trace'''
opentelemetry_client(otel_host).push_trace_metric_via_grpc()
''' metrics '''
ack = opentelemetry_client(otel_host).push_prometheus_via_grpc()
logging.info(json.dumps(ack, indent=2))