AZURE:当使用 kubernetes CronJob 安排作业时,事件不会流入 Azure Application Insight

AZURE: Events are not flowing into Azure Application insight when job is scheduled using kubernetes CronJob

我们编写了 java 应用程序(作业),它从 Azure blob 读取一些文件并将内容写入 Azure 事件中心。这是按预定时间间隔 运行 秒的批处理作业 我们已将应用程序部署并安排为 Kubernetes CronJob。当文件从 blob 移动到事件中心时,我们正在记录带有一些详细信息的事件,但这些事件并未反映在应用程序洞察力中。但是当我们从 IDE(Eclipse 或 intellij)

本地 运行 时,我们可以看到事件

下面是部署yaml文件

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: job-name-1.0.0.0
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          nodeSelector:
            agentpool: agentpoolname
          containers:
            - name: job-name-0
              image: opsregistry.azurecr.io/job-name:v1.0.0.0
              imagePullPolicy: Always
              command: ["java", "-jar","job-name-1.0.0.0.jar","$(connection_string)"]
              env:
                - name: connection_string
                  valueFrom:
                    configMapKeyRef:
                      name: job-configmap
                      key: connectionString
              resources:
                limits:
                  cpu: "15"
                requests:
                  cpu: "0.5"
          restartPolicy: Never

下面是 java 代码,用于将事件写入 azure 应用程序 insigh

 TelemetryClient telemetry = new TelemetryClient();
 telemetry.getContext().setInstrumentationKey(instrumentationKey);
 telemetry.getContext().getCloud().setRole("CloudRoleName");
 telemetry.trackTrace("SOME INFORMATION ABOUT JOB", SeverityLevel.Information);

请注意,我们已经部署了另一个 Kafka 流作业,其代码相同,但部署类型为 kind: Deployment 在 yaml 文件中,事件正在流入 Application insight 中,我们没有任何问题种类:CronJob

我们必须对 cron 作业做任何更改吗?

提前致谢。

很有可能作业在 TelemetryClient 可以从缓冲区中清除挂起的遥测数据之前结束。对于连续的 运行 作业,这不是问题(就像在本例中您的 Kafka 流作业),但对于计划的作业,执行结束时留下未决的遥测。要解决此问题,请在执行结束时在代码中添加以下内容,以确保在执行结束之前将挂起的遥测数据写入通道。

// here 'telemetry' is the instance of TelemetryClient as per your shared code 
telemetry.flush();