Skip to content

Commit

Permalink
Feature/opentelemetry (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraliv13 committed Mar 6, 2023
1 parent 0411c8d commit 1ced2f8
Show file tree
Hide file tree
Showing 30 changed files with 1,114 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/many-worms-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@totalsoft/opentelemetry-instrumentation-ws': major
'@totalsoft/pino-opentelemetry': major
'@totalsoft/pino-multitenancy': minor
---

Added pino-opentelemetry package to include logs in opentelemetry tracing spans. Added tenantCodeMixin in pino-multitenancy to make the tenant code available as a log property. Added opentelemetry-instrumentation-ws package (instrumentation for "ws" library).
6 changes: 6 additions & 0 deletions .changeset/wise-sheep-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@totalsoft/pino-multitenancy': minor
'@totalsoft/pino-opentelemetry': patch
---

Opentelemetry Pino Transport + TenantCode Mixin
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
1,
{
"args": "after-used",
"argsIgnorePattern": "^_"
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-use-before-define": "off",
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-instrumentation-ws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.ts
5 changes: 5 additions & 0 deletions packages/opentelemetry-instrumentation-ws/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
package-lock.json
coverage
__tests__
__mocks__
4 changes: 4 additions & 0 deletions packages/opentelemetry-instrumentation-ws/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @totalsoft/opentelemetry-instrumentation-ws

## 1.0.0

43 changes: 43 additions & 0 deletions packages/opentelemetry-instrumentation-ws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# opentelemetry-instrumentation-ws

This package provides opentelemetry instrumentation for ws library.

## Installation

```javascript
npm i @totalsoft/opentelemetry-instrumentation-ws
```

or

```javascript
yarn add @totalsoft/opentelemetry-instrumentation-ws
```
## Usage
```javascript
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { WSInstrumentation } = require('@totalsoft/opentelemetry-instrumentation-ws');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
instrumentations: [
new WSInstrumentation(),
],
});
```

## Config

The `ws` instrumentation has few options available to choose from. You can set the following:

| Options | Type | Description |
| ------------------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `generateSendSpans` | boolean | should the tracing library add spans for each sent message. Default: false |
| `generateReceiveSpans` | boolean | should the tracing library add spans for each incoming message. Default: false |
| `sendHook` | (span: Span, hookInfo: HookInfo) => void | hook for adding custom attributes to the ws send span when a websocket sends a message |
| `closeHook` | (span: Span, hookInfo: HookInfo) => void | hook for adding custom attributes to the ws close span when a websocket is imperatively closed |
| `handleUpgradeHook` | (span: Span, hookInfo: HookInfo) => void | hook for adding custom attributes to the ws.Server handleUpgrade span when a socket is opened against a server |
| `maxMessageLength` | number | max message length in the message attribute. "..." is added to the end when the message is truncated. Set 0 to omit the message attribute. Default: 1022. |
7 changes: 7 additions & 0 deletions packages/opentelemetry-instrumentation-ws/__tests__/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--
Copyright (c) TotalSoft.
This source code is licensed under the MIT license.
-->

# Issue when testing instrumentation with jest:
https://github.com/facebook/jest/issues/11295
58 changes: 58 additions & 0 deletions packages/opentelemetry-instrumentation-ws/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@totalsoft/opentelemetry-instrumentation-ws",
"description": "Opentelemetry instrumentation for ws",
"version": "1.1.0",
"author": "TotalSoft",
"packageManager": "yarn@3.2.0",
"type": "commonjs",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"composite": true,
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/osstotalsoft/rocket-toolkit.git",
"directory": "packages//opentelemetry-instrumentation-ws"
},
"keywords": [
"correlation"
],
"scripts": {
"build": "run version:update && run clean && tsc --project tsconfig.build.json",
"lint": "eslint src/*.ts",
"clean": "rimraf dist",
"test": "jest --collectCoverage --passWithNoTests --verbose --silent=false --runInBand",
"version:update": "node ./scripts/version-update.js"
},
"devDependencies": {
"@jest/types": "^28.1.0",
"@opentelemetry/core": "^1.9.1",
"@opentelemetry/instrumentation": "^0.34.0",
"@opentelemetry/semantic-conventions": "^1.9.1",
"@types/jest": "^27.5.1",
"@types/node": "^17.0.32",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.4",
"jest": "^28.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.3",
"typescript": "^4.7.2",
"ws": "^8.5.0"
},
"dependencies": {
"@opentelemetry/api": "^1.4.0",
"@opentelemetry/instrumentation-http": "^0.35.0",
"is-promise": "^4.0.0"
},
"peerDependencies": {
"ws": "^8.5.0"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"verbose": true
},
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

const fs = require('fs')
const path = require('path')

const appRoot = process.cwd()

const packageJsonUrl = path.resolve(`${appRoot}/package.json`)
const pjson = require(packageJsonUrl)

const content = `
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.mitations under the License.
// this is autogenerated file, see scripts/version-update.js
export const VERSION = '${pjson.version}'
`

const fileUrl = path.join(appRoot, 'src', 'version.ts')

fs.writeFileSync(fileUrl, content)
5 changes: 5 additions & 0 deletions packages/opentelemetry-instrumentation-ws/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.

export * from './instrumentation'
export * from './types'
Loading

0 comments on commit 1ced2f8

Please sign in to comment.