Reputation: 2037
I am trying to understand how to use and send logs using opentelemetry but find it is confusing. Suppose I am developing an application(preferable using javascript) and want to log things in the app and send the logs to a backend(e.g. azure monitor), what is the best way to do it? For traces and metrics, opentelemetry provides api/sdk to either direct exports traces/metrics to backend or through collector. However, my understanding of how logs work are so different. It looks to me opentelemetry does provide some api/sdk, but I am not sure if I should use it as an application developer. I see in the opentelemetry logging overview, it mentioned two things below:
OpenTelemetry defines an API for emitting LogRecords. Application developers are NOT encouraged to call this API directly. It is provided for library authors to build Appenders, which use the API to bridge between existing logging libraries and the OpenTelemetry log data model. Existing logging libraries generally provide a much richer set of features than what is defined in OpenTelemetry. It is NOT a goal of OpenTelemetry to ship a feature-rich logging library.
OpenTelemetry defines an API for emitting Events. The API consists of convenience methods which delegate to the API for emitting LogRecords. Application developers are encouraged to call this API directly.
How do I know if this api/sdk in javascript should be used by application developer or library authors to build Appenders? It seems this api/sdk has both emit event and emit LogRecords.
I understand opentelemetry log architecture aim to make sure both logs that we do not have control and controllable logs(like in terms of formats) to have correlation and context information, so one approach(called Via File or Stdout Logs) is to save the logs to some intermediary medium and then send to collector(My understanding this is more of the case of uncontrollable logs). The second approach(called Direct to Collector) is to send logs direct to collector. In the doc of the second way, it mentions "The most convenient way to achieve this is to provide addons or extensions to the commonly used logging libraries.", what does it mean? Does the addons or extensions refers to the Appenders mentioned above? I would appreciate some code examples of these two approaches in javascript especially the 2nd approach.
Upvotes: 13
Views: 7848
Reputation: 701
Direct OTEL Log emitting via http. Maybe BatchProcessor has better performance.
Typescript:
import * as logsAPI from '@opentelemetry/api-logs';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
LoggerProvider,
SimpleLogRecordProcessor
} from '@opentelemetry/sdk-logs';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
const resource = new Resource({
[ SemanticResourceAttributes.SERVICE_NAME ]: 'test',
[ SemanticResourceAttributes.SERVICE_NAMESPACE ]: 'ns_test',
});
const loggerProvider = new LoggerProvider({
resource
});
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new OTLPLogExporter({
url: 'http://otel_collector:4318',
keepAlive: true,
})));
const loggerOtel = loggerProvider.getLogger('default');
loggerOtel.emit({
severityNumber: logsAPI.SeverityNumber.INFO,
severityText: 'INFO',
body: 'test',
attributes: { 'log.type': 'LogRecord' },
});
Based on the example shown here.
Upvotes: 9