Skip to content

Commit

Permalink
Merge pull request #1114 from atlanhq/FT-864
Browse files Browse the repository at this point in the history
Adds OpenTelemetry logging for custom packages
  • Loading branch information
cmgrote authored Dec 11, 2024
2 parents 0cf1f97 + 0fefc94 commit 24e7a4a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ kotlin-mu = "3.0.5"
rocksdb = "9.7.3"
jetty = "12.0.15"
netty = "4.1.115.Final"
otlp = "1.45.0"
otel = "2.10.0"
otel-experimental = "2.10.0-alpha"

[libraries]
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
Expand Down Expand Up @@ -75,11 +78,18 @@ kotlin-logging = { module = "io.github.microutils:kotlin-logging-jvm", version.r
rocksdb = { module = "org.rocksdb:rocksdbjni", version.ref = "rocksdb" }
jetty-http = { module = "org.eclipse.jetty:jetty-http", version.ref = "jetty" }
netty-common = { module = "io.netty:netty-common", version.ref = "netty" }
otlp-bom = { module = "io.opentelemetry:opentelemetry-bom", version.ref = "otlp" }
otel-bom = { module = "io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom", version.ref = "otel" }
otel-sdk = { module = "io.opentelemetry:opentelemetry-sdk" }
otel-exporter = { module = "io.opentelemetry:opentelemetry-exporter-otlp" }
otel-semconv = { module = "io.opentelemetry.semconv:opentelemetry-semconv" }
otel-appender = { module = "io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17", version.ref = "otel-experimental" }

[bundles]
java-test = [ "jnanoid", "testng", "wiremock" ]
log4j = [ "log4j-core", "log4j-slf4j2" ]
poi = [ "apache-poi", "apache-poi-ooxml" ]
otel = [ "log4j-core", "otel-sdk", "otel-exporter", "otel-semconv", "otel-appender" ]

[plugins]
shadow = { id = "com.github.johnrengelman.shadow", version = "8.1.1" }
Expand Down
3 changes: 3 additions & 0 deletions package-toolkit/runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ dependencies {
api(libs.adls)
implementation(libs.sqlite)
implementation(libs.simple.java.mail)
implementation(platform(libs.otlp.bom))
implementation(platform(libs.otel.bom))
implementation(libs.bundles.otel)
// You would not need the dependencies below in reality, they are to simulate a running tenant
testImplementation(libs.bundles.java.test)
testImplementation(project(":mocks"))
Expand Down
23 changes: 23 additions & 0 deletions package-toolkit/runtime/src/main/kotlin/com/atlan/pkg/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import com.atlan.pkg.objectstore.S3Sync
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter
import io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.logs.SdkLoggerProvider
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor
import io.opentelemetry.sdk.resources.Resource
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.samplers.Sampler
import jakarta.activation.FileDataSource
import jakarta.mail.Message
import mu.KLogger
Expand Down Expand Up @@ -63,6 +71,21 @@ object Utils {
*/
inline fun <reified T : CustomConfig> setPackageOps(): T {
System.getProperty("logDirectory") ?: System.setProperty("logDirectory", "tmp")
val otelEndpoint = getEnvVar("OTEL_EXPORTER_OTLP_ENDPOINT")
if (otelEndpoint.isNotBlank()) {
// Configure OpenTelemetry, but only if there is an endpoint defined
val otel: OpenTelemetrySdk =
OpenTelemetrySdk.builder()
.setTracerProvider(SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build())
.setLoggerProvider(
SdkLoggerProvider.builder()
.setResource(Resource.getDefault().toBuilder().put("k8s.workflow.node.name", getEnvVar("OTEL_WF_NODE_NAME")).build())
.addLogRecordProcessor(BatchLogRecordProcessor.builder(OtlpGrpcLogRecordExporter.builder().setEndpoint(otelEndpoint).build()).build())
.build(),
).build()
Runtime.getRuntime().addShutdownHook(Thread(otel::close))
OpenTelemetryAppender.install(otel)
}
logDiagnostics()
logger.info { "Looking for configuration in environment variables..." }
val config = parseConfigFromEnv<T>()
Expand Down
2 changes: 2 additions & 0 deletions package-toolkit/runtime/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
</Route>
</Routes>
</Routing>
<OpenTelemetry name="OpenTelemetryAppender"/>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef name="OpenTelemetryAppender"/>
<AppenderRef ref="ConsoleAppender" level="INFO"/>
<AppenderRef ref="Routing"/>
</Root>
Expand Down

0 comments on commit 24e7a4a

Please sign in to comment.