UNPKG

@azure/eventgrid

Version:
57 lines 3.23 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.cloudEventDistributedTracingEnricherPolicyName = exports.ContentTypeHeaderName = exports.TraceStateHeaderName = exports.TraceParentHeaderName = exports.CloudEventBatchContentType = void 0; exports.cloudEventDistributedTracingEnricherPolicy = cloudEventDistributedTracingEnricherPolicy; exports.CloudEventBatchContentType = "application/cloudevents-batch+json; charset=utf-8"; exports.TraceParentHeaderName = "traceparent"; exports.TraceStateHeaderName = "tracestate"; exports.ContentTypeHeaderName = "Content-Type"; /** * The programmatic identifier of the cloudEventDistributedTracingEnricherPolicy. */ exports.cloudEventDistributedTracingEnricherPolicyName = "cloudEventDistributedTracingEnricherPolicy"; /** * cloudEventDistributedTracingEnricherPolicy is a policy which adds distributed tracing information * to a batch of cloud events. It does so by copying the `traceparent` and `tracestate` properties * from the HTTP request into the individual events as extension properties. * * This will only happen in the case where an event does not have a `traceparent` defined already. This * allows events to explicitly set a traceparent and tracestate which would be respected during "multi-hop * transmition". * * See https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md * for more information on distributed tracing and cloud events. */ function cloudEventDistributedTracingEnricherPolicy() { return { name: exports.cloudEventDistributedTracingEnricherPolicyName, async sendRequest(request, next) { const traceparent = request.headers.get(exports.TraceParentHeaderName); const tracestate = request.headers.get(exports.TraceStateHeaderName); if (request.headers.get(exports.ContentTypeHeaderName) === exports.CloudEventBatchContentType && typeof request.body === "string" && traceparent) { // per the cloud event batched content type we know the body is an array encoded in JSON. const parsedBody = JSON.parse(request.body); for (const item of parsedBody) { // When using the distributed tracing extension, the "traceparent" is a required property // and "tracestate" is optional. This means if an item already has a "traceparent" property // we should not stomp over it. Well formed events will not have a "tracestate" without // also having a "traceparent" so there's no need to guard against that case. if (typeof item !== "object" || item.traceparent) { continue; } item.traceparent = traceparent; if (tracestate) { item.tracestate = tracestate; } } request.body = JSON.stringify(parsedBody); } return next(request); }, }; } //# sourceMappingURL=cloudEventDistrubtedTracingEnricherPolicy.js.map