Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Provider
A provider is the API entry point that holds the configuration for telemetry data. The provider is responsible for setting up the environment and ensuring that all necessary configurations are in place. This can include configuring a vendor specific api key, or something as simple as setting the service name and environment. In our case, we will be using aTraceProvider to send traces that will be processed by the LaunchDarkly backend and converted to logs, errors, and traces.
In our example, the TracerProvider creates a resource, that builds attributes that we want to include with every trace. This includes the highlight.project_id to let LaunchDarkly know which project the traces below to, and other identifying data, such as service name and environment to help with monitoring and debugging.
Here’s a quick example of what this looks like in code:
Exporter
An exporter sends the telemetry data to the backend. This is where you configure the endpoint and any other necessary settings related to the backend you’re sending data to. In our example, we built a custom exporter as a workaround to some OpenTelemetry package issues with the React Native’s bundler, Metro. A bundler-based solution is also in progress to use OpenTelemetry’s OTLPTraceExporter, and substitute out our custom exporter. Here’s an example of how you might build a custom React Native exporter class that serializes and sends traces over http. Notice the majority of logic is serializing the batched spans:Processor
Finally, a processor defines any pre-processing that should be done on the created traces, such as batching, sampling, filtering or even enriching data. This is important because you may have specific needs on the machine that you’re sending data from that require customization. In our example, we will use aBatchSpanProcessor to collect spans in batches and send them to the exporter, which is more efficient than sending each span individually.
Here’s how we initialized the BatchSpanProcessor, and registered the traceProvider:
Instrumenting your application
After we created our tracer, we can now use it to send LaunchDarkly traces, logs, and errors. We can also monkeypatch javascript’sconsole methods, so they will send to LaunchDarkly by default.
Tracing
Tracing is possible by calling the createdtracer’s startSpan method. This accepts a parameter for the name of the span, and returns the span itself. From there, the span can record an error, add attributes, and much more. We will signal the end of a span by calling the end method.
Here is an example, assuming the tracer is exportable from a file called highlight.ts:
Logging
Logging can be sent to LaunchDarkly with a few configurations to the trace method. First, the span name should behighlight.log to let the LaunchDarkly backend know it is, in fact, a log. Second, we will pass in a log.severity and a log.message attribute to be used when constructing the log object. It is recommended you set up a log function to complete this.
Here is an example of a log function below:
console logs later. After you have created your function, you can export this function to be called in your application code.
Errors
In our solution, errors are also sent via traces to LaunchDarkly with some configuration details. Again, we will call the tracehighlight.log to ensure this trace will create a log for you in LaunchDarkly. Second, we will record an exception and add any attributes to the span.
Here is an example of a error function below:
console method will send any error logs to LaunchDarkly which will then be processed as an error. After you have created your function, you can export this function to be called in your application code.
Autoinstrumentation of console functions
The functions above are great for flexibility and customization, but maybe all you want is to report what is happening in the console to LaunchDarkly. We have you covered. We created a hook to call in your code that will monkeypatch theconsole methods to send to LaunchDarkly in addition to printing in the console dev tools. This should be called on app load or early on in the lifecycle of the session.
Here is an example of how to monkeypatch the console methods: