|
1 | 1 | import assert from "node:assert"; |
2 | 2 | import { EvmRpcStream, type Filter, rateLimitedHttp } from "@apibara/evm-rpc"; |
3 | 3 | import { createRpcClient } from "@apibara/protocol/rpc"; |
| 4 | +import { trace } from "@opentelemetry/api"; |
| 5 | +import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc"; |
| 6 | +import { Resource } from "@opentelemetry/resources"; |
| 7 | +import { |
| 8 | + BatchSpanProcessor, |
| 9 | + NodeTracerProvider, |
| 10 | +} from "@opentelemetry/sdk-trace-node"; |
4 | 11 | import { defineCommand, runMain } from "citty"; |
5 | 12 | import consola from "consola"; |
6 | 13 | import { createPublicClient } from "viem"; |
@@ -33,8 +40,29 @@ const command = defineCommand({ |
33 | 40 | default: "accepted", |
34 | 41 | description: "Block finality (finalized, accepted)", |
35 | 42 | }, |
| 43 | + trace: { |
| 44 | + type: "string", |
| 45 | + description: |
| 46 | + "Enable OpenTelemetry tracing and specify the OTLP gRPC endpoint URL (e.g. http://localhost:4317)", |
| 47 | + }, |
36 | 48 | }, |
37 | 49 | async run({ args }) { |
| 50 | + if (args.trace) { |
| 51 | + const provider = new NodeTracerProvider({ |
| 52 | + resource: new Resource({ |
| 53 | + "service.name": "example-evm-rpc-client", |
| 54 | + }), |
| 55 | + }); |
| 56 | + |
| 57 | + const exporter = new OTLPTraceExporter({ url: args.trace }); |
| 58 | + provider.addSpanProcessor(new BatchSpanProcessor(exporter)); |
| 59 | + provider.register(); |
| 60 | + |
| 61 | + trace.setGlobalTracerProvider(provider); |
| 62 | + |
| 63 | + consola.info("OpenTelemetry tracing enabled:", args.trace); |
| 64 | + } |
| 65 | + |
38 | 66 | consola.info("Creating EVM RPC client"); |
39 | 67 |
|
40 | 68 | const viemClient = createPublicClient({ |
|
0 commit comments