Skip to content

Instantly share code, notes, and snippets.

@greatvovan
Last active September 19, 2024 08:11
Show Gist options
  • Save greatvovan/5e9e3d8ecaf4e210ade619c5b55455b3 to your computer and use it in GitHub Desktop.
Save greatvovan/5e9e3d8ecaf4e210ade619c5b55455b3 to your computer and use it in GitHub Desktop.
Databricks Scala notebook: OpenTelemetry Exporter - manual configuration
// neiltestappi
val aiConnStr = "..."
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
import org.apache.logging.log4j.core.config.Configurator
import io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender
import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.resources.Resource
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor
import io.opentelemetry.sdk.metrics.SdkMeterProvider
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader
import io.opentelemetry.sdk.logs.SdkLoggerProvider
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor
import io.opentelemetry.semconv.ServiceAttributes
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporterBuilder
val configBuilder = ConfigurationBuilderFactory.newConfigurationBuilder()
val configuration = configBuilder
.add(
configBuilder
.newAppender("Console", "CONSOLE")
)
.add(
configBuilder
.newAppender("Otel", "OpenTelemetry")
)
.add(
configBuilder
.newRootLogger(Level.INFO)
.add(configBuilder.newAppenderRef("Console"))
.add(configBuilder.newAppenderRef("Otel"))
)
.build(false)
Configurator.initialize(configuration)
val logger = LogManager.getLogger()
val exporterBuilder = new AzureMonitorExporterBuilder().connectionString(aiConnStr)
val traceExporter = exporterBuilder.buildTraceExporter()
val metricExporter = exporterBuilder.buildMetricExporter()
val logExporter = exporterBuilder.buildLogRecordExporter()
val resource = Resource.getDefault().toBuilder()
.put(ServiceAttributes.SERVICE_NAME, "my-notebook")
.put(ServiceAttributes.SERVICE_VERSION, "0.1.0")
.build()
val sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(traceExporter))
.setResource(resource)
.build()
val sdkMeterProvider = SdkMeterProvider.builder()
.registerMetricReader(PeriodicMetricReader.builder(metricExporter).build())
.setResource(resource)
.build()
val sdkLoggerProvider = SdkLoggerProvider.builder()
.addLogRecordProcessor(BatchLogRecordProcessor.builder(logExporter).build())
.setResource(resource)
.build()
val openTelemetry = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setMeterProvider(sdkMeterProvider)
.setLoggerProvider(sdkLoggerProvider)
.buildAndRegisterGlobal()
OpenTelemetryAppender.install(openTelemetry)
println("Configured")
logger.info("ScalaEM Log outside span")
println("log sent")
val attributes = Attributes.of[java.lang.String, java.lang.Long](
AttributeKey.stringKey("foo"), "bar",
AttributeKey.longKey("code"), 42L
)
val tracer = openTelemetry.getTracer("my-notebook")
val span = tracer.spanBuilder("My-span-ScalaEM").startSpan()
val scope = span.makeCurrent()
try {
span.addEvent("ScalaEM span event", attributes)
logger.info("ScalaEM Log from span")
Thread.sleep(1000L)
}
finally {
scope.close()
span.end()
}
println("trace sent")
val meter = openTelemetry.getMeter("my-notebook")
val gauge = meter.gaugeBuilder("my-gauge-ScalaEM").build()
gauge.set(111, attributes)
println("metric sent")
openTelemetry.shutdown()
println("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment