SLS全栈监控数据分析

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
可观测可视化 Grafana 版,10个用户账号 1个月
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 本文通过展示主机监控, 数据库监控, 应用监控帮助用户完成从基础设施到业务层面的全栈监控, 我们在每个示例中使用不同的数据采集和可视化实现方式, 帮助用户全面了解SLS提供的时序监控能力


本文通过展示主机监控, 数据库监控, 应用监控帮助用户完成从基础设施到业务层面的监控, 我们在每个示例中使用不同的数据采集和可视化实现方式, 帮助用户全面了解SLS提供的时序监控能力

监控层次

在我们实施监控时, 服务端监控至少包含以下部分:


41B035AA-3D2F-4AFA-86CB-317D09CE25E3.png



基础设施&网络在云时代绝大多数用户已经不再需要关心, 因此我们主要关心操作系统/数据库&中间件以及应用&业务的监控
SLS拥抱开源, 可以借助成熟的监控软件提供的能力, 如Prometheus, telegraf, Grafana等, 构建灵活的解决方案.
FDE78416-9866-454A-A1CF-BC85C37FA7CF.png

灵活的数据收集方案

例如Prometheus支持众多Exporter, 并且是kubernetes标配, 那么我们可以选择用Prometheus exporter暴露数据, 用Prometheus进行采集, 通过remote write协议写入Metric Store(参见文档: 采集Prometheus监控数据_数据接入_时序存储_日志服务-阿里云), 下文将以Java应用监控为例展示该用法
telegraf同样支持众多采集插件, 因此也可以选择用telegraf进行采集, 并通过influxdb的协议写入Metric Store, 下文将以MySQL监控为例展示该用法
同时SLS的logtail本身也有采集能力, 因此也可以使用logtail进行采集, 例如我们提供的主机监控: 采集主机监控数据_数据接入_时序存储_日志服务-阿里云

查询基础

在展示具体例子之前, 我们先学习一点查询语法作为基础
SLS Metric Store支持使用SQL + PromQL进行查询, 使用方法为使用PromQL函数进行查询, 然后可将该查询作为子查询嵌套完整SQL语法:

SELECT 
    promql_query_range('up', '1m') 
FROM metrics;
SELECT 
    sum(value) 
FROM 
    (SELECT promql_query_range('up', '1m') 
    FROM metrics);

其中promql_query_range的第一个参数就是PromQL, 第二个参数为step, 即时间粒度 在MetricStore查询页面中, 可在Metrics下拉框中选择指标, 会自动生成最简单的查询, 点击预览即可看到图表: image.png

PromQL语法入门

EF9780DC-8969-4BE6-9F33-30E4AB5E59D3.png

例子:

avg(go_gc_duration_seconds{endpoint = “http-metrics”}) by (instance)

完整PromQL语法可查看Prometheus官方文档: https://prometheus.io/docs/prometheus/latest/querying/basics/
SLS支持Prometheus中主要的几个函数, 完整列表见: 时序数据查询分析简介_查询与分析_时序存储_日志服务-阿里云 
单是看语法说明难免有些枯燥, 下面我们就进入实战环节!

主机监控

主机监控我们采用logtail收集操作系统指标, 直接写入Metric Store, 同时我们提供了内置的dashboard做可视化, 它的数据流如下:
 E829C436-0CD1-4269-93BD-1FB55C9FB4EF.png

操作步骤

新建logtail配置: 
DDF1EC82-4BF2-403E-9359-B23ED3B06C21.png

选择主机监控 
FD70BB43-90D1-4AFF-B18F-C1FA9D76745B.png

选择机器组
3530BE05-017A-44EB-9C91-3B54D6240F61.png

确认插件配置 
86C2663F-936A-4703-BD8F-A33666A846CF.png

 IntervalMs代表采集间隔, 默认30s, 可保持默认点击下一步即可完成
创建完成后即可在左边dashboard列表中找到主机监控 
C44D098A-6D25-474D-8264-C83145892CAD.png

稍等1-2分钟即可看到数据产生
 FBE692AE-9BB2-4299-A2E1-067E8EEA503B.png

数据库监控

数据库监控采用telegraf进行采集, 并通过logtail支持的http receiver插件传输数据, logtail将把数据写入metric store 
E5AA131E-00E3-42D9-A113-97938E2117DD.png

telegraf写入logtail走influxdb协议, 因此在telegraf中按照influxdb配置即可
首先我们先创建一个logtail配置, 用于接收telegraf的数据, 新建logtail配置, 选择自定义数据插件
B5C6980C-02BD-40AF-9B6A-9FF71BFDC6B7.png

选择机器组(参照主机监控中的步骤) 输入配置名称, 并粘贴以下内容:

{
    "inputs": [
        {
            "detail": {
                "Format": "influx",
                "Address": ":8476"
            },
            "type": "service_http_server"
        }
    ],
    "global": {
        "AlwaysOnline": true,
        "DelayStopSec": 500
    }
}


E82D6201-7A67-4A62-BD9E-563331CD8237.png

点击下一步即可完成 接着我们开始配置telegraf 修改telegraf.conf, 默认在_etc_telegraf/telegraf.conf, 建议备份原文件, 新建并粘贴以下内容:

# Global tags can be specified here in key="value" format.
[global_tags]
  # dc = "us-east-1" # will tag all metrics with dc=us-east-1
  # rack = "1a"
  ## Environment variables can be used as tags, and throughout the config file
  # user = "$USER"
# Configuration for telegraf agent
[agent]
  ## Default data collection interval for all inputs
  interval = "10s"
  ## Rounds collection interval to 'interval'
  ## ie, if interval="10s" then always collect on :00, :10, :20, etc.
  round_interval = true
  ## Telegraf will send metrics to outputs in batches of at most
  ## metric_batch_size metrics.
  ## This controls the size of writes that Telegraf sends to output plugins.
  metric_batch_size = 1000
  ## Maximum number of unwritten metrics per output.  Increasing this value
  ## allows for longer periods of output downtime without dropping metrics at the
  ## cost of higher maximum memory usage.
  metric_buffer_limit = 10000
  ## Collection jitter is used to jitter the collection by a random amount.
  ## Each plugin will sleep for a random time within jitter before collecting.
  ## This can be used to avoid many plugins querying things like sysfs at the
  ## same time, which can have a measurable effect on the system.
  collection_jitter = "0s"
  ## Default flushing interval for all outputs. Maximum flush_interval will be
  ## flush_interval + flush_jitter
  flush_interval = "10s"
  ## Jitter the flush interval by a random amount. This is primarily to avoid
  ## large write spikes for users running a large number of telegraf instances.
  ## ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
  flush_jitter = "0s"
  ## By default or when set to "0s", precision will be set to the same
  ## timestamp order as the collection interval, with the maximum being 1s.
  ##   ie, when interval = "10s", precision will be "1s"
  ##       when interval = "250ms", precision will be "1ms"
  ## Precision will NOT be used for service inputs. It is up to each individual
  ## service input to set the timestamp at the appropriate precision.
  ## Valid time units are "ns", "us" (or "µs"), "ms", "s".
  precision = ""
  ## Maximum number of rotated archives to keep, any older logs are deleted.
  ## If set to -1, no archives are removed.
  # logfile_rotation_max_archives = 5
  ## Override default hostname, if empty use os.Hostname()
  hostname = ""
  ## If set to true, do no set the "host" tag in the telegraf agent.
  omit_hostname = false
      
###############################################################################
#                            OUTPUT PLUGINS                                   #
###############################################################################
# Configuration for sending metrics to Logtail's InfluxDB receiver
[[outputs.influxdb]]
  ## The full HTTP Logtail listen address
  urls = ["http://127.0.0.1:8476"]
  ## Always be true
  skip_database_creation = true

再在telegraf.d目录中新建mysql.conf文件, 粘贴以下内容:

[[inputs.mysql]]
  ## specify servers via a url matching:
  ##  [username[:password]@][protocol[(address)]]/[?tls=[true|false|skip-verify|custom]]
  ##  see https://github.com/go-sql-driver/mysql#dsn-data-source-name
  ##  e.g.
  ##    servers = ["user:passwd@tcp(127.0.0.1:3306)/?tls=false"]
  ##    servers = ["user@tcp(127.0.0.1:3306)/?tls=false"]
  #
  ## If no servers are specified, then localhost is used as the host.
  servers = ["user:passwd@tcp(127.0.0.1:3306)/?tls=false"]
  metric_version = 2
  ## if the list is empty, then metrics are gathered from all databasee tables
  table_schema_databases = []
  ## gather metrics from INFORMATION_SCHEMA.TABLES for databases provided above list
  gather_table_schema = false
  ## gather thread state counts from INFORMATION_SCHEMA.PROCESSLIST
  gather_process_list = false
  ## gather user statistics from INFORMATION_SCHEMA.USER_STATISTICS
  gather_user_statistics = false
  ## gather auto_increment columns and max values from information schema
  gather_info_schema_auto_inc = false
  ## gather metrics from INFORMATION_SCHEMA.INNODB_METRICS
  gather_innodb_metrics = true
  ## gather metrics from SHOW SLAVE STATUS command output
  gather_slave_status = false
  ## gather metrics from SHOW BINARY LOGS command output
  gather_binary_logs = false
  ## gather metrics from SHOW GLOBAL VARIABLES command output
  gather_global_variables = true
  ## gather metrics from PERFORMANCE_SCHEMA.TABLE_IO_WAITS_SUMMARY_BY_TABLE
  gather_table_io_waits = false
  ## gather metrics from PERFORMANCE_SCHEMA.TABLE_LOCK_WAITS
  gather_table_lock_waits = false
  ## gather metrics from PERFORMANCE_SCHEMA.TABLE_IO_WAITS_SUMMARY_BY_INDEX_USAGE
  gather_index_io_waits = false
  ## gather metrics from PERFORMANCE_SCHEMA.EVENT_WAITS
  gather_event_waits = false
  ## gather metrics from PERFORMANCE_SCHEMA.FILE_SUMMARY_BY_EVENT_NAME
  gather_file_events_stats = false
  ## gather metrics from PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_SUMMARY_BY_DIGEST
  gather_perf_events_statements = false
  ## the limits for metrics form perf_events_statements
  perf_events_statements_digest_text_limit = 120
  perf_events_statements_limit = 250
  perf_events_statements_time_limit = 86400
  ## Some queries we may want to run less often (such as SHOW GLOBAL VARIABLES)
  ##   example: interval_slow = "30m"
  interval_slow = ""
  ## Optional TLS Config (will be used if tls=custom parameter specified in server uri)
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  ## Use TLS but skip chain & host verification
  # insecure_skip_verify = false omit_hostname = false
[[processors.strings]]
  namepass = ["mysql", "mysql_innodb"]
  [[processors.strings.replace]]
    tag = "server"
    old = "127.0.0.1:3306"
    new = "mysql-dev"
  [[processors.strings.replace]]
    tag = "server"
    old = "192.168.1.98:3306"
    new = "mysql-prod"

注意修改servers字段为对应的MySQL连接串 重启telegraf即可:

sudo service telegraf reload
# 或者
sudo systemctl reload telegraf

稍等1-2分钟刷新页面, 选择Metrics, 即可看到数据, MySQL监控暂时未提供预置dashboard, 可自行配置, 后续SLS将对常用数据库和中间件提供默认dashboard模板

应用监控

应用监控中我们以Spring Boot应用为例, 使用Spring Boot Actuator暴露数据, 通过Prometheus采集, 并使用remote write 协议写入Metric Store, 再使用Grafana对接做可视化, 整个数据流如下: 
8273A347-6444-4FC9-80AE-767A5381AB0B.png

首先我们需要引入两个依赖:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.1.3</version>
</dependency>


接着修改spring boot配置, 默认在resources/application.yml, 没有的话请创建:

server:
  port: 8080
spring:
  application:
    name: spring-demo # 修改成您的应用名
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus' # 暴露/actuator/prometheus
  metrics:
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

启动应用, 访问http://localhost:8080/actuator/prometheus, 应该看到如下数据:

# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Eden Space",} 1.77733632E8
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Metaspace",} 3.6880384E7
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Old Gen",} 1.53092096E8
jvm_memory_committed_bytes{application="spring-demo",area="heap",id="PS Survivor Space",} 1.4680064E7
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Compressed Class Space",} 5160960.0
jvm_memory_committed_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 7798784.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="spring-demo",} 0.0
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
jvm_memory_max_bytes{application="spring-demo",area="nonheap",id="Code Cache",} 2.5165824E8
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="spring-demo",} 7010.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="spring-demo",} 24.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# 太长, 后面省略


现在数据已经暴露出来了, 我们需要配置Prometheus进行采集, 修改Prometheus的配置文件:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: "spring-demo"
    metrics_path: "/actuator/prometheus"
    static_configs:
    - targets: ["localhost:8080"]
remote_write:
  - url: "https://cn-zhangjiakou-share.log.aliyuncs.com/prometheus/sls-metric-store-test-cn-zhangjiakou/test-logstore-1/api/v1/write"
  # - url: "https://sls-zc-test-bj-b.cn-beijing-share.log.aliyuncs.com/prometheus/sls-zc-test-bj-b/prometheus-spring/api/v1/write"
    basic_auth:
      username: ${accessKeyId}
      password: ${accessKeySecret}
    # Configures the queue used to write to remote storage.
    queue_config:
      max_samples_per_send: 2048
      batch_send_deadline: 20s
      min_backoff: 100ms
      max_backoff: 5s
      # max_retries: 10

其中scrape_configs是用来采集我们的应用数据的, remote_write部分用于将数据写入Metric Store, 注意替换basic_auth中的username和password为您对应的accessKeyId和accessKeySecret 配置完成后重启Prometheus, 可访问http://${prometheus_域名}/graph选择metric查看是否采集成功 接着我们要配置grafana进行可视化 首先要把我们的Metric Store接入到Grafana的数据源中: 
D8570C50-78A6-4297-84BE-317ED7339E38.png

数据源接入成功后, 就可以配置dashbaord了, 我们已经在grafana.com上传了模板: SLS JVM监控大盘(via MicroMeter) dashboard for Grafana | Grafana Labs 直接在grafana中导入即可: 做侧边栏选择+  Import  粘贴url: https://grafana.com/grafana/dashboards/12856 选择上一步创建的数据源 点击Load 这样就配置完成了, 我们完整的dashboard是这样的:
 B60284E9-AB50-48C1-85A9-6FDDA99A5E42.png

总结

我们首先介绍了SLS时序数据的查询方式, 接着我们通过主机监控, MySQL监控, Spring Boot应用监控三种监控类型向大家分别展示了多种不同的数据接入, 可视化方法, 大家可以根据自身的环境选择最容易使用的方式进行接入, 当数据都存储在SLS上以后, 就可以使用SLS提供的SQL语法, PromQL语法对数据进行分析挖掘, 祝大家使用愉快! 如有任何问题, 可提工单, 或在用户群中反馈(见下放钉钉二维码), 也欢迎关注我们的微信公众号, 会推送实用的使用技巧和最佳实践哦~ 
1597888715255-ba9db769-13c7-483e-b49b-62a8c34a0dc5.png

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
15天前
|
监控 关系型数据库 MySQL
《MySQL 简易速速上手小册》第7章:MySQL监控和日志分析(2024 最新版)
《MySQL 简易速速上手小册》第7章:MySQL监控和日志分析(2024 最新版)
37 3
|
23天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
|
2月前
|
Prometheus 监控 Kubernetes
Kubernetes 集群监控与日志管理实践
【2月更文挑战第29天】 在微服务架构日益普及的当下,Kubernetes 已成为容器编排的事实标准。然而,随着集群规模的扩大和业务复杂度的提升,有效的监控和日志管理变得至关重要。本文将探讨构建高效 Kubernetes 集群监控系统的策略,以及实施日志聚合和分析的最佳实践。通过引入如 Prometheus 和 Fluentd 等开源工具,我们旨在为运维专家提供一套完整的解决方案,以保障系统的稳定性和可靠性。
|
5天前
|
Prometheus 监控 Cloud Native
【Go语言专栏】Go语言中的日志记录与监控
【4月更文挑战第30天】Go语言在软件开发和运维中扮演重要角色,提供灵活的日志记录机制和与多种监控工具的集成。内置`log`包支持基本日志记录,而第三方库如`zap`、`zerolog`和`logrus`则扩展了更多功能。监控方面,Go应用可与Prometheus、Grafana、Jaeger等工具配合,实现系统指标收集、可视化和分布式追踪。健康检查通过HTTP端点确保服务可用性。结合日志和监控,能有效提升Go应用的稳定性和性能。
|
5天前
|
存储 监控 关系型数据库
PHP编写的电脑监控软件:用户登录日志记录与分析
使用PHP编写简单但功能强大的电脑监控软件,记录用户登录日志并进行分析。代码示例展示了如何获取并存储用户IP地址和登录时间到数据库,然后进行登录数据的分析,如计算登录频率和常见登录时间。此外,还介绍了如何通过定时任务自动将监控数据提交到网站,以便实时监控用户活动,提升系统安全性和稳定性。
19 0
|
5天前
|
Prometheus 监控 Kubernetes
Kubernetes 集群的监控与日志管理策略
【4月更文挑战第30天】 在微服务架构日益普及的当下,容器化技术与编排工具如Kubernetes成为了运维领域的重要话题。有效的监控和日志管理对于保障系统的高可用性和故障快速定位至关重要。本文将探讨在Kubernetes环境中实施监控和日志管理的最佳实践,包括选用合适的工具、部署策略以及如何整合这些工具来提供端到端的可见性。我们将重点讨论Prometheus监控解决方案和EFK(Elasticsearch, Fluentd, Kibana)日志管理堆栈,分析其在Kubernetes集群中的应用,并给出优化建议。
|
19天前
|
存储 Prometheus 监控
Flask监控与日志记录:掌握应用运行状况
【4月更文挑战第16天】本文介绍了在Flask应用中实现监控和日志记录的方法,以确保应用稳定性和问题排查。推荐使用Prometheus、Grafana、New Relic或Flask-MonitoringDashboard等工具进行监控,并通过Python的logging模块记录日志。监控集成涉及安装配置工具、添加监控代码,而日志管理则需要集中存储和使用分析工具。安全是关键,要防止未授权访问和数据泄露,避免记录敏感信息。监控和日志记录有助于提升应用性能和用户体验。
|
27天前
|
运维 Prometheus 监控
Kubernetes 集群的监控与日志管理实践
【4月更文挑战第8天】在微服务架构日益普及的背景下,容器化技术成为支撑快速迭代和部署的关键。其中,Kubernetes 作为容器编排的事实标准,承载着服务的稳定性和扩展性。然而,随着集群规模的扩大,如何有效监控和管理集群状态、确保服务的高可用性成为一个挑战。本文将深入探讨 Kubernetes 集群的监控和日志管理策略,从系统资源利用到服务健康检查,再到日志的收集与分析,提供一个全面的运维视角,帮助运维人员构建一个健壮、可观察的 Kubernetes 环境。
19 0
|
2月前
|
监控 数据可视化 算法
上网行为监控管理:利用R编写的数据分析和可视化代码示例
本文介绍了使用R语言进行上网行为监控管理的方法。通过加载和分析日志数据,如示例代码所示,可以了解用户行为。使用ggplot2绘制时间趋势图以洞察用户访问模式,借助caret进行聚类分析以识别异常行为。此外,利用httr包可自动将数据提交至网站,提升管理效率。R语言为网络安全和行为监控提供了强大工具。
92 0
|
2月前
|
Prometheus 监控 Kubernetes
Kubernetes 集群的监控与日志管理实践
【2月更文挑战第31天】 在微服务架构日益普及的今天,容器编排工具如Kubernetes已成为部署、管理和扩展容器化应用的关键平台。然而,随着集群规模的扩大和业务复杂性的增加,如何有效监控集群状态、及时响应系统异常,以及管理海量日志信息成为了运维人员面临的重要挑战。本文将深入探讨 Kubernetes 集群监控的最佳实践和日志管理的高效策略,旨在为运维团队提供一套系统的解决思路和操作指南。
31 0