-
Notifications
You must be signed in to change notification settings - Fork 43
core-logs: add LogRecordBuilder #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
core/logs/src/main/scala/org/typelevel/otel4s/logs/LogRecordBuilder.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/* | ||
* Copyright 2025 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.logs | ||
|
||
import cats.Applicative | ||
import cats.Monad | ||
import org.typelevel.otel4s.AnyValue | ||
import org.typelevel.otel4s.Attribute | ||
import org.typelevel.otel4s.KindTransformer | ||
import org.typelevel.otel4s.meta.InstrumentMeta | ||
|
||
import java.time.Instant | ||
import scala.collection.immutable | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** Provides a way to build and emit a log record. | ||
* | ||
* @see | ||
* [[https://opentelemetry.io/docs/specs/otel/logs/api/#emit-a-logrecord]] | ||
* | ||
* @see | ||
* [[https://opentelemetry.io/docs/specs/otel/logs/data-model/]] | ||
*/ | ||
trait LogRecordBuilder[F[_], Ctx] { | ||
|
||
/** The instrument's metadata. Indicates whether instrumentation is enabled. | ||
*/ | ||
def meta: InstrumentMeta.Dynamic[F] | ||
|
||
/** Sets the time when the event occurred measured by the origin clock, i.e. the time at the source. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withTimestamp(timestamp: FiniteDuration): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the time when the event occurred measured by the origin clock, i.e. the time at the source. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withTimestamp(timestamp: Instant): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the time when the event was observed by the collection system. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withObservedTimestamp(timestamp: FiniteDuration): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the time when the event was observed by the collection system. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withObservedTimestamp(timestamp: Instant): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the context to associate with the log record. | ||
* | ||
* The context will be used to extract tracing information, such as trace id and span id. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withContext(context: Ctx): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the [[Severity]] level. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withSeverity(severity: Severity): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the severity text (also known as log level) to the builder. | ||
* | ||
* This is the original string representation of the severity as it is known at the source. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withSeverityText(severityText: String): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the given body to the builder. | ||
* | ||
* Can be, for example, a human-readable string message (including multi-line) describing the event in a free form, | ||
* or it can be a structured data composed of arrays and maps of other values. | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained. | ||
* | ||
* @example | ||
* {{{ | ||
* val builder: LogRecordBuilder[F] = ??? | ||
* builder.withBody(AnyValue.string("the log message")) | ||
* }}} | ||
*/ | ||
def withBody(body: AnyValue): LogRecordBuilder[F, Ctx] | ||
|
||
/** Sets the event name, which identifies the class or type of the event. | ||
* | ||
* This name should uniquely identify the event structure (both attributes and body). | ||
* | ||
* @note | ||
* on multiple subsequent calls, the value from the last call will be retained | ||
*/ | ||
def withEventName(eventName: String): LogRecordBuilder[F, Ctx] | ||
|
||
/** Adds the given attribute to the builder. | ||
* | ||
* @note | ||
* if the builder previously contained a mapping for the key, the old value is replaced by the specified value | ||
*/ | ||
def addAttribute[A](attribute: Attribute[A]): LogRecordBuilder[F, Ctx] | ||
|
||
/** Adds attributes to the builder. | ||
* | ||
* @note | ||
* if the builder previously contained a mapping for any of the keys, the old values are replaced by the specified | ||
* values | ||
*/ | ||
def addAttributes(attributes: Attribute[_]*): LogRecordBuilder[F, Ctx] | ||
|
||
/** Adds attributes to the builder. | ||
* | ||
* @note | ||
* if the builder previously contained a mapping for any of the keys, the old values are replaced by the specified | ||
* values | ||
*/ | ||
def addAttributes(attributes: immutable.Iterable[Attribute[_]]): LogRecordBuilder[F, Ctx] | ||
|
||
/** Creates a log record and emits it to the processing pipeline. | ||
*/ | ||
def emit: F[Unit] | ||
|
||
def mapK[G[_]](implicit G: Monad[G], kt: KindTransformer[F, G]): LogRecordBuilder[G, Ctx] = | ||
new LogRecordBuilder.MappedK(this) | ||
|
||
} | ||
|
||
object LogRecordBuilder { | ||
|
||
def noop[F[_]: Applicative, Ctx]: LogRecordBuilder[F, Ctx] = | ||
new LogRecordBuilder[F, Ctx] { | ||
val meta: InstrumentMeta.Dynamic[F] = InstrumentMeta.Dynamic.disabled | ||
def withTimestamp(timestamp: FiniteDuration): LogRecordBuilder[F, Ctx] = this | ||
def withTimestamp(timestamp: Instant): LogRecordBuilder[F, Ctx] = this | ||
def withObservedTimestamp(timestamp: FiniteDuration): LogRecordBuilder[F, Ctx] = this | ||
def withObservedTimestamp(timestamp: Instant): LogRecordBuilder[F, Ctx] = this | ||
def withContext(context: Ctx): LogRecordBuilder[F, Ctx] = this | ||
def withSeverity(severity: Severity): LogRecordBuilder[F, Ctx] = this | ||
def withSeverityText(severityText: String): LogRecordBuilder[F, Ctx] = this | ||
def withBody(body: AnyValue): LogRecordBuilder[F, Ctx] = this | ||
def withEventName(eventName: String): LogRecordBuilder[F, Ctx] = this | ||
def addAttribute[A](attribute: Attribute[A]): LogRecordBuilder[F, Ctx] = this | ||
def addAttributes(attributes: Attribute[_]*): LogRecordBuilder[F, Ctx] = this | ||
def addAttributes(attributes: immutable.Iterable[Attribute[_]]): LogRecordBuilder[F, Ctx] = this | ||
def emit: F[Unit] = Applicative[F].unit | ||
} | ||
|
||
private final class MappedK[F[_], G[_]: Monad, Ctx]( | ||
builder: LogRecordBuilder[F, Ctx] | ||
)(implicit kt: KindTransformer[F, G]) | ||
extends LogRecordBuilder[G, Ctx] { | ||
|
||
val meta: InstrumentMeta.Dynamic[G] = builder.meta.mapK[G] | ||
|
||
def withTimestamp(timestamp: FiniteDuration): LogRecordBuilder[G, Ctx] = | ||
builder.withTimestamp(timestamp).mapK | ||
|
||
def withTimestamp(timestamp: Instant): LogRecordBuilder[G, Ctx] = | ||
builder.withTimestamp(timestamp).mapK | ||
|
||
def withObservedTimestamp(timestamp: FiniteDuration): LogRecordBuilder[G, Ctx] = | ||
builder.withObservedTimestamp(timestamp).mapK | ||
|
||
def withObservedTimestamp(timestamp: Instant): LogRecordBuilder[G, Ctx] = | ||
builder.withObservedTimestamp(timestamp).mapK | ||
|
||
def withContext(context: Ctx): LogRecordBuilder[G, Ctx] = | ||
builder.withContext(context).mapK | ||
|
||
def withSeverity(severity: Severity): LogRecordBuilder[G, Ctx] = | ||
builder.withSeverity(severity).mapK | ||
|
||
def withSeverityText(severityText: String): LogRecordBuilder[G, Ctx] = | ||
builder.withSeverityText(severityText).mapK | ||
|
||
def withBody(body: AnyValue): LogRecordBuilder[G, Ctx] = | ||
builder.withBody(body).mapK | ||
|
||
def withEventName(eventName: String): LogRecordBuilder[G, Ctx] = | ||
builder.withEventName(eventName).mapK | ||
|
||
def addAttribute[A](attribute: Attribute[A]): LogRecordBuilder[G, Ctx] = | ||
builder.addAttribute(attribute).mapK | ||
|
||
def addAttributes(attributes: Attribute[_]*): LogRecordBuilder[G, Ctx] = | ||
builder.addAttributes(attributes).mapK | ||
|
||
def addAttributes(attributes: immutable.Iterable[Attribute[_]]): LogRecordBuilder[G, Ctx] = | ||
builder.addAttributes(attributes).mapK | ||
|
||
def emit: G[Unit] = | ||
kt.liftK(builder.emit) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the unfortunate part.
The spec explicitly says the API must accept the
Context
: