Skip to content

Commit

Permalink
Minor OTEL instrumentation changes (#578)
Browse files Browse the repository at this point in the history
* Ignore traces on incoming request on / and /ping

Add `deployment.environment=local` as default env variable.

Default diagLogLevel to ERROR.

* Move OTEL log to environment variable
  • Loading branch information
wcalderipe authored Nov 12, 2024
1 parent 0e0afeb commit 04f86e8
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vault.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ jobs:
status: ${{ job.status }}
fields: message,commit,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ include ./apps/devtool/Makefile
include ./apps/docs/Makefile
include ./apps/policy-engine/Makefile
include ./apps/vault/Makefile
include ./packages/armory-e2e-testing/Makefile
include ./packages/armory-sdk/Makefile
include ./packages/nestjs-shared/Makefile
include ./packages/open-telemetry/Makefile
include ./packages/policy-engine-shared/Makefile
include ./packages/transaction-request-intent/Makefile
include ./packages/signature/Makefile
include ./packages/armory-sdk/Makefile
include ./packages/armory-e2e-testing/Makefile
include ./packages/transaction-request-intent/Makefile

# For more terminal color codes, head over to
# https://opensource.com/article/19/9/linux-terminal-colors
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ below to generate a project of your choice.

```bash
# Generate an standard JavaScript library.
npx nx g @nrwl/js:lib
npx nx g @nrwl/js:lib
# Generate an NestJS library.
npx nx g @nx/nest:library
# Generate an NestJS application.
Expand Down Expand Up @@ -166,7 +166,7 @@ OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
```

> [!NOTE]
> OTEL is disabled by default in development.
> OTEL is disabled by default in development.
3. Restart the application.

Expand All @@ -175,7 +175,7 @@ OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
### Naming Conventions

- Traces: `operation.entity.action` (e.g., `policyStore.policies.update`)
- Metrics: `system_entity_unit_state` (e.g., `policy_store_updates_total`)
- Metrics: `system_entity_unit_state` (e.g., `policy_store_updates_total`)

See [Metric Naming](./packages/nestjs-shared/src/lib/module/open-telemetry/service/metric.service.ts)
and [Trace/Span
Expand Down
17 changes: 15 additions & 2 deletions apps/armory/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ NODE_ENV=development

PORT=3005

APP_UID=local-dev-armory-instance-1

# OPTIONAL: Sets the admin API key instead of generating a new one during the
# provision.
#
# Key should be hashed, like this: `echo -n "my-api-key" | openssl dgst -sha256 | awk '{print $2}'`
# Plain text API key: armory-admin-api-key
ADMIN_API_KEY=171c50ec62122b8c08362dcf9dce9b016ed615cfc7b90d4bc3fe5b223d967fb2

# === Database ===

# APP db connection string
APP_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/armory?schema=public

Expand All @@ -20,20 +24,29 @@ APP_DATABASE_HOST=host.docker.internal
APP_DATABASE_PORT=5432
APP_DATABASE_NAME=armory

APP_UID=local-dev-armory-instance-1
# === Redis ===

REDIS_HOST=localhost
REDIS_PORT=6379

# === Data Feed ===

PRICE_FEED_PRIVATE_KEY=0xc7a1b8ba040a238e36058fc5693f801d129aca9f10ed30d0133878f1b9147c01
HISTORICAL_TRANSFER_FEED_PRIVATE_KEY=0xf5c8f17cc09215c5038f6b8d5e557c0d98d341236307fe831efdcdd7faeef134

# === Policy Engine ===

POLICY_ENGINE_URLS=http://localhost:3010
POLICY_ENGINE_ADMIN_API_KEYS=engine-admin-api-key

MANAGED_DATASTORE_BASE_URL=http://localhost:3005/v1/data

# OpenTelemetry configuration.
# === OpenTelemetry configuration ===

OTEL_SDK_DISABLED=true
# OTEL Collector container HTTP port.
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=local
7 changes: 7 additions & 0 deletions apps/armory/.env.test.default
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ POLICY_ENGINE_URLS=http://localhost:3010
POLICY_ENGINE_ADMIN_API_KEYS=test-api-key

MANAGED_DATASTORE_BASE_URL=http://localhost:3005/data

OTEL_SDK_DISABLED=true
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=test
4 changes: 2 additions & 2 deletions apps/armory/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { instrumentOpenTelemetry } from '@narval/open-telemetry'
import { instrumentTelemetry } from '@narval/open-telemetry'

// IMPORTANT: OpenTelemetry SDK must be registered before any other imports to
// ensure proper instrumentation. The instrumentation packages patches Node.js
// runtime - if NestFactory or other dependencies load first, they'll use the
// unpatched runtime and won't be instrumented correctly.
instrumentOpenTelemetry({ serviceName: 'auth' })
instrumentTelemetry({ serviceName: 'auth' })

import { ConfigService } from '@narval/config-module'
import { LoggerService, withApiVersion, withCors, withLogger, withSwagger } from '@narval/nestjs-shared'
Expand Down
16 changes: 14 additions & 2 deletions apps/policy-engine/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ NODE_ENV=development

PORT=3010

APP_UID=local-dev-engine-instance-1

# OPTIONAL: Sets the admin API key instead of generating a new one during the
# provision.
#
# Key should be hashed, like this: `echo -n "my-api-key" | openssl dgst -sha256 | awk '{print $2}'`
# Plain text API key: engine-admin-api-key
ADMIN_API_KEY=dde1fba05d6b0b1a40f2cd9f480f6dcc37a6980bcff3db54377a46b056dc472c

# === Database ===

# APP db connection string
APP_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/engine?schema=public

Expand All @@ -20,7 +24,8 @@ APP_DATABASE_HOST=host.docker.internal
APP_DATABASE_PORT=5432
APP_DATABASE_NAME=engine

APP_UID=local-dev-engine-instance-1

# === Encryption ===

RESOURCE_PATH=./apps/policy-engine/src/resource

Expand All @@ -37,12 +42,19 @@ MASTER_AWS_KMS_ARN=
# Either "simple" or "mpc"
SIGNING_PROTOCOL=simple

# === MPC ===

# If SIGNING_PROTOCOL is set to "mpc", the application MUST configure a TSM.
TSM_URL=
TSM_API_KEY=
TSM_PLAYER_COUNT=

# OpenTelemetry configuration.
# === OpenTelemetry configuration ===

OTEL_SDK_DISABLED=true
# OTEL Collector container HTTP port.
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=local
7 changes: 7 additions & 0 deletions apps/policy-engine/.env.test.default
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ RESOURCE_PATH=./apps/policy-engine/src/resource
# MASTER_AWS_KMS_ARN="arn:aws:kms:us-east-2:728783560968:key/f6aa3ddb-47c3-4f31-977d-b93205bb23d1"

SIGNING_PROTOCOL=simple

OTEL_SDK_DISABLED=true
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=test
4 changes: 2 additions & 2 deletions apps/policy-engine/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { instrumentOpenTelemetry } from '@narval/open-telemetry'
import { instrumentTelemetry } from '@narval/open-telemetry'

// IMPORTANT: OpenTelemetry SDK must be registered before any other imports to
// ensure proper instrumentation. The instrumentation packages patches Node.js
// runtime - if NestFactory or other dependencies load first, they'll use the
// unpatched runtime and won't be instrumented correctly.
instrumentOpenTelemetry({ serviceName: 'policy-engine' })
instrumentTelemetry({ serviceName: 'policy-engine' })

import { ConfigService } from '@narval/config-module'
import { LoggerService, withApiVersion, withCors, withLogger, withSwagger } from '@narval/nestjs-shared'
Expand Down
16 changes: 13 additions & 3 deletions apps/vault/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ NODE_ENV=development

PORT=3011

APP_UID=local-dev-vault-instance-1

# OPTIONAL: Sets the admin API key instead of generating a new one during the
# provision.
#
# Key should be hashed, like this: `echo -n "my-api-key" | openssl dgst -sha256 | awk '{print $2}'`
# Plain text API key: vault-admin-api-key
ADMIN_API_KEY=d4a6b4c1cb71dbdb68a1dd429ad737369f74b9e264b9dfa639258753987caaad

# === Database ===

# APP db connection string
APP_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/vault?schema=public

Expand All @@ -20,7 +24,7 @@ APP_DATABASE_HOST=host.docker.internal
APP_DATABASE_PORT=5432
APP_DATABASE_NAME=vault

APP_UID=local-dev-vault-instance-1
# === Encryption ===

# Determine the encryption module keyring type.
# Either "awskms" or "raw".
Expand All @@ -32,10 +36,16 @@ MASTER_PASSWORD=unsafe-local-dev-master-password
# If using awskms keyring, provide the ARN of the KMS encryption key instead of a master password
MASTER_AWS_KMS_ARN=

# BaseUrl where the Vault is deployed. Will be used to verify jwsd request signatures
# Base URL where the Vault is deployed. Will be used to verify jwsd request
# signatures.
BASE_URL=http://localhost:3011

# OpenTelemetry configuration.
# === OpenTelemetry configuration ===

OTEL_SDK_DISABLED=true
# OTEL Collector container HTTP port.
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=local
7 changes: 7 additions & 0 deletions apps/vault/.env.test.default
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ KEYRING_TYPE="raw"
# MASTER_AWS_KMS_ARN="arn:aws:kms:us-east-2:728783560968:key/f6aa3ddb-47c3-4f31-977d-b93205bb23d1"

BASE_URL="https://vault-test.narval.xyz"

OTEL_SDK_DISABLED=true
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_LOGS_EXPORTER=otlp
OTEL_LOG_LEVEL=error
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=test
4 changes: 2 additions & 2 deletions apps/vault/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { instrumentOpenTelemetry } from '@narval/open-telemetry'
import { instrumentTelemetry } from '@narval/open-telemetry'

// IMPORTANT: OpenTelemetry SDK must be registered before any other imports to
// ensure proper instrumentation. The instrumentation packages patches Node.js
// runtime - if NestFactory or other dependencies load first, they'll use the
// unpatched runtime and won't be instrumented correctly.
instrumentOpenTelemetry({ serviceName: 'vault' })
instrumentTelemetry({ serviceName: 'vault' })

import { ConfigService } from '@narval/config-module'
import { LoggerService, withApiVersion, withCors, withLogger, withSwagger } from '@narval/nestjs-shared'
Expand Down
29 changes: 29 additions & 0 deletions packages/open-telemetry/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
OPEN_TELEMETRY_PROJECT_NAME := open-telemetry
OPEN_TELEMETRY_PROJECT_DIR := ./packages/open-telemetry

# == Code format ==

open-telemetry/format:
npx nx format:write --projects ${OPEN_TELEMETRY_PROJECT_NAME}

open-telemetry/lint:
npx nx lint ${OPEN_TELEMETRY_PROJECT_NAME} -- --fix

open-telemetry/format/check:
npx nx format:check --projects ${OPEN_TELEMETRY_PROJECT_NAME}

open-telemetry/lint/check:
npx nx lint ${OPEN_TELEMETRY_PROJECT_NAME}

# == Testing ==

open-telemetry/test/type:
npx tsc \
--project ${OPEN_TELEMETRY_PROJECT_DIR}/tsconfig.lib.json \
--noEmit

open-telemetry/test/unit:
npx nx test:unit ${OPEN_TELEMETRY_PROJECT_NAME} -- ${ARGS}

open-telemetry/test/unit/watch:
make open-telemetry/test/unit ARGS=--watch
9 changes: 4 additions & 5 deletions packages/open-telemetry/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Open Telemetry
# Open Telemetry

Armory Stack shared Open Telemetry (OTEL) instrumentation configuration.

Expand All @@ -7,9 +7,8 @@ Armory Stack shared Open Telemetry (OTEL) instrumentation configuration.
OTEL registration is kept in a separate package because:

- OTEL modifies Node.js runtime behavior by patching core modules. If
we import any dependencies before registering OTEL, those imports
will use the unpatched runtime and won't be instrumented correctly.
we import any dependencies before registering OTEL, those imports
will use the unpatched runtime and won't be instrumented correctly.

- Having it separate ensures OTELis registered first, before any
other code runs, guaranteeing proper instrumentation of all dependencies.

other code runs, guaranteeing proper instrumentation of all dependencies.
19 changes: 13 additions & 6 deletions packages/open-telemetry/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/* eslint-disable */
export default {
import type { Config } from 'jest'

const config: Config = {
displayName: 'open-telemetry',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/open-telemetry'
'^.+\\.[tj]sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json'
}
]
}
}

export default config
9 changes: 9 additions & 0 deletions packages/open-telemetry/jest.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Config } from 'jest'
import sharedConfig from './jest.config'

const config: Config = {
...sharedConfig,
testMatch: ['<rootDir>/**/__test__/unit/**/*.spec.ts']
}

export default config
11 changes: 9 additions & 2 deletions packages/open-telemetry/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"test:type": {
"executor": "nx:run-commands",
"options": {
"command": "make open-telemetry/test/type"
}
},
"test:unit": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/open-telemetry/jest.config.ts"
"jestConfig": "packages/open-telemetry/jest.unit.ts",
"verbose": true
}
}
}
Expand Down
Loading

0 comments on commit 04f86e8

Please sign in to comment.