-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huangyongwen
committed
May 23, 2024
1 parent
0d06e37
commit 120e282
Showing
14 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://www.cnblogs.com/lccsdncnblogs/p/17517383.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
官方对Python接入opentelemetry的说明: | ||
|
||
https://opentelemetry.io/docs/languages/python/ | ||
|
||
## 指标、链路接入成功 | ||
|
||
其中指标Metrics、链路Trace 都有详细的介绍了。 | ||
|
||
我在本地都接入数据也都成功上报了,但是唯独没有找到关于 日志Log 的说明。 | ||
|
||
官方也说明了,日志目前还是 开发 中。 | ||
|
||
 | ||
|
||
|
||
|
||
那这就尴尬,指标、链路都有数据了,没有日志可不行。 | ||
|
||
## 日志怎么办? | ||
|
||
这里opentelemetry也提供了一种通用的解决方案,就是类似于 filebeat那种,直接扫描指定的日志路径,然后收集日志。这里可以参考我的另外一篇文章。 | ||
|
||
以上是一种解决方案。 | ||
|
||
后来我看了下[opentelemetry-python](https://github.com/open-telemetry/opentelemetry-python)的仓库,它的介绍是有关于 Log的试验的: | ||
|
||
| Signal | Status | Project | | ||
| ------- | ------------ | ------- | | ||
| Traces | Stable | N/A | | ||
| Metrics | Stable | N/A | | ||
| Logs | Experimental | N/A | | ||
|
||
|
||
|
||
接着看,大家猜我发现了什么? | ||
|
||
仓库的代码,它在一个[example](https://github.com/open-telemetry/opentelemetry-python/blob/main/docs/examples/logs/example.py)的使用例子里面,有关于log的使用demo: | ||
|
||
 | ||
|
||
安装我本地试了下,竟然成功了。 | ||
|
||
这样日志直接以otel的协议就发送给 otel-collector 了,这种方案就更简单了。 | ||
|
||
|
||
|
||
整合Metrics、Trace、Log 的核心代码如下: | ||
|
||
```python | ||
# metrics | ||
from opentelemetry import metrics | ||
from opentelemetry import trace | ||
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter as OTLPMetricExporterGRPC | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPTraceExporterGRPC | ||
from opentelemetry.metrics import NoOpMeterProvider | ||
from opentelemetry.sdk.metrics import * | ||
from opentelemetry.sdk.metrics._internal.aggregation import AggregationTemporality | ||
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader | ||
from opentelemetry.sdk.metrics.view import * | ||
from opentelemetry.sdk.metrics.view import View | ||
from opentelemetry.sdk.resources import * | ||
from opentelemetry.sdk.trace import TracerProvider, Tracer | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
from opentelemetry.trace import NoOpTracerProvider | ||
# log | ||
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter as OTLPLogExporterGRPC | ||
from opentelemetry import _logs | ||
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler | ||
from opentelemetry.sdk._logs._internal.export import BatchLogRecordProcessor | ||
|
||
# begin | ||
#endpoint 是 OTLP exporters grpc 协议的 receivers | ||
endpoint = "http://localhost:4317" | ||
metrics_exporter = OTLPMetricExporterGRPC(endpoint=endpoint) | ||
|
||
reader = PeriodicExportingMetricReader(exporter=metrics_exporter, export_interval_millis=15, | ||
export_timeout_millis=5) | ||
|
||
# 创建 Prometheus 导出器 | ||
# Start Prometheus client | ||
|
||
# start_http_server(port=8073, addr="localhost") | ||
# reader = PrometheusMetricReader() | ||
config = get_fcop_config() | ||
metrics_attributes = { | ||
SERVICE_NAMESPACE: config.tenant_code, | ||
SERVICE_NAME: config.app_namespace, | ||
SERVICE_INSTANCE_ID: config.application_code, | ||
CLUSTER_TYPE: config.cluster_type, | ||
REGION_CODE: config.region_code, | ||
HOST_NAME: Utils.get_hostname() | ||
} | ||
metrics_attributes.update(CommonTag.get_common_tag()) | ||
resource = Resource.create(metrics_attributes) | ||
# metrics | ||
meter_provider = MeterProvider( | ||
resource=resource, metric_readers=[reader] | ||
, views=[ | ||
change_bucket_view | ||
] | ||
) | ||
metrics.set_meter_provider(meter_provider) | ||
global _meter | ||
_meter = metrics.get_meter(name="meter-sdk") | ||
|
||
# trace | ||
trace_exporter = OTLPTraceExporterGRPC(endpoint=endpoint, timeout=10, | ||
compression=CompressionAlgorithm.gzip | ||
) | ||
|
||
trace_processor = BatchSpanProcessor(span_exporter=trace_exporter, | ||
max_queue_size=5120, | ||
max_export_batch_size=1024, | ||
schedule_delay_millis=2000, | ||
export_timeout_millis=10000) | ||
trace_provider = TracerProvider(resource=resource) | ||
trace_provider.add_span_processor(trace_processor) | ||
trace.set_tracer_provider(trace_provider) | ||
global _tracer | ||
_tracer = trace.get_tracer(instrumenting_module_name="tracer-sdk") | ||
|
||
# log | ||
logger_provider = LoggerProvider(resource=resource) | ||
_logs.set_logger_provider(logger_provider) | ||
log_exporter = OTLPLogExporterGRPC(endpoint=endpoint, timeout=10, insecure=True) | ||
log_record_processor = BatchLogRecordProcessor(exporter=log_exporter, max_queue_size=5120, | ||
max_export_batch_size=1024, | ||
schedule_delay_millis=2000, | ||
export_timeout_millis=10000) | ||
logger_provider.add_log_record_processor(log_record_processor) | ||
|
||
handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider) | ||
# Attach OTLP handler to root logger | ||
logging.getLogger().addHandler(handler) | ||
``` | ||
|
||
|
||
|
||
关于 opentelemetry log 模块的使用,仅支持 python 的 `logging` 日志库,像其他的日志框架和库,比如:`loguru`、`structlog`、`logbook` 均不支持日志拦截上报。 | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
opentelemetry提供了一种类似于 filebeat 的日志收集插件:**filelogreceiver** | ||
|
||
简单来说,你只需要配置日志路径即可。 | ||
|
||
|
||
|
||
官方说明:https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/filelogreceiver | ||
|
||
|
||
|
||
这种就是通用的日志收集方案,它和语言无关,opentelemetry 只关心日志在哪里,具体怎么收集,正则?单行?改写?过滤?这些能力都支持。 | ||
|
||
|
||
|
||
|
||
|
||
一般来说,常用两种日志切割方案: | ||
|
||
- 单行全文 | ||
- 多行正则 | ||
|
||
|
||
|
||
单行全文最简单了,只需要配置日子里路径即可: | ||
|
||
```yaml | ||
filelog: | ||
'include': | ||
- '/otel-file-log/data/log/app/*.log' | ||
'include_file_path': !!bool 'true' | ||
'operators': [] | ||
``` | ||
单行全文 需要注意的是,这里的时间是 sidecar日志收集的时间,我们其实更需要的是 日志真正记录的时间。 | ||
多行正则就不用担心这个问题,我们可以通过正则,把日志真正记录的时间给提取出来。 | ||
假如我本地的日志如下: | ||
```shell | ||
2024-05-15 16:19:51,823 [WARNING] [6de580dbb64922369c7a359fef697e25] - [166132] - [MainThread] - [otel_sdk_config.py ][125] : use NoOpTracerProvider | ||
``` | ||
在正则测试平台可以写一下正则表达式: | ||
 | ||
我本地 collector sidecar 的 `config.yaml` 配置如下(正则多行): | ||
|
||
```yaml | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: 0.0.0.0:4317 | ||
http: | ||
endpoint: 0.0.0.0:4318 | ||
filelog: | ||
start_at: beginning | ||
include: | ||
- /otel-file-log/data/log/app/*.log | ||
include_file_path: true | ||
multiline: | ||
line_start_pattern: '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d+' | ||
operators: | ||
- regex: '(?P<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d+) \[(?P<log_level>\w+)\s*\] \[(?P<trace_id>\w*)\] - \[(?P<process_id>\d+)\] - \[(?P<thread_name>\S+)\] - \[(?P<class_name>\S+)\s*\]\[(?P<line>\d+)\s*\] : (?P<msg>[\s\S]*)' | ||
severity: | ||
parse_from: attributes.log_level | ||
timestamp: | ||
layout: '%Y-%m-%d %H:%M:%S,%L' | ||
layout_type: strptime | ||
parse_from: attributes.log_time | ||
type: regex_parser | ||
- field: attributes.log_time | ||
type: remove | ||
- field: attributes.log_level | ||
type: remove | ||
processors: | ||
memory_limiter: | ||
check_interval: 5s | ||
limit_percentage: 85 | ||
spike_limit_percentage: 25 | ||
batch/metrics: | ||
timeout: 200ms | ||
send_batch_size: 8192 | ||
send_batch_max_size: 8192 | ||
batch/traces: | ||
timeout: 500ms | ||
send_batch_size: 9216 | ||
send_batch_max_size: 9216 | ||
batch/logs: | ||
timeout: 1000ms | ||
send_batch_size: 7168 | ||
send_batch_max_size: 7168 | ||
resourcedetection: | ||
detectors: [env] | ||
override: false | ||
resourcedetection/sys: | ||
detectors: [system] | ||
override: false | ||
exporters: | ||
logging: | ||
verbosity: detailed | ||
prometheus: | ||
endpoint: "0.0.0.0:8073" | ||
resource_to_telemetry_conversion: | ||
enabled: false | ||
loadbalancing: | ||
routing_key: "traceID" | ||
protocol: | ||
otlp: | ||
tls: | ||
insecure: true | ||
resolver: | ||
dns: | ||
hostname: trace.gateway.collector | ||
interval: 10s | ||
timeout: 8s | ||
kafka: | ||
brokers: | ||
- log.gateway.collector:9092 | ||
protocol_version: 2.1.0 | ||
encoding: otlp_json | ||
timeout: 5s | ||
sending_queue: | ||
enabled: true | ||
num_consumers: 10 | ||
queue_size: 5000 | ||
producer: | ||
max_message_bytes: 8388608 | ||
required_acks: 0 | ||
compression: lz4 | ||
topic: mop-dev-test | ||
extensions: | ||
health_check: | ||
endpoint: 0.0.0.0:13133 | ||
zpages: | ||
endpoint: 0.0.0.0:55679 | ||
service: | ||
extensions: [health_check, zpages] | ||
pipelines: | ||
metrics: | ||
receivers: [otlp] | ||
processors: [memory_limiter, batch/metrics] | ||
exporters: [prometheus] | ||
traces: | ||
receivers: [otlp] | ||
processors: [memory_limiter, batch/traces] | ||
exporters: [loadbalancing] | ||
logs: | ||
receivers: [filelog] | ||
processors: [memory_limiter, resourcedetection, resourcedetection/sys, batch/logs] | ||
exporters: [kafka] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
早期的原始架构,没有考虑到日志的记录。 | ||
|
||
通常开发人员直接登入线上服务器查看日志文件。 | ||
|
||
 | ||
|
||
这种在架构简单,日志量少,单点部署的应用,是没什么问题的。 | ||
|
||
如果日志量大,加上分布式部署,缺点明显: | ||
|
||
- 每次都要登入服务器,需要通过IP定位到是哪台机器,否则只能一台一台查看 | ||
- 日志量大的时候,查询很慢,tail、cat 等等命令可能会导致磁盘IO高 | ||
- 日志文件格式不规范,可能保存时间较短被删除、压缩,也可能一直保存导致磁盘空间满 | ||
|
||
|
||
|
||
目前行业内的日志架构基本上都是采取ELK。 | ||
|
||
|
||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
https://www.zhihu.com/question/300830746/answer/2836522148 | ||
|
||
IDEA的plugins插件就不在这里介绍了,有很多文章和答案都有分享过了。 | ||
|
||
|
||
作为一名老司机程序员,例举一些 | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GPT升级:https://xiaowen.cc/219.html |