Azure Data Factory管道失败时借助Batch Service发送邮件教程求助
Azure Data Factory管道失败邮件告警方案(非Logic Apps版)
方案一:Azure Functions + SMTP 实现告警
- 创建HTTP触发的Azure Function,编写代码实现SMTP邮件发送逻辑
示例Python代码:import smtplib from email.mime.text import MIMEText import azure.functions as func def main(req: func.HttpRequest) -> func.HttpResponse: try: # 从请求体获取管道失败信息 req_body = req.get_json() pipeline_name = req_body.get('pipelineName') run_id = req_body.get('runId') error_msg = req_body.get('errorMessage') # 配置SMTP信息 smtp_server = 'smtp.example.com' smtp_port = 587 sender_email = 'alert@example.com' sender_password = 'your_password' receiver_email = 'admin@example.com' # 构造邮件内容 subject = f"ADF管道运行失败告警:{pipeline_name}" body = f"管道名称:{pipeline_name}\n运行ID:{run_id}\n错误信息:{error_msg}" msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender_email msg['To'] = receiver_email # 发送邮件 with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(sender_email, sender_password) server.send_message(msg) return func.HttpResponse("邮件发送成功", status_code=200) except Exception as e: return func.HttpResponse(f"发送失败:{str(e)}", status_code=500) - 在ADF管道中设置失败分支:添加Web活动,将Function的HTTP触发URL作为请求地址,请求方法设为POST,请求体中传入管道名称、运行ID、错误信息等关键数据
- 配置ADF的管道运行失败监控:可以通过ADF的触发器(如管道运行失败事件触发),或者直接在管道的每个活动失败分支添加该Web活动
方案二:Azure Batch Service 执行邮件发送脚本
- 创建Azure Batch账户、计算池(选择合适的虚拟机规格)和作业
- 编写邮件发送脚本(以PowerShell为例):
param( [string]$PipelineName, [string]$RunId, [string]$ErrorMessage ) $smtpServer = "smtp.example.com" $smtpPort = 587 $senderEmail = "alert@example.com" $senderPassword = ConvertTo-SecureString "your_password" -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($senderEmail, $senderPassword) $receiverEmail = "admin@example.com" $subject = "ADF管道运行失败告警:$PipelineName" $body = @" 管道名称:$PipelineName 运行ID:$RunId 错误信息:$ErrorMessage "@ Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -Credential $credential -From $senderEmail -To $receiverEmail -Subject $subject -Body $body -UseSsl - 将脚本上传到Azure存储账户,配置Batch作业的任务指向该脚本,并传入管道失败相关参数
- 在ADF管道的失败分支添加Azure Batch活动,触发预先创建的Batch作业,传递管道名称、运行ID、错误信息等参数
方案三:ADF + 自定义虚拟机脚本(轻量替代)
- 部署一台Azure虚拟机,配置SMTP客户端并编写邮件发送脚本
- 在ADF管道失败分支添加SSH活动,远程执行虚拟机上的脚本,传入管道失败信息
- 脚本逻辑可参考方案二的PowerShell代码,确保虚拟机能访问SMTP服务器
内容的提问来源于stack exchange,提问作者f_to
相关产品推荐
相关产品推荐

