DEV Community

Cover image for Node.js Projelerine OpenTelemetry Entegrasyonu
Talha Namaldı for Açıklab

Posted on

2

Node.js Projelerine OpenTelemetry Entegrasyonu

Giriş

Bu yazı Node.js projelerine OpenTelemetry engtegrasyonunu projede minimum değişikilik ile nasıl yapıldığını gösterecek.

Adımlar

1. Gerekli Kütüphaneleri İndirin

OpenTelemetry'nin çalışması için gerekli kütüphaneleri npm install ile indirin.

npm install @opentelemetry/sdk-node \
  @opentelemetry/api \
  @opentelemetry/auto-instrumentations-node \
  @opentelemetry/sdk-metrics \
  @opentelemetry/sdk-trace-node
Enter fullscreen mode Exit fullscreen mode

2. Instrumentation Dosyasını Oluşturun

Projenizin en dış kısmına instrumentation.js adlı bir dosya oluşturun ve içine aşağıdaki kodu kopyalayıp yapıştırın.

const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations,} = require('@opentelemetry/auto-instrumentations-node');
const { PeriodicExportingMetricReader, ConsoleMetricExporter,} = require('@opentelemetry/sdk-metrics');

const sdk = new NodeSDK({
  traceExporter: new ConsoleSpanExporter(),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new ConsoleMetricExporter(),
  }),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

Enter fullscreen mode Exit fullscreen mode

Eğer OpenTelemetry çıktısını consola bastırmak yerine başka bir uygulmaya göndermek isterseniz instrumentation.js şu şekilde güncelleyiniz:

const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations,} = require('@opentelemetry/auto-instrumentations-node');
const { PeriodicExportingMetricReader, ConsoleMetricExporter,} = require('@opentelemetry/sdk-metrics');

const sdk = new NodeSDK({
    traceExporter: new OTLPTraceExporter({
    url: "http://0.0.0.0:0000/v1/traces", //göndermek istediğiniz uygulmanın ip adresi ve port numarasını buraya yazın
    serviceName: "nodeJSAPI",
  }),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new ConsoleMetricExporter(),
  }),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();

Enter fullscreen mode Exit fullscreen mode

Eğer servis isminiz yukardaki kodda serviceName kısmına yazdığınız halde değişmezse adımları takip edin.

1. Resource kütüphanesini ekleyin

const { Resource } = require('@opentelemetry/resources');

2.

Kodun instrumentations:[getNodeAutoInstrumentations()], satırının altına yeni bir resource tanımlayın resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'servis_isminiz',}).

3. Projenizi instrumentation.js Dosyası ile Çalıştırın

Projenizi çalıştırmak için aşağıdaki komutu kullanın. Komutta en son kısımda yer alan dosya ismini(app.js) kendinize göre değiştirebilirsiniz.

node --require ./instrumentation.js app.js
Enter fullscreen mode Exit fullscreen mode

Bu komutu sürekli olarak yazmak zor olacağından komutu projenizdeki package.json dosyasının içindeki scripts kısmına start komutu olarak ekleyebilirsiniz.

  "scripts": {
    "start": "node --require ./instrumentation.js app.js"
  }
Enter fullscreen mode Exit fullscreen mode

Komutu package.json dosyasına ekledikten sonra npm start yazarak prejenizi OpenTelemetry kullanarak başlatabilirsiniz.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay