-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trackMetric do not works #864
Comments
@russosalv can you add more details about the custom metric you are sending?, you should be able to see custom metrics querying customMetrics table |
hi @hectorhdzg ys sure, i'm using nestjs framework i created a injectable that manage the application insight, then it is used in other service or controller. Application Insight service: import { Injectable, Scope } from '@nestjs/common';
import * as appInsights from 'applicationinsights';
@Injectable({ scope: Scope.TRANSIENT })
export class ApplicationInsightUtils {
public telemetryClient: appInsights.TelemetryClient;
public defaultClient: any;
constructor() {
let appInsights = require('applicationinsights');
let liveMetrics = false;
if (process.env.APPINSIGHTS_LIVEMETRICS == 'true') liveMetrics = true;
appInsights
.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
.setAutoDependencyCorrelation(true)
.setAutoCollectRequests(true)
.setAutoCollectPerformance(true, true)
.setAutoCollectExceptions(true)
.setAutoCollectDependencies(true)
.setAutoCollectConsole(true)
.setUseDiskRetryCaching(true)
.setSendLiveMetrics(liveMetrics)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI);
//Sampling if data are a lot
//appInsights.defaultClient.config.samplingPercentage = 33;
appInsights.start();
this.telemetryClient = new appInsights.TelemetryClient();
this.defaultClient = appInsights.defaultClient;
}
} and here is where used: import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
import { createPrismaQueryEventHandler } from 'prisma-query-log';
import { Prisma, PrismaClient } from '@prisma/client';
import { ApplicationInsightUtils } from 'src/utils/applicationInsight.utils';
import * as appInsights from 'applicationinsights';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
/**
*
*/
constructor(private applicationInsight: ApplicationInsightUtils) {
super({
log: [
{
level: 'query',
emit: 'event',
},
],
});
}
async onModuleInit() {
await this.$connect();
const log = createPrismaQueryEventHandler({
format: true,
language: 'sql',
// linesBetweenQueries: 1
});
this.$on<any>('query', (event: Prisma.QueryEvent) => {
log(event);
console.log('Params: ', event.params);
console.log('Duration: ' + event.duration + 'ms');
console.log('\n');
const stringHash = require('string-hash');
const queryMetric = stringHash(event.query);
// this.applicationInsight.telemetryClient.trackMetric({
// name: queryMetric,
// value: event.duration,
// } as appInsights.Contracts.MetricTelemetry);
//Seems that trtack Metric do not works with Node 16
//WorkAround
this.applicationInsight.telemetryClient.trackTrace({
message: `sqlMetric: ${queryMetric}`,
severity: appInsights.Contracts.SeverityLevel.Information,
properties: {
queryMetric: queryMetric,
query: event.query,
params: event.params,
target: event.target,
duration: event.duration,
timestamp: event.timestamp,
},
} as appInsights.Contracts.TraceTelemetry);
});
}
async enableShutdownHooks(app: INestApplication) {
this.$on('beforeExit', async () => {
await app.close();
});
}
} as per comment in the code
i noted also that during execution on log appear a strange error
pls note also that application work inside a docker container, but i have same behaviour aslo running on my machine |
Thanks @russosalv, just one thing I noticed is that you are creating another telemetry client apart from the default one created during "setup" method, this is usually the case when people want to send to multiple Application Insights resources, you can simplify your code a little bit just using the defaultClient where all the configurations are applied, the error you are seeing in logs is because the SDK was not able to reach the ingestion endpoint and disk retry is disabled for that telemetry client, if you use the default one disk retry would be enabled and will try to send data from disk when this happens, can you provide some sample values for the name and value you are sending for the metric?, I wonder if some of these are causing conflicts for some reason. |
Hi @hectorhdzg thx for your suggest; //NOT USE THIS
this.telemetryClient = new appInsights.TelemetryClient();
//USE THIS INSTEAD
this.defaultClient = appInsights.defaultClient; here some metric name and value that i'm sendig |
@russosalv yes please use only the defaultClient to avoid having multiple clients unless you really need them, following code works perfectly for me, can you try to add setInternalLogging to see if there is any warning log in the console. applicationinsights.setup()
.setInternalLogging(true, true)
.start();
applicationinsights.defaultClient.trackEvent({ name: "TestEvent" });
applicationinsights.defaultClient.trackMetric({ name: "TestMetric", value: 123 });
applicationinsights.defaultClient.trackMetric({ name: "1075945211", value: 123 }); |
Hi @hectorhdzg i did some test with .setInternalLogging(true,true), here atached:
|
Thanks for the logs @russosalv, issue with your metrics is that you are sending the name as number instead of string and telemetry is being rejected on the ingestion endpoint, please convert it to string before sending and let me know how it goes. |
Hi @hectorhdzg i tried to chenge it but i'm still having the issue this.applicationInsight.defaultClient.trackMetric({
name: `sqlMetric_${queryMetric}`,
value: event.duration,
} as appInsights.Contracts.MetricTelemetry); |
@russosalv are you getting different errors now in the logs?, the issue before was the following: Please take a look to see if there is some other error coming from ingestion endpoint |
@russosalv please let me know if this is still an issue on your side |
Hi Hector
i still have the issue,
i'm trying to updated the package to latest (if exist)
but currently i'm not receive any metrics on application Insight using
TrackMetric
*Salvatore Alessandro Russo*
***@***.***
Il giorno sab 22 gen 2022 alle ore 01:15 Hector Hernandez <
***@***.***> ha scritto:
… @russosalv <https://github.com/russosalv> please let me know if this is
still an issue on your side
—
Reply to this email directly, view it on GitHub
<#864 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXLWRCE2LKIGYVUH2JSYJ3UXHZJTANCNFSM5HCRTA7Q>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Using application insight in Node 16 enviroment
trackMetric do not works, it's seems that data are sent ot application insight but also waiting ingestion time it do no appear on Azure
The text was updated successfully, but these errors were encountered: