<p>We have written java application (Job) which reads some files from Azure blob and writes content to Azure Event hub. This is batch job runs at scheduled interval
We have deployed and scheduled the application as Kubernetes CronJob. We are recording events with some details when files are moved from blob to Event hub, but those events are not reflecting in application insight. But we can see events when we run locally from IDE(Eclipse or intellij)</p>
<p>Below is deployment yaml file</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: job-name-1.0.0.0
spec:
  schedule: &quot;*/5 * * * *&quot;
  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: [&quot;java&quot;, &quot;-jar&quot;,&quot;job-name-1.0.0.0.jar&quot;,&quot;$(connection_string)&quot;]
              env:
                - name: connection_string
                  valueFrom:
                    configMapKeyRef:
                      name: job-configmap
                      key: connectionString
              resources:
                limits:
                  cpu: &quot;15&quot;
                requests:
                  cpu: &quot;0.5&quot;
          restartPolicy: Never
</code></pre>
<p>below is the java code used to write event to azure application insigh</p>
<pre><code> TelemetryClient telemetry = new TelemetryClient();
 telemetry.getContext().setInstrumentationKey(instrumentationKey);
 telemetry.getContext().getCloud().setRole(&quot;CloudRoleName&quot;);
 telemetry.trackTrace(&quot;SOME INFORMATION ABOUT JOB&quot;, SeverityLevel.Information);
</code></pre>
<p>Please note we have deployed another Kafka stream job with same code but kind of deployment as <strong>kind: Deployment</strong> in yaml file and the events are flowing into Application insight without any issue we are facing for the <strong>kind: CronJob</strong></p>
<p>Is there any changes we have to do for cron jobs?</p>
<p>Thanks in advance.</p>

chandu ram
<p>It is quite possible that job is ending before TelemetryClient could flush the pending telemetry from buffer. For continuous running job, it's not a problem (like in this case your Kafka stream job), but for scheduled job, execution ends leaving the pending telemetry. To fix this, add below in the code at the end of execution to ensure pending telemetries are written in the channel before execution ends.</p>
<pre class="lang-java prettyprint-override"><code>// here 'telemetry' is the instance of TelemetryClient as per your shared code 
telemetry.flush(); 
</code></pre>
