Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add otel tracing #3294

Merged
merged 15 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
adamhathcock marked this conversation as resolved.
Show resolved Hide resolved
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"envFile": "${workspaceFolder}/packages/server/.env",
"program": "${workspaceFolder}/packages/server/bin/www",
"console": "integratedTerminal"
}
]
}
6 changes: 6 additions & 0 deletions packages/server/bin/ts-www
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
*/

require('ts-node/register')

//need to initialize before everything else
//knex is a singleton controlled by module so can't wait til app init
const { initOpenTelemetry } = require('../dist/otel')
adamhathcock marked this conversation as resolved.
Show resolved Hide resolved
initOpenTelemetry()

const { logger } = require('../logging/logging')
const { init, startHttp } = require('../app')

Expand Down
5 changes: 5 additions & 0 deletions packages/server/bin/www
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env node
'use strict'

//need to initialize before everything else
//knex is a singleton controlled by module so can't wait til app init
const { initOpenTelemetry } = require('../dist/otel')
initOpenTelemetry()

const { logger } = require('../dist/logging/logging')
const { init, startHttp } = require('../dist/app')

Expand Down
14 changes: 14 additions & 0 deletions packages/server/modules/shared/helpers/envHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,17 @@ export function getS3BucketName() {
export function createS3Bucket() {
return getBooleanFromEnv('S3_CREATE_BUCKET')
}

export function getOtelTracingUrl() {
if (!process.env.OTEL_TRACE_URL) return 'http://localhost:4318'
return process.env.OTEL_TRACE_URL
}

export function getOtelTraceKey() {
if (!process.env.OTEL_TRACE_KEY) return 'X-Seq-ApiKey'
return process.env.OTEL_TRACE_KEY
}

export function getOtelHeaderValue() {
return process.env.OTEL_TRACE_VALUE
}
68 changes: 68 additions & 0 deletions packages/server/otel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
import { KnexInstrumentation } from '@opentelemetry/instrumentation-knex'
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino'
import { AlwaysOnSampler, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node'
import { Resource } from '@opentelemetry/resources'
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
import { registerInstrumentations } from '@opentelemetry/instrumentation'
import opentelemetry from '@opentelemetry/api'
import {
getOtelHeaderValue,
getOtelTraceKey,
getOtelTracingUrl
} from '@/modules/shared/helpers/envHelper'

const OTEL_NAME = 'speckle'

export function initOpenTelemetry() {
const provider = new NodeTracerProvider({
resource: new Resource({
[ATTR_SERVICE_NAME]: OTEL_NAME
}),
sampler: new AlwaysOnSampler()
})
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
// Express instrumentation expects HTTP layer to be instrumented
new HttpInstrumentation(),
new GraphQLInstrumentation({
// optional params
// allowValues: true,
// depth: 2,
// mergeItems: true,
// ignoreTrivialResolveSpans: true,
// ignoreResolveSpans: true,
}),
new ExpressInstrumentation(),
new KnexInstrumentation({
maxQueryLength: 100
}),
new PinoInstrumentation({
disableLogSending: true
})
]
})
const headers: Partial<Record<string, unknown>> = {}
const key = getOtelTraceKey()
const value = getOtelHeaderValue()
if (key && value) {
headers[key] = value
}

const exporter = new OTLPTraceExporter({
url: getOtelTracingUrl(),
headers
})

provider.addSpanProcessor(new SimpleSpanProcessor(exporter))

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register()

opentelemetry.trace.getTracer(OTEL_NAME)
}
8 changes: 8 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
"@graphql-tools/mock": "^9.0.4",
"@graphql-tools/schema": "^10.0.6",
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.53.0",
"@opentelemetry/instrumentation-express": "^0.42.0",
"@opentelemetry/instrumentation-graphql": "^0.43.0",
"@opentelemetry/instrumentation-http": "^0.53.0",
"@opentelemetry/instrumentation-knex": "^0.40.0",
"@opentelemetry/instrumentation-pino": "^0.42.0",
"@opentelemetry/sdk-trace-node": "^1.26.0",
"@speckle/objectloader": "workspace:^",
"@speckle/shared": "workspace:^",
"ajv": "^8.12.0",
Expand Down
1 change: 1 addition & 0 deletions packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"type-augmentations/**/*",
"test/**/*",
"app.ts",
"otel.ts",
"bootstrap.js",
"knexfile.js"
],
Expand Down
Loading