basant307/AI_Governance_Project
048
1# OpenTelemetry HTTP and HTTPS Instrumentation for Node.js2 3[![NPM Published Version][npm-img]][npm-url]4[![Apache License][license-image]][license-image]5 6**Note: This is an experimental package under active development. New releases may include breaking changes.**7 8This module provides automatic instrumentation for [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html).9 10For automatic instrumentation see the11[@opentelemetry/sdk-trace-node](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package.12 13## Installation14 15```bash16npm install --save @opentelemetry/instrumentation-http17```18 19## Supported Versions20 21- Nodejs `>=14`22 23## Usage24 25OpenTelemetry HTTP Instrumentation allows the user to automatically collect telemetry and export it to their backend of choice, to give observability to distributed systems.26 27To load a specific instrumentation (HTTP in this case), specify it in the Node Tracer's configuration.28 29```js30const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');31const {32 ConsoleSpanExporter,33 NodeTracerProvider,34 SimpleSpanProcessor,35} = require('@opentelemetry/sdk-trace-node');36const { registerInstrumentations } = require('@opentelemetry/instrumentation');37 38const provider = new NodeTracerProvider({39 spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())]40});41 42provider.register();43 44registerInstrumentations({45 instrumentations: [new HttpInstrumentation()],46});47 48```49 50See [examples/http](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/http) for a short example.51 52### Http instrumentation Options53 54Http instrumentation has a few [configuration options](https://github.com/open-telemetry/opentelemetry-js/blob/e1ec4026edae53a2dea3a9a604d6d21bb5e8d99f/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L60-L93) available to choose from.55You can set the following:56 57| Options | Type | Description |58| --------------------------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |59| `applyCustomAttributesOnSpan` | `HttpCustomAttributeFunction` | Function for adding custom attributes |60| `requestHook` | `HttpRequestCustomAttributeFunction` | Function for adding custom attributes before request is handled |61| `responseHook` | `HttpResponseCustomAttributeFunction` | Function for adding custom attributes before response is handled |62| `startIncomingSpanHook` | `StartIncomingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in incomingRequest |63| `startOutgoingSpanHook` | `StartOutgoingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in outgoingRequest |64| `ignoreIncomingRequestHook` | `IgnoreIncomingRequestFunction` | Http instrumentation will not trace all incoming requests that matched with custom function |65| `ignoreOutgoingRequestHook` | `IgnoreOutgoingRequestFunction` | Http instrumentation will not trace all outgoing requests that matched with custom function |66| `disableOutgoingRequestInstrumentation` | `boolean` | Set to true to avoid instrumenting outgoing requests at all. This can be helpful when another instrumentation handles outgoing requests. |67| `disableIncomingRequestInstrumentation` | `boolean` | Set to true to avoid instrumenting incoming requests at all. This can be helpful when another instrumentation handles incoming requests. |68| `serverName` | `string` | The primary server name of the matched virtual host. |69| `requireParentforOutgoingSpans` | Boolean | Require that is a parent span to create new span for outgoing requests. |70| `requireParentforIncomingSpans` | Boolean | Require that is a parent span to create new span for incoming requests. |71| `headersToSpanAttributes` | `object` | List of case insensitive HTTP headers to convert to span attributes. Client (outgoing requests, incoming responses) and server (incoming requests, outgoing responses) headers will be converted to span attributes in the form of `http.{request\|response}.header.header_name`, e.g. `http.response.header.content_length` |72 73## Semantic Conventions74 75Prior to version `0.54.0`, this instrumentation created spans targeting an experimental semantic convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md).76 77HTTP semantic conventions (semconv) were stabilized in v1.23.0, and a [migration process](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/http-migration.md#http-semantic-convention-stability-migration) was defined.78`instrumentation-http` versions 0.54.0 and later include support for migrating to stable HTTP semantic conventions, as described below.79The intent is to provide an approximate 6 month time window for users of this instrumentation to migrate to the new HTTP semconv, after which a new minor version will use the *new* semconv by default and drop support for the old semconv.80See the [HTTP semconv migration plan for OpenTelemetry JS instrumentations](https://github.com/open-telemetry/opentelemetry-js/issues/5646).81 82To select which semconv version(s) is emitted from this instrumentation, use the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable.83 84- `http`: emit the new (stable) v1.23.0+ semantics85- `http/dup`: emit **both** the old v1.7.0 and the new (stable) v1.23.0+ semantics86- By default, if `OTEL_SEMCONV_STABILITY_OPT_IN` includes neither of the above tokens, the old v1.7.0 semconv is used.87 88### Attributes collected89 90| v1.7.0 semconv | v1.23.0 semconv | Short Description |91| ------------------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |92| `http.client_ip` | `client.address` | The IP address of the original client behind all proxies, if known |93| `http.flavor` | `network.protocol.version` | Kind of HTTP protocol used |94| `http.host` | `server.address` | The value of the HTTP host header |95| `http.method` | `http.request.method` | HTTP request method |96| `http.request_content_length` | (opt-in, `headersToSpanAttributes`) | The size of the request payload body in bytes. For newer semconv, use the `headersToSpanAttributes:` option to capture this as `http.request.header.content_length`. |97| `http.request_content_length_uncompressed` | (not included) | The size of the uncompressed request payload body after transport decoding. (In semconv v1.23.0 this is defined by `http.request.body.size`, which is experimental and opt-in.) |98| `http.response_content_length` | (opt-in, `headersToSpanAttributes`) | The size of the response payload body in bytes. For newer semconv, use the `headersToSpanAttributes:` option to capture this as `http.response.header.content_length`. |99| `http.response_content_length_uncompressed` | (not included) | The size of the uncompressed response payload body after transport decoding. (In semconv v1.23.0 this is defined by `http.response.body.size`, which is experimental and opt-in.) |100| `http.route` | no change | The matched route (path template). |101| `http.scheme` | `url.scheme` | The URI scheme identifying the used protocol |102| `http.server_name` | `server.address` | The primary server name of the matched virtual host |103| `http.status_code` | `http.response.status_code` | HTTP response status code |104| `http.target` | `url.path` and `url.query` | The URI path and query component |105| `http.url` | `url.full` | Full HTTP request URL in the form `scheme://host[:port]/path?query[#fragment]` |106| `http.user_agent` | `user_agent.original` | Value of the HTTP User-Agent header sent by the client |107| `net.host.ip` | `network.local.address` | Like net.peer.ip but for the host IP. Useful in case of a multi-IP host |108| `net.host.name` | `server.address` | Local hostname or similar |109| `net.host.port` | `server.port` | Like net.peer.port but for the host port |110| `net.peer.ip.` | `network.peer.address` | Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6) |111| `net.peer.name` | `server.address` | Server domain name if available without reverse DNS lookup |112| `net.peer.port` | `server.port` | Server port number |113| `net.transport` | `network.transport` | Transport protocol used |114 115Metrics Exported:116 117- [`http.server.request.duration`](https://github.com/open-telemetry/semantic-conventions/blob/v1.27.0/docs/http/http-metrics.md#metric-httpserverrequestduration)118- [`http.client.request.duration`](https://github.com/open-telemetry/semantic-conventions/blob/v1.27.0/docs/http/http-metrics.md#metric-httpclientrequestduration)119 120### Upgrading Semantic Conventions121 122When upgrading to the new semantic conventions, it is recommended to do so in the following order:123 1241. Upgrade `@opentelemetry/instrumentation-http` to the latest version1252. Set `OTEL_SEMCONV_STABILITY_OPT_IN=http/dup` to emit both old and new semantic conventions1263. Modify alerts, dashboards, metrics, and other processes to expect the new semantic conventions1274. Set `OTEL_SEMCONV_STABILITY_OPT_IN=http` to emit only the new semantic conventions128 129This will cause both the old and new semantic conventions to be emitted during the transition period.130 131## Useful links132 133- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>134- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>135- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]136 137## License138 139Apache 2.0 - See [LICENSE][license-url] for more information.140 141[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions142[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE143[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat144[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-http145[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-http.svg146 