Skip to content

Commit

Permalink
Code formatting with prettier (#4)
Browse files Browse the repository at this point in the history
* Setup prettier, pre-commit hooks

* Remove husky, add lint step in CI/CD

* Prettier changes

* Tweak README with link and version

* Fix homepage and repo url
  • Loading branch information
niwsa authored Apr 11, 2023
1 parent 4efba72 commit 69237a0
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 83 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"root": true
Expand Down
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
- package-ecosystem: 'npm' # See documentation for possible values
directory: '/' # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "github-actions"
directory: "/"
interval: 'weekly'
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: "weekly"
interval: 'weekly'
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main
- release
tags:
- "beta-v*"
- 'beta-v*'
pull_request:
# Trigger only for PRs that target main branch
branches:
Expand All @@ -24,6 +24,7 @@ jobs:
with:
node-version: 18
- run: npm ci
- run: npm run lint
- run: npm test --if-present

publish-npm:
Expand Down
13 changes: 13 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
**/**/node_modules

*.lock
*.log

.gitignore
.npmignore
.prettierignore
.DS_Store
.eslintignore

**/LICENSE
11 changes: 11 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
bracketSpacing: true,
bracketSameLine: true,
singleQuote: true,
jsxSingleQuote: true,
trailingComma: 'es5',
semi: true,
printWidth: 110,
arrowParens: 'always',
importOrderSeparation: true,
};
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# metrics
# @boxyhq/metrics

<a href="https://www.npmjs.com/package/@boxyhq/metrics"><img src="https://img.shields.io/npm/v/@boxyhq/metrics.svg" alt="npm" ></a>

Package for OTel instrumentation
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
],
"scripts": {
"build": "tsc -p tsconfig.json",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"lint": "eslint -c .eslintrc.json --ext .ts ./"
},
"repository": {
"type": "git",
"url": "git+https://github.com/boxyhq/jackson.git",
"directory": "metrics"
"url": "git+https://github.com/boxyhq/metrics.git"
},
"keywords": [
"otel",
"telemetry"
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/boxyhq/jackson/issues"
"url": "https://github.com/boxyhq/metrics/issues"
},
"homepage": "https://github.com/boxyhq/jackson#readme",
"homepage": "https://github.com/boxyhq/metrics#readme",
"dependencies": {
"@opentelemetry/api": "1.4.1",
"@opentelemetry/exporter-metrics-otlp-grpc": "0.37.0",
Expand All @@ -38,6 +38,8 @@
"@typescript-eslint/eslint-plugin": "5.57.1",
"@typescript-eslint/parser": "5.57.1",
"eslint": "8.38.0",
"eslint-config-prettier": "8.8.0",
"prettier": "2.8.7",
"typescript": "5.0.4"
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./init";
export * from "./instruments";
export * from './init';
export * from './instruments';
29 changes: 9 additions & 20 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import {
DiagConsoleLogger,
DiagLogLevel,
diag,
metrics,
} from "@opentelemetry/api";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
import { OTLPMetricExporter as OTLPMetricExporterGRPC } from "@opentelemetry/exporter-metrics-otlp-grpc";
import {
MeterProvider,
PeriodicExportingMetricReader,
} from "@opentelemetry/sdk-metrics";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { DiagConsoleLogger, DiagLogLevel, diag, metrics } from '@opentelemetry/api';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { OTLPMetricExporter as OTLPMetricExporterGRPC } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

type ServiceInfo = {
name: string;
Expand All @@ -25,10 +17,7 @@ type ServiceInfo = {
*/

function initializeMetrics(serviceInfo: ServiceInfo) {
if (
process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT ||
process.env.OTEL_EXPORTER_OTLP_ENDPOINT
) {
if (process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT || process.env.OTEL_EXPORTER_OTLP_ENDPOINT) {
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: `${serviceInfo.name}`,
Expand All @@ -38,8 +27,8 @@ function initializeMetrics(serviceInfo: ServiceInfo) {

let metricExporter;
if (
process.env.OTEL_EXPORTER_OTLP_PROTOCOL === "grpc" ||
process.env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL === "grpc"
process.env.OTEL_EXPORTER_OTLP_PROTOCOL === 'grpc' ||
process.env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL === 'grpc'
) {
metricExporter = new OTLPMetricExporterGRPC();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/instruments/counter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Attributes, Counter, MetricOptions } from "@opentelemetry/api";
import { acquireMeter } from "../lib/meter";
import type { Attributes, Counter, MetricOptions } from '@opentelemetry/api';
import { acquireMeter } from '../lib/meter';

const counters: Record<string, Counter<Attributes>> = {};

Expand Down
16 changes: 3 additions & 13 deletions src/instruments/gauge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type {
Attributes,
MetricOptions,
ObservableGauge,
} from "@opentelemetry/api";
import { acquireMeter } from "../lib/meter";
import type { Attributes, MetricOptions, ObservableGauge } from '@opentelemetry/api';
import { acquireMeter } from '../lib/meter';

const gauges: Record<string, ObservableGauge<Attributes>> = {};

Expand All @@ -20,13 +16,7 @@ type GaugeOperationParams = {
gaugeAttributes?: Attributes;
};

const observeGauge = ({
meter,
name,
val,
gaugeOptions,
gaugeAttributes,
}: GaugeOperationParams) => {
const observeGauge = ({ meter, name, val, gaugeOptions, gaugeAttributes }: GaugeOperationParams) => {
let gauge = gauges[name];
if (gauge === undefined) {
const _otelMeter = acquireMeter(meter);
Expand Down
9 changes: 3 additions & 6 deletions src/instruments/histogram.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Attributes, MetricOptions, Histogram } from "@opentelemetry/api";
import { acquireMeter } from "../lib/meter";
import type { Attributes, MetricOptions, Histogram } from '@opentelemetry/api';
import { acquireMeter } from '../lib/meter';

const histograms: Record<string, Histogram<Attributes>> = {};

Expand All @@ -26,10 +26,7 @@ const recordHistogram = ({
let histogram = histograms[name];
if (histogram === undefined) {
const _otelMeter = acquireMeter(meter);
histogram = histograms[name] = _otelMeter.createHistogram(
name,
histogramOptions
);
histogram = histograms[name] = _otelMeter.createHistogram(name, histogramOptions);
}
histogram.record(val, histogramAttributes);
};
Expand Down
10 changes: 5 additions & 5 deletions src/instruments/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./counter";
export * from "./gauge";
export * from "./histogram";
export * from "./timer";
export * from "./instrument";
export * from './counter';
export * from './gauge';
export * from './histogram';
export * from './timer';
export * from './instrument';
17 changes: 6 additions & 11 deletions src/instruments/instrument.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Attributes } from "@opentelemetry/api";
import { incrementCounter } from "./counter";
import { recordTimer } from "./timer";
import type { Attributes } from '@opentelemetry/api';
import { incrementCounter } from './counter';
import { recordTimer } from './timer';

type InstrumentOperationParams = {
/** OTel meter name */
Expand All @@ -19,19 +19,14 @@ type InstrumentOperationParams = {
* @param operationParams
*/

async function instrument({
meter,
name,
delegate,
instrumentAttributes,
}: InstrumentOperationParams) {
async function instrument({ meter, name, delegate, instrumentAttributes }: InstrumentOperationParams) {
const start = process.hrtime();
try {
return await delegate();
} catch (err) {
incrementCounter({
meter,
name: "function.errors",
name: 'function.errors',
counterAttributes: { function: name, ...instrumentAttributes },
});
throw err;
Expand All @@ -40,7 +35,7 @@ async function instrument({
const elapsedNanos = elapsed[0] * 1000000000 + elapsed[1];
recordTimer({
meter,
name: "function.executionTime",
name: 'function.executionTime',
val: elapsedNanos,
timerAttributes: { function: name, ...instrumentAttributes },
});
Expand Down
14 changes: 4 additions & 10 deletions src/instruments/timer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Attributes, MetricOptions, Histogram } from "@opentelemetry/api";
import { acquireMeter } from "../lib/meter";
import type { Attributes, MetricOptions, Histogram } from '@opentelemetry/api';
import { acquireMeter } from '../lib/meter';

const timers: Record<string, Histogram<Attributes>> = {};

Expand All @@ -16,19 +16,13 @@ type TimerOperationParams = {
timerAttributes?: Attributes;
};

const recordTimer = ({
meter,
name,
val,
timerOptions,
timerAttributes,
}: TimerOperationParams) => {
const recordTimer = ({ meter, name, val, timerOptions, timerAttributes }: TimerOperationParams) => {
let timer = timers[name];
if (timer === undefined) {
const _otelMeter = acquireMeter(meter);
timer = timers[name] = _otelMeter.createHistogram(name, {
...timerOptions,
unit: "ns",
unit: 'ns',
});
}
timer.record(val, timerAttributes);
Expand Down

0 comments on commit 69237a0

Please sign in to comment.