Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnydgreen committed May 14, 2023
0 parents commit 974be10
Show file tree
Hide file tree
Showing 25 changed files with 1,210 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .githooks/precommit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#/usr/bin/env sh

npm run pre-commit
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.log
.DS_Store
node_modules
build
.turbo
.nyc_output
package-lock.json
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.0.0
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["rome.rome"]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"crossmodule", "jaegertracing", "linkinator", "opentelemetry", "otlp",
"teardown", "Treeware", "zipkin"
]
}
129 changes: 129 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behaviour that contributes to a positive environment for our
community include:

- Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
- Focusing on what is best not just for us as individuals, but for the overall
community

Examples of unacceptable behaviour include:

- The use of sexualized language or imagery, and sexual attention or advances of
any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or email address,
without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behaviour and will take appropriate and fair corrective action in
response to any behaviour that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behaviour may be
reported to the community leaders responsible for enforcement at
hello[@]jonnydgreen.com. All complaints will be reviewed and investigated
promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behaviour deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behaviour was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series of
actions.

**Consequence**: A warning with consequences for continued behaviour. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behaviour.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behaviour, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within the
community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.

Community Impact Guidelines were inspired by
[Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Jonny Green

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# @nifty-lil-tricks/monitoring

**UNRELEASED**

[![Latest Version](https://img.shields.io/npm/v/@nifty-lil-tricks/monitoring?style=flat-square)](https://www.npmjs.com/package/@nifty-lil-tricks/monitoring)
[![GitHub License](https://img.shields.io/github/license/jonnydgreen/nifty-lil-tricks-monitoring?style=flat-square)](https://raw.githubusercontent.com/jonnydgreen/nifty-lil-tricks-monitoring/main/LICENSE)

A selection of useful utilities for all things monitoring and
[OpenTelemetry](https://opentelemetry.io/).

## Installation

**`@opentelemetry/api` package is a peer-dependency, and it should be installed
separately.**

```shell
npm install @nifty-lil-tricks/monitoring @opentelemetry/api
```

## Features

- [Monitoring decorator](#monitoring-decorator) that wraps all methods of a class in an
[OpenTelemetry Span](https://opentelemetry.io/docs/concepts/glossary/#span).

**Official docs coming soon!**

### Monitoring Decorator

#### Pre-requisites

Ensure [OpenTelemetry tracing is set-up](https://github.com/open-telemetry/opentelemetry-js/tree/main/api#trace-your-application). See example [setup](https://github.com/jonnydgreen/nifty-lil-tricks-monitoring/blob/main/examples/basic.ts).

#### Wrap all methods of a class in a span

```typescript
import { Monitor } from "@nifty-lil-tricks/monitoring";
import { promisify } from "node:util";

@Monitor()
class Service {
async hello(): Promise<void> {
// Do work
await promisify(setTimeout)(500);

// Do nested work
await this.nested();
}

async nested(): Promise<void> {
// Do work
await promisify(setTimeout)(1000);
}
}
```

## Examples

Examples can be found [here](https://github.com/jonnydgreen/nifty-lil-tricks-monitoring/blob/main/examples/basic.ts).

## Useful links

- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
- For help or feedback on this project, join us in [GitHub Discussions](https://github.com/jonnydgreen/nifty-lil-tricks-monitoring/discussions)

## License

Nifty lil tricks packages are 100% free and open-source, under the
[MIT license](https://github.com/jonnydgreen/nifty-lil-tricks-monitoring/blob/main/LICENSE).

This package is [Treeware](https://treeware.earth). If you use it in production,
then we ask that you
[**buy the world a tree**](https://plant.treeware.earth/jonnydgreen/nifty-lil-tricks-monitoring)
to thank us for our work. By contributing to the Treeware forest you’ll be
creating employment for local families and restoring wildlife habitats.
53 changes: 53 additions & 0 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
context,
diag,
DiagConsoleLogger,
DiagLogLevel,
trace,
} from "@opentelemetry/api";
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
import { Resource } from "@opentelemetry/resources";
import {
BasicTracerProvider,
ConsoleSpanExporter,
SimpleSpanProcessor,
} from "@opentelemetry/sdk-trace-base";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { promisify } from "node:util";
import { Monitor } from "../src";

// Setting the default Global logger to use the Console
// And optionally change the logging level (Defaults to INFO)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const consoleExporter = new ConsoleSpanExporter();
// const exporter = new OTLPTraceExporter()
const provider = new BasicTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "basic-example",
}),
});
context.setGlobalContextManager(new AsyncLocalStorageContextManager());
provider.addSpanProcessor(new SimpleSpanProcessor(consoleExporter));
provider.register();

@Monitor()
class Service {
hello(): void {}

async helloAsync(): Promise<void> {
await promisify(setTimeout)(500);
await this.nestedAsync();
await promisify(setTimeout)(500);
}

async nestedAsync(): Promise<void> {
await promisify(setTimeout)(1000);
}
}

const a = new Service();
const tracer = trace.getTracer("example-basic-tracer-node");
const parentSpan = tracer.startSpan("basic-root");
const ctx = trace.setSpan(context.active(), parentSpan);
context.with(ctx, () => a.helloAsync()).finally(() => parentSpan.end());
58 changes: 58 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@nifty-lil-tricks/monitoring",
"description": "Nifty lil monitoring utilities",
"version": "0.1.0",
"author": "Jonny Green",
"license": "MIT",
"repository": "jonnydgreen/nifty-lil-tricks-monitoring",
"keywords": ["monitoring", "opentelemetry", "nodejs", "tracing"],
"main": "build/src/index.js",
"module": "build/esm/src/index.js",
"types": "build/src/index.d.ts",
"scripts": {
"clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"build": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"test": "tap --ts test/**/*.test.ts",
"test:watch": "tap --ts -w test/**/*.test.ts",
"docs": "typedoc",
"docs:deploy": "gh-pages --dist docs/out",
"docs:test": "linkinator docs/out --silent && linkinator docs/*.md *.md --markdown --silent",
"start": "docker run --name jaeger -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 -e COLLECTOR_OTLP_ENABLED=true -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 4317:4317 -p 4318:4318 -p 14250:14250 -p 14268:14268 -p 14269:14269 -p 9411:9411 jaegertracing/all-in-one:1.44",
"lint": "rome check .",
"lint:fix": "rome check --apply .",
"format:check": "rome format .",
"format": "rome format . --write",
"pre-commit": "npm run lint && npm run format:check && npm run build && npm run test"
},
"browser": {
"./src/platform/index.ts": "./src/platform/browser/index.ts",
"./build/esm/platform/index.js": "./build/esm/platform/browser/index.js",
"./build/src/platform/index.js": "./build/src/platform/browser/index.js"
},
"files": [
"src/**/*.ts", "test/**/*.ts", "examples/**/*.ts", "build/esm/**/*.js",
"build/esm/**/*.js.map", "build/esm/**/*.d.ts", "build/src/**/*.js",
"build/src/**/*.js.map", "build/src/**/*.d.ts", "LICENSE", "README.md"
],
"dependencies": {
"@opentelemetry/api": "^1.4.1"
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^1.12.0",
"@opentelemetry/exporter-zipkin": "^1.12.0",
"@opentelemetry/resources": "^1.12.0",
"@opentelemetry/sdk-node": "^0.38.0",
"@opentelemetry/sdk-trace-base": "^1.12.0",
"@opentelemetry/semantic-conventions": "^1.12.0",
"@types/node": "^18.15.11",
"@types/tap": "^15.0.8",
"rome": "12.0.0",
"tap": "^16.3.4",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
},
"engines": {
"node": ">=18"
}
}
17 changes: 17 additions & 0 deletions rome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "./node_modules/rome/configuration_schema.json",
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
},
"ignore": ["build/*", "dist/*", "node_modules/*", "vendor/*"]
},
"formatter": {
"enabled": true,
"ignore": ["build/*", "dist/*", "node_modules/*", "vendor/*"]
}
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./monitor";
1 change: 1 addition & 0 deletions src/decorators/monitor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./monitor";
Loading

0 comments on commit 974be10

Please sign in to comment.