【APIM】Azure API Management Self‐Host Gateway是否可以把请求的日志发送到Application Insights呢? - LuBu0505/My-Code GitHub Wiki

问题描述

Azure API Management Self-Host Gateway是否可以把请求的日志发送到Application Insights呢?让它和使用Azure上托管的 Gateway一样呢? image.png

这是在APIM门户上配置API,设置的Diagnostics Logs,当选择Application Insights时,就可以把对接口请求时候所携带的Header/Body等信息发送到Applciation Insights中进行存储并分析。

但是,在自建网关在K8S中,确无法发送日志到Applicaiton Insights,有什么办法呢? 

问题解答

因为APIM Self-Host Gateway默认使用Application Insights的Endpoint ( ***https://dc.services.visualstudio.com/ ***) 是Global Application Insights。而且在默认生产的YAML文件中,初始化并没有包含对Application Insights的设置项:

# NOTE: Before deploying to a production environment, please review the documentation -> https://aka.ms/self-hosted-gateway-production
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: testselfhost-env
  labels:
    app: testselfhost
data:
 config.service.endpoint: "xxxxxxxxxx.configuration.azure-api.cn"
  neighborhood.host: "testselfhost-instance-discovery"
  runtime.deployment.artifact.source: "Azure Portal"
  runtime.deployment.mechanism: "YAML"
  runtime.deployment.orchestrator.type: "Kubernetes"
---

所以如果需要发送请求日志到中国区指定的Application Insights中,需要修改YAML文件,在configMap中添加**  logs.applicationinsights.endpoint: "https://dc.applicationinsights.azure.cn/v2/track"   ,** 效果如下图所示: image.png

如上修改后,就能实现本地网关也发送数据到Appliation Insights中。

参考资料

如何将 Azure API 管理与 Azure Application Insights 集成:https://docs.azure.cn/zh-cn/api-management/api-management-howto-app-insights?tabs=rest#enable-application-insights-logging-for-your-api

使用 YAML 将自承载网关部署到 Kubernetes:https://docs.azure.cn/zh-cn/api-management/how-to-deploy-self-hosted-gateway-kubernetes 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!