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 fs module support #777

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading