-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from amzn/metadata-provider
Add metadata provider.
- Loading branch information
Showing
8 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
50 changes: 50 additions & 0 deletions
50
Sources/SmokeOperations/MetadataProvider+smokeFramework.swift
This file contains 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,50 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
// | ||
// MetadataProvider+smokeFramework | ||
// SmokeOperations | ||
// | ||
|
||
import Logging | ||
import ServiceContextModule | ||
|
||
extension Logger.MetadataProvider { | ||
/// A metadata provider exposing the attributes of the current invocation. | ||
/// | ||
/// - Parameters: | ||
/// - internalRequestIdKey: The metadata key of the internalRequestId. Defaults to "internalRequestId". | ||
/// - incomingOperationKey: The metadata key of the incomingOperation. Defaults to "incomingOperation". | ||
/// - externalRequestIdKey: The metadata key of the externalRequestId. Defaults to "externalRequestId". | ||
/// - Returns: A metadata provider ready to use with Logging. | ||
public static func smokeFramework(internalRequestIdKey: String = "internalRequestId", | ||
incomingOperationKey: String = "incomingOperation", | ||
externalRequestIdKey: String = "externalRequestId") -> Logger.MetadataProvider { | ||
.init { | ||
guard let invocationContext = ServiceContext.current?.invocationContext else { return [:] } | ||
|
||
var metadataProvider: Logger.Metadata = [ | ||
internalRequestIdKey: "\(invocationContext.internalRequestId)", | ||
incomingOperationKey: "\(invocationContext.incomingOperation)", | ||
] | ||
|
||
if let externalRequestId = invocationContext.externalRequestId { | ||
metadataProvider[externalRequestIdKey] = "\(externalRequestId)" | ||
} | ||
|
||
return metadataProvider | ||
} | ||
} | ||
|
||
/// A metadata provider exposing the attributes of the current invocation with the default key names. | ||
public static let smokeFramework = Logger.MetadataProvider.smokeFramework() | ||
} |
This file contains 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
53 changes: 53 additions & 0 deletions
53
Sources/SmokeOperations/ServiceContext+invocationContext.swift
This file contains 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,53 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file 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. | ||
// | ||
// ServiceContext+invocationContext | ||
// SmokeOperations | ||
// | ||
|
||
import ServiceContextModule | ||
|
||
public struct InvocationContext { | ||
var internalRequestId: String | ||
var incomingOperation: String | ||
var externalRequestId: String? | ||
|
||
public init(internalRequestId: String, incomingOperation: String, externalRequestId: String?) { | ||
self.internalRequestId = internalRequestId | ||
self.incomingOperation = incomingOperation | ||
self.externalRequestId = externalRequestId | ||
} | ||
} | ||
|
||
extension ServiceContext { | ||
public var invocationContext: InvocationContext? { | ||
get { | ||
self[InvocationContextKey.self] | ||
} | ||
set { | ||
self[InvocationContextKey.self] = newValue | ||
} | ||
} | ||
} | ||
|
||
extension InvocationContext: CustomStringConvertible { | ||
public var description: String { | ||
return internalRequestId | ||
} | ||
} | ||
|
||
private enum InvocationContextKey: ServiceContextKey { | ||
typealias Value = InvocationContext | ||
|
||
static var nameOverride: String? = "smoke-framework-invocation-context" | ||
} |
This file contains 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
This file contains 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