How to send openetelemetry data from instrumented libraries to OTLP

72 Views Asked by At

I have troubles with gathering the output from openetelemetry in nodejs app. Here is my setup:

tracing.js:


const { logs, SeverityNumber } = require('@opentelemetry/api-logs');
let otelLogs = logs.getLogger("publisher", "1.1")


const traceExporter = new OTLPTraceExporter({
     url: 'http://localhost:4318/v1/traces',
     headers: { hello: 'OTLPTraceExporter' },
 });

const logExporter = new OTLPLogExporter({
     url: 'http://localhost:4318/v1/logs',
     headers: { hello: 'OTLPLogExporter' },
 });

const provider = new BasicTracerProvider({
    resource: new Resource({
        [SemanticResourceAttributes.SERVICE_NAME]: process.env.SERVICE,
    }),
});

provider.addSpanProcessor(new SimpleSpanProcessor(traceExporter));
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register()

const sdk = new opentelemetry.NodeSDK({
    logRecordProcessor: new SimpleLogRecordProcessor(logExporter),
    traceExporter, // http
    instrumentations: [
        new HttpInstrumentation()
    ],
    resource: new Resource({
        [ SemanticResourceAttributes.SERVICE_NAME ]: process.env.SERVICE,
        [ SemanticResourceAttributes.SERVICE_NAMESPACE ]: "test",
        [ SemanticResourceAttributes.SERVICE_VERSION ]: "1.0",
        [ SemanticResourceAttributes.SERVICE_INSTANCE_ID ]: `my-instance-id-1 ${Date.now() }`,
    }),
    serviceName: "SERVICE"
});

const getTracer = () => {
    return trace.getTracer("default")
}

sdk.start()

module.exports = { getTracer }

publisher.js


const express = require('express')
const app = express()
const port = process.env.PORT || 3001
const { logs, SeverityNumber } = require('@opentelemetry/api-logs');
const otelLogs = logs.getLogger("publisher", "1.1")


const tracer = tracing.getTracer()
app.get('/a', async (req, res) => {
    const message = {
        pattern: {cmd: 'add-recipe'},
        data: {
            id: Date.now().toString(),
            title: "adgadgadgdag",
            description: "aadfadfadfeeeee111",
            creationDate: Date(),
            ingredients: ["adfadf","adfadf"]
        }

    }
    res.send(message)
})

app.listen(port, () => {
    console.log(`${process.env.SERVICE} Running ${port}`)
})
otelLogs.emit({
     severityNumber: SeverityNumber.INFO,
     body: "test publisher otelLogs",
     attributes : {test:"attribute"}
})

When I do GET to localhost:3001/a i can see the output in the console:

{
  traceId: '55245fd1400262e3774bd2c4ece669b7',
  parentId: undefined,
  traceState: undefined,
  name: 'GET',
  id: 'd93da19364db5f55',
  kind: 1,
  timestamp: 1699968631970000,
  duration: 59399.224,
  attributes: {
    'http.url': 'http://localhost:3001/a',
    'http.host': 'localhost:3001',
    'net.host.name': 'localhost',
    'http.method': 'GET',
    'http.scheme': 'http',
    'http.target': '/a',
    'http.user_agent': 'curl/7.81.0',
    'http.flavor': '1.1',
    'net.transport': 'ip_tcp',
    'net.host.ip': '::ffff:127.0.0.1',
    'net.host.port': 3001,
    'net.peer.ip': '::ffff:127.0.0.1',
    'net.peer.port': 42138,
    'http.status_code': 200,
    'http.status_text': 'OK'
  },
  status: { code: 0 },
  events: [],
  links: []
}

But the record is absent in the grafana, meanwhile the direct call to opentelemtry log function like

otelLogs.emit({
     severityNumber: SeverityNumber.INFO,
     body: "test publisher otelLogs",
     attributes : {test:"attribute"}
})

is present in the grafana. How to feed all the output from the instrumented libraries to grafana, or how to see them there?

0

There are 0 best solutions below