Skip to content

Commit

Permalink
added registerInstrumentations
Browse files Browse the repository at this point in the history
  • Loading branch information
silvicir committed Oct 24, 2024
1 parent f5db916 commit fae6c75
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 9 deletions.
3 changes: 3 additions & 0 deletions apps/io-wallet-user-func/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"@azure/cosmos": "^4.1.1",
"@azure/functions": "^4.5.1",
"@azure/identity": "^4.4.1",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/instrumentation": "^0.54.0",
"@opentelemetry/instrumentation-undici": "^0.6.0",
"@pagopa/handler-kit": "^1.1.0",
"@pagopa/handler-kit-azure-func": "^2.0.7",
"@pagopa/ts-commons": "^13.1.1",
Expand Down
100 changes: 94 additions & 6 deletions apps/io-wallet-user-func/src/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,102 @@ app.timer("generateEntityConfiguration", {
schedule: "0 0 */12 * * *", // the function returns a jwt that is valid for 24 hours, so the trigger is set every 12 hours
});

import { HttpRequest, InvocationContext, HttpHandler } from "@azure/functions";

Check failure on line 126 in apps/io-wallet-user-func/src/app/main.ts

View workflow job for this annotation

GitHub Actions / Code Review / js_code_review

Expected "HttpHandler" to come before "InvocationContext"
import {
Attributes,
SpanKind,
SpanOptions,
SpanStatusCode,
TraceFlags,
context,
trace,
Span,

Check failure on line 135 in apps/io-wallet-user-func/src/app/main.ts

View workflow job for this annotation

GitHub Actions / Code Review / js_code_review

Expected "Span" to come before "trace"
SpanContext,
} from "@opentelemetry/api";
import {
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_STATUS_CODE,
SEMATTRS_HTTP_URL,
} from "@opentelemetry/semantic-conventions";

function createAppInsightsWrapper(func: HttpHandler) {
return async (req: HttpRequest, invocationContext: InvocationContext) => {
const startTime = Date.now();

// Extract the trace context from the incoming request
const traceParent = req.headers.get("traceparent");
const parts = traceParent?.split("-");

const parentSpanContext: SpanContext | null =
parts &&
parts.length === 4 &&
parts[1].length === 32 &&
parts[2].length === 16
? {
traceId: parts[1],
spanId: parts[2],

Check failure on line 159 in apps/io-wallet-user-func/src/app/main.ts

View workflow job for this annotation

GitHub Actions / Code Review / js_code_review

Expected "spanId" to come before "traceId"
traceFlags: TraceFlags.NONE,
}
: null;

const activeContext = context.active();

// Set span context as the parent context if any
const parentContext = parentSpanContext
? trace.setSpanContext(activeContext, parentSpanContext)
: activeContext;

const attributes: Attributes = {
[SEMATTRS_HTTP_METHOD]: "HTTP",
[SEMATTRS_HTTP_URL]: req.url,
};

const options: SpanOptions = {
kind: SpanKind.SERVER,
attributes: attributes,

Check failure on line 178 in apps/io-wallet-user-func/src/app/main.ts

View workflow job for this annotation

GitHub Actions / Code Review / js_code_review

Expected "attributes" to come before "kind"
startTime: startTime,
};

const span: Span = trace
.getTracer("ApplicationInsightsTracer")
.startSpan(`${req.method} ${req.url}`, options, parentContext);

let res;
try {
res = await context.with(trace.setSpan(activeContext, span), async () => {

Check failure on line 188 in apps/io-wallet-user-func/src/app/main.ts

View workflow job for this annotation

GitHub Actions / Code Review / js_code_review

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return await func(req, invocationContext);
});
const status = res?.status;
if (status) {
span.setStatus({
code: status < 400 ? SpanStatusCode.OK : SpanStatusCode.ERROR,
});
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, status);
}
} catch (error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : JSON.stringify(error),
});
throw error;
} finally {
span.end(Date.now());
}

return res;
};
}

app.http("getCurrentWalletInstanceStatus", {
authLevel: "function",
handler: GetCurrentWalletInstanceStatusFunction({
jwtValidate: tokenValidate,
telemetryClient: appInsightsClient,
userTrialSubscriptionRepository: trialSystemClient,
walletInstanceRepository,
}),
handler: createAppInsightsWrapper(
GetCurrentWalletInstanceStatusFunction({
jwtValidate: tokenValidate,
telemetryClient: appInsightsClient,
userTrialSubscriptionRepository: trialSystemClient,
walletInstanceRepository,
}),
),
methods: ["GET"],
route: "wallet-instances/current/status",
});
Expand Down
11 changes: 9 additions & 2 deletions apps/io-wallet-user-func/src/infra/azure/appinsights/start.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { metrics, trace } from "@opentelemetry/api";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
import * as ai from "applicationinsights";

registerInstrumentations({
instrumentations: [new UndiciInstrumentation()],
meterProvider: metrics.getMeterProvider(),
tracerProvider: trace.getTracerProvider(),
});

ai.setup(process.env["AppInsightsConnectionString"])
.setAutoCollectRequests(true)
.setAutoCollectExceptions(true)
.setUseDiskRetryCaching(true)
.start();

Expand Down
53 changes: 52 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,16 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/api@npm:^1.0.0, @opentelemetry/api@npm:^1.7.0, @opentelemetry/api@npm:^1.9.0":
"@opentelemetry/api-logs@npm:0.54.0":
version: 0.54.0
resolution: "@opentelemetry/api-logs@npm:0.54.0"
dependencies:
"@opentelemetry/api": "npm:^1.3.0"
checksum: 10c0/877cf75fb6fe37cae9dde1fe9a38202af4d00246bfe3ac29b8c06da5e6e1818a27cb851a0f2c14b3cb7acdebb85ba847144f8523d84279fbb000e88685e66e3b
languageName: node
linkType: hard

"@opentelemetry/api@npm:^1.0.0, @opentelemetry/api@npm:^1.3.0, @opentelemetry/api@npm:^1.7.0, @opentelemetry/api@npm:^1.9.0":
version: 1.9.0
resolution: "@opentelemetry/api@npm:1.9.0"
checksum: 10c0/9aae2fe6e8a3a3eeb6c1fdef78e1939cf05a0f37f8a4fae4d6bf2e09eb1e06f966ece85805626e01ba5fab48072b94f19b835449e58b6d26720ee19a58298add
Expand All @@ -1435,6 +1444,17 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/core@npm:^1.8.0":
version: 1.27.0
resolution: "@opentelemetry/core@npm:1.27.0"
dependencies:
"@opentelemetry/semantic-conventions": "npm:1.27.0"
peerDependencies:
"@opentelemetry/api": ">=1.0.0 <1.10.0"
checksum: 10c0/0f9dea15f146c04debb3f30b133d4d0886b2bae3f3d9696362b2c0b0c4ec89c1da759477f5bce0c50e7c6d57572a7215ec4feb38a6dc2c25ae61089f51f23134
languageName: node
linkType: hard

"@opentelemetry/exporter-logs-otlp-grpc@npm:0.53.0":
version: 0.53.0
resolution: "@opentelemetry/exporter-logs-otlp-grpc@npm:0.53.0"
Expand Down Expand Up @@ -1651,6 +1671,18 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/instrumentation-undici@npm:^0.6.0":
version: 0.6.0
resolution: "@opentelemetry/instrumentation-undici@npm:0.6.0"
dependencies:
"@opentelemetry/core": "npm:^1.8.0"
"@opentelemetry/instrumentation": "npm:^0.53.0"
peerDependencies:
"@opentelemetry/api": ^1.7.0
checksum: 10c0/eafaf213f6da1ad479ee56c8cf884cf27871a40749444784ca03d77fd0c4418164f3fffaec42ad07b81a4613cdf076594e305917698663424a081b50d0c6b481
languageName: node
linkType: hard

"@opentelemetry/instrumentation-winston@npm:^0.40.0":
version: 0.40.0
resolution: "@opentelemetry/instrumentation-winston@npm:0.40.0"
Expand Down Expand Up @@ -1695,6 +1727,22 @@ __metadata:
languageName: node
linkType: hard

"@opentelemetry/instrumentation@npm:^0.54.0":
version: 0.54.0
resolution: "@opentelemetry/instrumentation@npm:0.54.0"
dependencies:
"@opentelemetry/api-logs": "npm:0.54.0"
"@types/shimmer": "npm:^1.2.0"
import-in-the-middle: "npm:^1.8.1"
require-in-the-middle: "npm:^7.1.1"
semver: "npm:^7.5.2"
shimmer: "npm:^1.2.1"
peerDependencies:
"@opentelemetry/api": ^1.3.0
checksum: 10c0/3b80aac8bb64c53c91926b6b19cacea27b98ac3ababa2b36cf02451b168094d0894ef4682ddbd680117c1f748fd0104750a1e0e924f3ef4bcb9e9e1cad37a141
languageName: node
linkType: hard

"@opentelemetry/otlp-exporter-base@npm:0.53.0, @opentelemetry/otlp-exporter-base@npm:^0.53.0":
version: 0.53.0
resolution: "@opentelemetry/otlp-exporter-base@npm:0.53.0"
Expand Down Expand Up @@ -4807,6 +4855,9 @@ __metadata:
"@azure/cosmos": "npm:^4.1.1"
"@azure/functions": "npm:^4.5.1"
"@azure/identity": "npm:^4.4.1"
"@opentelemetry/api": "npm:^1.9.0"
"@opentelemetry/instrumentation": "npm:^0.54.0"
"@opentelemetry/instrumentation-undici": "npm:^0.6.0"
"@pagopa/eslint-config": "npm:^4.0.1"
"@pagopa/handler-kit": "npm:^1.1.0"
"@pagopa/handler-kit-azure-func": "npm:^2.0.7"
Expand Down

0 comments on commit fae6c75

Please sign in to comment.