天天看點

【前端監控】fee kafka filebeat部署筆記

前言

  • 本篇是在上一篇nginx打點配置後的基礎上制作。

fee

  • 前面配置了sentry,這sentry相當大,fee是開源的輕量級監控系統。
  • 位址https://github.com/LianjiaTech/fee
  • 先克隆此倉庫
  • 修改sdk下打點伺服器位址:
const feeTarget = 'https://test.com/dig' // 打點伺服器,或Nginx位址
           
  • 然後在sdk裡build下:
cd fee/sdk
npm i --registry=https://registry.npm.taobao.org
npm run build
           
  • 完畢後建個建個demo,把build的sdk弄出來改名sdk.js
  • 整個html模拟項目,通路後會發送給打點nginx。
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>SDK demo</title>
	</head>

	<body>
		<div id="test">測試SDK</div>
		<script src="./sdk.js"></script>
		<script>
			// 模拟uuid
			if (!localStorage.getItem("custom_UUID")) {
				const customUUID = Math.random().toString().slice(-6);
				localStorage.setItem("custom_UUID", customUUID);
			}
			window.dt &&
				dt.set({
					pid: "project_test_id", // [必填]項目id, 由燈塔項目組統一配置設定
					uuid: localStorage.getItem("custom_UUID") || "", // [可選]裝置唯一id, 用于計算uv數&裝置分布. 一般在cookie中可以取到, 沒有uuid可用裝置mac/idfa/imei替代. 或者在storage的key中存入随機數字, 模拟裝置唯一id.
					ucid: "", // [可選]使用者ucid, 用于發生異常時追蹤使用者資訊, 一般在cookie中可以取到, 沒有可傳空字元串

					is_test: false, // 是否為測試資料, 預設為false(測試模式下打點資料僅供浏覽, 不會展示在系統中)
					record: {
						time_on_page: true, // 是否監控使用者線上時長資料, 預設為true
						performance: true, // 是否監控頁面載入性能, 預設為true
						js_error: true, //  是否監控頁面報錯資訊, 預設為true
						// 配置需要監控的頁面報錯類别, 僅在js_error為true時生效, 預設均為true(可以将配置改為false, 以屏蔽不需要上報的錯誤類别)
						js_error_report_config: {
							ERROR_RUNTIME: true, // js運作時報錯
							ERROR_SCRIPT: true, // js資源加載失敗
							ERROR_STYLE: true, // css資源加載失敗
							ERROR_IMAGE: true, // 圖檔資源加載失敗
							ERROR_AUDIO: true, // 音頻資源加載失敗
							ERROR_VIDEO: true, // 視訊資源加載失敗
							ERROR_CONSOLE: true, // vue運作時報錯
							ERROR_TRY_CATCH: true, // 未catch錯誤
							// 自定義檢測函數, 上報前最後判斷是否需要報告該錯誤
							// 回調函數說明
							// 傳入參數 =>
							//            desc:  字元串, 錯誤描述
							//            stack: 字元串, 錯誤堆棧資訊
							// 傳回值 =>
							//            true  : 上報打點請求
							//            false : 不需要上報
							checkErrrorNeedReport: function (desc, stack) {
								return true;
							},
						},
					},

					// 業務方的js版本号, 會随着打點資料一起上傳, 友善區分資料來源
					// 可以不填, 預設為1.0.0
					version: "1.0.0",

					// 對于如同
					// test.com/detail/1.html
					// test.com/detail/2.html
					// test.com/detail/3.html
					// ...
					// 這種頁面來說, 雖然url不同, 但他們本質上是同一個頁面
					// 是以需要業務方傳入一個處理函數, 根據目前url解析出真實的頁面類型(例如: 二手房清單/經紀人詳情頁), 以便燈塔系統對錯誤來源進行分類
					// 回調函數說明
					// 傳入參數 => window.location
					// 傳回值 => 對應的的頁面類型(50字以内, 建議傳回漢字, 友善檢視), 預設是傳回目前頁面的url
					getPageType: function (location) {
						return `${location.host}${location.pathname}`;
					},
				});

			// 通過點選手動建立錯誤
			const testDOM = document.getElementById("test");
			testDOM.addEventListener("click", function () {
				console.log(window.a.b);
			});
		</script>
	</body>
</html>

           

kafka

  • https://kafka.apache.org/
  • 入門文檔
  • 入門文檔2
  • 下載下傳zookeeper:
cd /home/admin
# java
yum install java-11-openjdk-devel -y
# zookeeper
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
           
  • 解壓
tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
cd apache-zookeeper-3.6.2-bin/conf
           
  • 運作
./bin/zkServer.sh start
           
  • 檢視是否成功
netstat -tunlp | egrep 2181
           
  • 配置kafka
cd /home/admin
wget http://mirror.bit.edu.cn/apache/kafka/2.6.0/kafka_2.13-2.6.0.tgz
           
  • 解壓
tar -zxvf kafka_2.13-2.6.0.tgz
cd kafka_2.13-2.6.0/config
           
  • 配置listener 為本機
# vim server.properties
listeners=PLAINTEXT://192.168.199.101:9092
advertised.listeners=PLAINTEXT://192.168.199.101:9092
           
  • 開啟服務
cd ../bin
./kafka-server-start.sh ./../config/server.properties 1>/dev/null 2>&1 &
           
  • 開始消費
./kafka-console-consumer.sh --bootstrap-server 192.168.199.101:9092 --topic fee --from-beginning
           
  • 開另一個視窗生産:
./kafka-console-producer.sh --broker-list 192.168.199.101:9092 --topic fee
> A awesome message from me!
           
  • 看是否能消費到消息,能就Ok。

Filebeat

  • 官網
  • filebeat會作為producer将nginx産生的日志作為消息發送到kafka
  • 下載下傳
sudo rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
           
  • 在/etc/yum.repos.d/目錄下,建立一個elastic.repo檔案,并且添加以下行
[elastic-7.x]
name=Elastic repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
           
  • 安裝
sudo yum install filebeat -y
           
  • 安裝目錄為:/usr/share/filebeat

    配置檔案所在目錄為:/etc/filebeat/filebeat.yml

vim /etc/filebeat/filebeat.yml
           
  • 修改裡面ip
###################### Filebeat Configuration Example #########################

# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html

# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.

# ============================== Filebeat inputs ===============================

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /var/log/nginx/ferms/*.log
    #- c:\programdata\elasticsearch\logs\*

  # Exclude lines. A list of regular expressions to match. It drops the lines that are
  # matching any regular expression from the list.
  #exclude_lines: ['^DBG']

  # Include lines. A list of regular expressions to match. It exports the lines that are
  # matching any regular expression from the list.
  #include_lines: ['^ERR', '^WARN']

  # Exclude files. A list of regular expressions to match. Filebeat drops the files that
  # are matching any regular expression from the list. By default, no files are dropped.
  #exclude_files: ['.gz$']

  # Optional additional fields. These fields can be freely picked
  # to add additional information to the crawled log files for filtering
  #fields:
  #  level: debug
  #  review: 1

  ### Multiline options

  # Multiline can be used for log messages spanning multiple lines. This is common
  # for Java Stack Traces or C-Line Continuation

  # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
  #multiline.pattern: ^\[

  # Defines if the pattern set under pattern should be negated or not. Default is false.
  #multiline.negate: false

  # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
  # that was (not) matched before or after or as long as a pattern is not matched based on negate.
  # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
  #multiline.match: after

# ============================== Filebeat modules ==============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #reload.period: 10s

# ======================= Elasticsearch template setting =======================

setup.template.settings:
  index.number_of_shards: 3
  #index.codec: best_compression
  #_source.enabled: false


# ================================== General ===================================

# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:

# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]

# Optional fields that you can specify to add additional information to the
# output.
#fields:
#  env: staging

# ================================= Dashboards =================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
#setup.dashboards.enabled: false

# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the artifacts.elastic.co
# website.
#setup.dashboards.url:

# =================================== Kibana ===================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  #host: "localhost:5601"

  # Kibana Space ID
  # ID of the Kibana Space into which the dashboards should be loaded. By default,
  # the Default Space will be used.
  #space.id:

# =============================== Elastic Cloud ================================

# These settings simplify using Filebeat with the Elastic Cloud (https://cloud.elastic.co/).

# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:

# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:

# ================================== Outputs ===================================

# Configure what output to use when sending the data collected by the beat.

# ---------------------------- Elasticsearch Output ----------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["localhost:9200"]

  # Protocol - either `http` (default) or `https`.
  #protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  #username: "elastic"
  #password: "changeme"

# ------------------------------ Logstash Output -------------------------------
#output.logstash:
  # The Logstash hosts
  #hosts: ["localhost:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"


# ================================== Logging ===================================

# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
logging.level: debug

# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
#logging.selectors: ["*"]

# ================================== Outputs ===================================

output.kafka:
  hosts: ["192.168.16.121:9092", "xxx.xxx.xxx.xxx:9092","yyy.yyy.yyy.yyy:9092","zzz.zzz.zzz.zzz:9092"]
  topic: "fee"


# ============================= X-Pack Monitoring ==============================
# Filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster.  This requires xpack monitoring to be enabled in Elasticsearch.  The
# reporting is disabled by default.

# Set to true to enable the monitoring reporter.
#monitoring.enabled: false

# Sets the UUID of the Elasticsearch cluster under which monitoring data for this
# Filebeat instance will appear in the Stack Monitoring UI. If output.elasticsearch
# is enabled, the UUID is derived from the Elasticsearch cluster referenced by output.elasticsearch.
#monitoring.cluster_uuid:

# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well.
# Note that the settings should point to your Elasticsearch *monitoring* cluster.
# Any setting that is not set is automatically inherited from the Elasticsearch
# output configuration, so if you have the Elasticsearch output configured such
# that it is pointing to your Elasticsearch monitoring cluster, you can simply
# uncomment the following line.
#monitoring.elasticsearch:

# ============================== Instrumentation ===============================

# Instrumentation support for the filebeat.
#instrumentation:
    # Set to true to enable instrumentation of filebeat.
    #enabled: false

    # Environment in which filebeat is running on (eg: staging, production, etc.)
    #environment: ""

    # APM Server hosts to report instrumentation results to.
    #hosts:
    #  - http://localhost:8200

    # API Key for the APM Server(s).
    # If api_key is set then secret_token will be ignored.
    #api_key:

    # Secret token for the APM Server(s).
    #secret_token:


# ================================= Processors =================================
processors:
  - drop_fields:
      fields: ["@timestamp", "@metadata", "log", "input", "ecs", "host", "agent"]


# ================================= Migration ==================================

# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: true
           
  • 啟動
# 啟動
systemctl start filebeat.service
# 開機啟動
systemctl enable filebeat.service
           
  • 看狀态,此時應該會顯示激活中
# 狀态
systemctl status filebeat.service
# 停止
systemctl stop filebeat.service
           
  • 開啟消費者
./kafka-console-consumer.sh --bootstrap-server=192.168.153.199:9092 --topic fee --from-beginning
           
  • 遇到問題可以配置日志級别debug
# vim /etc/filebeat/filebeat.yml
logging.level: debug
           
tail -f  /var/lib/filebeat/registry/filebeat/log.json
           
  • 會收到filebeat生産的檔案,此時通路上面fee裡寫的網頁,觸發發送給打點伺服器産生日志,filebeat生産消息,kafka拿到消息給消費者,這部分就完成了。