Skip to content
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

Adds support for metadata providers #12

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions .github/workflows/deploy_docs.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "ab3a58b7209a17d781c0d1dbb3e1ff3da306bae8",
"version" : "1.20.3"
"revision" : "9f0c76544701845ad98716f3f6a774a892152bcb",
"version" : "1.26.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PackageDescription

let package = Package(
name: "swift-log-loki",
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v9), .visionOS(.v1)],
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v10), .visionOS(.v1)],
products: [
.library(name: "LoggingLoki", targets: ["LoggingLoki"]),
],
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![Coverage](https://codecov.io/gh/lovetodream/swift-log-loki/graph/badge.svg?token=Q70PZWS0T2)](https://codecov.io/gh/lovetodream/swift-log-loki)
[![Tests](https://github.com/lovetodream/swift-log-loki/actions/workflows/tests.yml/badge.svg)](https://github.com/lovetodream/swift-log-loki/actions/workflows/tests.yml)
[![Docs](https://github.com/lovetodream/swift-log-loki/actions/workflows/deploy_docs.yml/badge.svg)](https://github.com/lovetodream/swift-log-loki/actions/workflows/deploy_docs.yml)
[![Swift Versions](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flovetodream%2Fswift-log-loki%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/lovetodream/swift-log-loki)
[![Supported Platforms](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flovetodream%2Fswift-log-loki%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/lovetodream/swift-log-loki)

This library can be used as an implementation of Apple's [swift-log](https://github.com/apple/swift-log) interface that captures console logs from apps or services and sends them to [Grafana Loki](https://grafana.com/oss/loki).

Expand Down Expand Up @@ -59,7 +60,7 @@ try await withThrowingDiscardingTaskGroup { group in

## API documentation

For more information visit the [API reference](https://timozacherl.com/swift-log-loki/documentation/loggingloki/).
For more information visit the [API reference](https://swiftpackageindex.com/lovetodream/swift-log-loki/documentation/loggingloki).

## License

Expand Down
7 changes: 6 additions & 1 deletion Sources/LoggingLoki/LokiLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public struct LokiLogHandler<Clock: _Concurrency.Clock>: LogHandler, Sendable wh
/// Static labels sent to Loki, which should not depend on the context of a log message.
public var lokiLabels: [String: String]

public var metadataProvider: Logger.MetadataProvider?

/// Creates a log handler, which sends logs to Grafana Loki.
///
/// @Snippet(path: "swift-log-loki/Snippets/BasicUsage", slice: "setup")
Expand All @@ -28,16 +30,19 @@ public struct LokiLogHandler<Clock: _Concurrency.Clock>: LogHandler, Sendable wh
/// It will be sent to Loki as the `service` label.
/// - lokiLabels: Static labels sent to Loki, which should not depend on the context of a log message.
/// - processor: Backend service which manages and sends logs to Loki.
/// - metadataProvider: A MetadataProvider, used to automatically inject runtime-generated metadata to all logs.
public init(
label: String,
service: String = ProcessInfo.processInfo.processName,
lokiLabels: [String: String] = [:],
processor: LokiLogProcessor<Clock>
processor: LokiLogProcessor<Clock>,
metadataProvider: Logger.MetadataProvider? = nil
) {
self.label = label
self.service = service
self.lokiLabels = lokiLabels
self.processor = processor
self.metadataProvider = metadataProvider
}

/// This method is called when a `LogHandler` must emit a log message. There is no need for the `LogHandler` to
Expand Down
23 changes: 23 additions & 0 deletions Tests/LoggingLokiTests/LokiLogHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ final class LokiLogHandlerTests: XCTestCase {
processing.cancel()
}

func testMetadataPreparation() {
let metadata1 = LokiLogHandler<TestClock>.prepareMetadata(base: [:], provider: .init({ [:] }), explicit: [:])
XCTAssertEqual(metadata1, [:])
let metadata2 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: .init({ [:] }), explicit: [:])
XCTAssertEqual(metadata2, ["hello": "there"])
let metadata3 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: .init({ ["provided": "metadata"] }), explicit: [:])
XCTAssertEqual(metadata3, ["hello": "there", "provided": "metadata"])
let metadata4 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: .init({ ["provided": "metadata"] }), explicit: ["explicit": "metadata"])
XCTAssertEqual(metadata4, ["hello": "there", "provided": "metadata", "explicit": "metadata"])
let metadata5 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: nil, explicit: ["explicit": "metadata"])
XCTAssertEqual(metadata5, ["hello": "there", "explicit": "metadata"])
let metadata6 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: nil, explicit: nil)
XCTAssertEqual(metadata6, ["hello": "there"])
let metadata7 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: .init({ ["hello": "how are you"] }), explicit: nil)
XCTAssertEqual(metadata7, ["hello": "how are you"])
let metadata8 = LokiLogHandler<TestClock>.prepareMetadata(base: ["hello": "there"], provider: .init({ ["hello": "how are you"] }), explicit: ["hello": "I am fine"])
XCTAssertEqual(metadata8, ["hello": "I am fine"])
var handler = LokiLogHandler(label: "test", processor: .init(configuration: .init(lokiURL: "")))
handler[metadataKey: "key"] = "value"
XCTAssertEqual(handler.metadata, ["key": "value"])
XCTAssertEqual(handler[metadataKey: "key"], "value")
}

func checkIfLogExists(for transformer: TestTransformer, file: StaticString = #filePath, line: UInt = #line) throws {
let firstLog = try XCTUnwrap(transformer.logs?.first, file: file, line: line)

Expand Down
Loading