欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用dotnet-monitor sidecar模式 dump docker運(yùn)行的dotnet程序.

前情概要

隨著容器和云技術(shù)的發(fā)展, 大量的應(yīng)用運(yùn)行在云上的容器中, 它們的好處是毋庸置疑的, 例如極大的提高了我們的研發(fā)部署速度, 快速的擴(kuò)縮容等等, 但是也存在一些小小的問(wèn)題, 例如難以調(diào)試.
基于VM的部署我們可以通過(guò)安全的方式登錄到主機(jī)上做一些你想做的事情, 但是云上的容器那就是不太方便了(目前AWS的ECS已經(jīng)有類似docker exec的方式直接進(jìn)入容器中了, 其他的云未作了解).
但是就算能進(jìn)入容器也不意味著調(diào)試就好做了, 通常來(lái)說(shuō)使用的鏡像都是經(jīng)過(guò)優(yōu)化和精簡(jiǎn)的(如果要調(diào)式可能需要安裝大量的組件).

專注于為中小企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)姚安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

所以, 接下來(lái)介紹一下使用dotnet-monitor 來(lái)內(nèi)存轉(zhuǎn)儲(chǔ)(memory dump)運(yùn)行在容器中的 dotnet 程序.

需要提前知曉的一些知識(shí)點(diǎn)

什么是 dotnet-monitor?

Announcing dotnet monitor in .NET 6 官方博客的原文:

Running a .NET application in diverse environments can make collecting diagnostics artifacts (e.g., logs, traces, process dumps) challenging. dotnet monitor is a tool that provides an unified way to collect these diagnostic artifacts regardless of whether running you’re running on your desktop machine or in a kubernetes cluster.

There are two different mechanisms for collection of these diagnostic artifacts:

An HTTP API for on demand collection of artifacts. You can call these API endpoints when you already know your application is experiencing an issue and you are interested in gathering more information.
Triggers for rule-based configuration for always-on collection of artifacts. You may configure rules to collect diagnostic artifacts when a desired condition is met, for example, collect a process dump when you have sustained high CPU.

google翻譯:

在不同的環(huán)境中運(yùn)行 .NET 應(yīng)用程序會(huì)使收集診斷工件(例如,日志、跟蹤、進(jìn)程轉(zhuǎn)儲(chǔ))具有挑戰(zhàn)性。dotnet monitor是一個(gè)工具,它提供了一種統(tǒng)一的方式來(lái)收集這些診斷工件,無(wú)論您是在臺(tái)式機(jī)上運(yùn)行還是在 kubernetes 集群中運(yùn)行。

收集這些診斷工件有兩種不同的機(jī)制:

用于按需收集工件的HTTP API 。當(dāng)您已經(jīng)知道您的應(yīng)用程序遇到問(wèn)題并且您有興趣收集更多信息時(shí),您可以調(diào)用這些 API 端點(diǎn)。
基于規(guī)則的配置觸發(fā)器,用于始終在線收集工件。您可以配置規(guī)則以在滿足所需條件時(shí)收集診斷工件,例如,當(dāng)您持續(xù)使用高 CPU 時(shí)收集進(jìn)程轉(zhuǎn)儲(chǔ)。

dotnet-monitor工作在什么位置?

借用官方博客中的一張圖說(shuō)明一下dotnet-monitor工作在什么地方

dotnet-monitor是如何能對(duì)我們的目標(biāo)程序進(jìn)行操作的?

dotnet-monitor 可以連接到dotnet運(yùn)行時(shí)公開的一個(gè)診斷端口(diagnostic port)(3.0新提供的新功能), 并通過(guò)自定義協(xié)議(ipc protocol)與運(yùn)行時(shí)交互,

更多調(diào)試知識(shí)和工具例如ETW, eventpipe, lldb, dotnet-trace, dotent-counters 等可以查看 dotnet diagnostics.

目標(biāo)應(yīng)用程序容器準(zhǔn)備

首先, 我們得讓我們被調(diào)試的目標(biāo)程序公開這個(gè)診斷端口, 因?yàn)槟J(rèn)情況下這個(gè)診斷端口只能由運(yùn)行這個(gè)程序的用戶或者root用戶來(lái)訪問(wèn), 顯然sidecar 模式啟動(dòng)的dotnet-monitor是不可能和目標(biāo)程序用的是同一個(gè)用戶的.

未作特別聲明的話, 后文給出的實(shí)驗(yàn)都是基于AWS FargateLinux 配置.

#添加環(huán)境變量
DOTNET_DiagnosticPorts=/my_diagnostic_volume/diag.sock,suspend,connect

/my_diagnostic_volume/diag.sockUnix Domain Socket 文件路徑, my_diagnostic_volume 是掛載的一個(gè)volume.
suspend 意思是讓運(yùn)行時(shí)等待dotnet-monitor 連接進(jìn)來(lái)之后在執(zhí)行托管代碼.
connect 接受dotnet-monitor連接, 詳細(xì)解釋看這里diagnostic ports
上述配置的完整語(yǔ)法結(jié)構(gòu)是 address[,(listen|connect)][,(suspend|nosuspend)]
詳情請(qǐng)查看文檔configure additional diagnostic ports

如果我們的需要dump內(nèi)存文件, 可能會(huì)遇到WriteDumpAsync failed - HRESULT: 0x00000000 issues 1783這樣的錯(cuò)誤, 是因?yàn)闄?quán)限問(wèn)題.
比如我在AWS Fargate中遇到的就是 /dump API 返回400錯(cuò)誤 Write dump failed - HRESULT: 0x00000000, 目標(biāo)程序輸出日志 ptrace(ATTACH, 1) FAILED Operation not permitted.
解決這個(gè)需要吧SYS_PTRACE權(quán)限給到目標(biāo)程序. AWS Fargate 是編輯任務(wù)定義的json文件增加這一部分, docker 啟動(dòng)是通過(guò)增加--cap-add=SYS_PTRACE 參數(shù).

{
    "linuxParameters": {
        "capabilities": {
            "add": [
                "SYS_PTRACE"
            ]
        }
    }
}

最后, 配置目標(biāo)程序容器依賴dotnet-monitor容器, 這樣可以先讓dotnet-monitor容器啟動(dòng)后, 在啟動(dòng)目標(biāo)程序容器.
到此, 目標(biāo)程序容器的配置就完成了, 接下來(lái)配置dotnet-monitor

dotnet-monitor容器準(zhǔn)備

  • 增加 Docker image 作為目標(biāo)容器的sidecar 容器.
  • 暴露端口52323 #dotnet-monitor映射端口.
  • 增加容器啟動(dòng)命令參數(shù) --no-auth # 簡(jiǎn)單粗暴的讓所有的API都不要鑒權(quán).
  • 添加環(huán)境變量
    • DOTNETMONITOR_DiagnosticPort__ConnectionMode=Listen # 必須的.
    • DOTNETMONITOR_DiagnosticPort__EndpointName=/my_diagnostic_volume/diag.sock # 目標(biāo)容器配置的DOTNET_DiagnosticPorts中的address.
    • DOTNETMONITOR_Storage__DumpTempFolder=/my_diagnostic_volume/dump_files # dump內(nèi)存是用的目錄.
    • DOTNETMONITOR_Urls=http://+:52323 # dotnet-monitor要提供服務(wù)在什么端口上. dotnet-monitor默認(rèn)用的就是52323.

詳細(xì)的文檔解釋看這里

至此, 所有的配置就都完成了.

使用dotnet-monitor 來(lái)dump目標(biāo)容器的內(nèi)存文件

Get 請(qǐng)求 /dump endpoint 即可下載內(nèi)存轉(zhuǎn)儲(chǔ)文件.

wget ip:52323/dump -O my_target_application_memory_dump.dmp

當(dāng)前可以用API's列表, 詳情請(qǐng)看這里API's

| Route            | Description                                                        | Version Introduced |
| ---------------- | ------------------------------------------------------------------ | ------------------ |
| /processes       | Gets detailed information about discoverable processes.            | 6.0                |
| /dump            | Captures managed dumps of processes without using a debugger.      | 6.0                |
| /gcdump          | Captures GC dumps of processes.                                    | 6.0                |
| /trace           | Captures traces of processes without using a profiler.             | 6.0                |
| /metrics         | Captures metrics of a process in the Prometheus exposition format. | 6.0                |
| /livemetrics     | Captures live metrics of a process.                                | 6.0                |
| /stacks          | [Experimental] Gets the current callstacks of all .NET threads.    | 7.0                |
| /logs            | Captures logs of processes.                                        | 6.0                |
| /info            | Gets info about dotnet monitor.                                    | 6.0                |
| /operations      | Gets egress operation status or cancels operations.                | 6.0                |
| /collectionrules | Gets the current state of collection rules.                        | 6.3                |

在之后的對(duì)內(nèi)存文件的分析可以使用dotnet-dump, lldb等程序.
更多高級(jí)用法請(qǐng)查看, 例如可以配置內(nèi)存每增加100Mb就觸發(fā)dump內(nèi)存文件.

相關(guān)鏈接

https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/dotnet-monitor
https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/dotnet-dump
https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/diagnostic-port
https://github.com/dotnet/dotnet-monitor/blob/main/README.md
https://devblogs.microsoft.com/dotnet/announcing-dotnet-monitor-in-net-6/

文章名稱:使用dotnet-monitor sidecar模式 dump docker運(yùn)行的dotnet程序.
路徑分享:http://chinadenli.net/article14/dsoidde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、企業(yè)建站、企業(yè)網(wǎng)站制作外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)