Skip to content

Commit

Permalink
Add fs module support
Browse files Browse the repository at this point in the history
All fs module usage is now automatically instrumented.

Tests are in the express-redis sample application.

Automatic instrumentation is manually suppresed in the test span
processor because it uses the fs module to handle the spans.json file.
It is done using the `supressTracing` helper provided by OpenTelemetry
core library.
  • Loading branch information
luismiramirez committed Nov 3, 2022
1 parent f65b59a commit cee020f
Show file tree
Hide file tree
Showing 8 changed files with 2,923 additions and 16,842 deletions.
6 changes: 6 additions & 0 deletions .changesets/add-fs-module-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add fs module support
168 changes: 122 additions & 46 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@opentelemetry/sdk-node": "^0.33.0",
"@opentelemetry/sdk-trace-base": "^1.7.0",
"@opentelemetry/instrumentation-fastify": "^0.30.0",
"@opentelemetry/instrumentation-fs": "^0.5.1",
"@opentelemetry/instrumentation-express": "^0.31.2",
"@opentelemetry/instrumentation-graphql": "^0.31.0",
"@opentelemetry/instrumentation-http": "^0.32.0",
Expand Down
2 changes: 2 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ExpressLayerType
} from "@opentelemetry/instrumentation-express"
import { FastifyInstrumentation } from "@opentelemetry/instrumentation-fastify"
import { FsInstrumentation } from "@opentelemetry/instrumentation-fs"
import { GraphQLInstrumentation } from "@opentelemetry/instrumentation-graphql"
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http"
import { IORedisInstrumentation } from "@opentelemetry/instrumentation-ioredis"
Expand All @@ -34,6 +35,7 @@ import { SpanProcessor, TestModeSpanProcessor } from "./span_processor"
const DefaultInstrumentations = {
"@opentelemetry/instrumentation-express": ExpressInstrumentation,
"@opentelemetry/instrumentation-fastify": FastifyInstrumentation,
"@opentelemetry/instrumentation-fs": FsInstrumentation,
"@opentelemetry/instrumentation-graphql": GraphQLInstrumentation,
"@opentelemetry/instrumentation-http": HttpInstrumentation,
"@opentelemetry/instrumentation-ioredis": IORedisInstrumentation,
Expand Down
17 changes: 11 additions & 6 deletions src/span_processor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from "fs"
import type { Context } from "@opentelemetry/api"
import { SpanKind } from "@opentelemetry/api"
import { context, SpanKind } from "@opentelemetry/api"
import type {
Span,
ReadableSpan,
SpanProcessor as OpenTelemetrySpanProcessor
} from "@opentelemetry/sdk-trace-base"
import { suppressTracing } from "@opentelemetry/core"
import { Client } from "./client"

export class SpanProcessor implements OpenTelemetrySpanProcessor {
Expand Down Expand Up @@ -92,11 +93,15 @@ export class TestModeSpanProcessor implements OpenTelemetrySpanProcessor {
endTime: span.endTime
}

// Re-open the file for every write, as the test process might have
// truncated it in between writes.
const file = fs.openSync(this.#filePath, "a")
fs.appendFileSync(file, `${JSON.stringify(serializableSpan)}\n`)
fs.closeSync(file)
// As `fs` is an automatically instrumented module by default, we supress tracing during
// its usage here so it doesn't fail nor contaminate actual traces.
context.with(suppressTracing(context.active()), () => {
// Re-open the file for every write, as the test process might have
// truncated it in between writes.
const file = fs.openSync(this.#filePath, "a")
fs.appendFileSync(file, `${JSON.stringify(serializableSpan)}\n`)
fs.closeSync(file)
})
}

shutdown() {
Expand Down

0 comments on commit cee020f

Please sign in to comment.