-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lwc-events: support duration for default value (#1670)
When wrapping timer events it can be useful for the default value to be a duration for mapping to percentile timer type.
- Loading branch information
1 parent
d013537
commit bf81f55
Showing
4 changed files
with
121 additions
and
5 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
115 changes: 115 additions & 0 deletions
115
atlas-lwc-events/src/test/scala/com/netflix/atlas/lwc/events/LwcEventDurationSuite.scala
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,115 @@ | ||
/* | ||
* Copyright 2014-2024 Netflix, Inc. | ||
* | ||
* 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 com.netflix.atlas.lwc.events | ||
|
||
import com.netflix.atlas.core.util.SortedTagMap | ||
import munit.FunSuite | ||
|
||
import java.lang.System.Logger | ||
import java.time.Duration | ||
|
||
class LwcEventDurationSuite extends FunSuite { | ||
|
||
import LwcEventDurationSuite.* | ||
|
||
private val sampleSpan: TestEvent = { | ||
TestEvent(SortedTagMap("app" -> "www", "node" -> "i-123"), Duration.ofMillis(42)) | ||
} | ||
|
||
private val sampleLwcEvent: LwcEvent = new TestSpan(sampleSpan) | ||
|
||
test("tagValue: exists") { | ||
assertEquals(sampleLwcEvent.tagValue("app"), "www") | ||
assertEquals(sampleLwcEvent.tagValue("node"), "i-123") | ||
} | ||
|
||
test("tagValue: enum") { | ||
assertEquals(sampleLwcEvent.tagValue("level"), "TRACE") | ||
} | ||
|
||
test("tagValue: missing") { | ||
assertEquals(sampleLwcEvent.tagValue("foo"), null) | ||
} | ||
|
||
test("tagValue: wrong type") { | ||
assertEquals(sampleLwcEvent.tagValue("duration"), null) | ||
} | ||
|
||
test("extractValue: exists") { | ||
assertEquals(sampleLwcEvent.extractValue("app"), "www") | ||
assertEquals(sampleLwcEvent.extractValue("node"), "i-123") | ||
assertEquals(sampleLwcEvent.extractValue("duration"), Duration.ofMillis(42)) | ||
} | ||
|
||
test("extractValue: missing") { | ||
assertEquals(sampleLwcEvent.extractValue("foo"), null) | ||
} | ||
|
||
test("defautl value") { | ||
assertEquals(sampleLwcEvent.value, Duration.ofMillis(42)) | ||
} | ||
|
||
test("toJson: raw event") { | ||
val expected = """{"tags":{"app":"www","node":"i-123"},"duration":42}""" | ||
assertEquals(sampleLwcEvent.toJson, expected) | ||
} | ||
|
||
test("toJson: row no columns") { | ||
val expected = """[]""" | ||
assertEquals(sampleLwcEvent.toJson(List.empty), expected) | ||
} | ||
|
||
test("toJson: row nested object") { | ||
val expected = """[42,{"app":"www","node":"i-123"}]""" | ||
assertEquals(sampleLwcEvent.toJson(List("duration", "tags")), expected) | ||
} | ||
|
||
test("toJson: row simple") { | ||
val expected = """[42,"www"]""" | ||
assertEquals(sampleLwcEvent.toJson(List("duration", "app")), expected) | ||
} | ||
} | ||
|
||
object LwcEventDurationSuite { | ||
|
||
case class TestEvent(tags: Map[String, String], duration: Duration) | ||
|
||
def extractSpanValue(span: TestEvent)(key: String): Any = { | ||
key match { | ||
case "tags" => span.tags | ||
case "duration" => span.duration | ||
case "level" => Logger.Level.TRACE | ||
case k => span.tags.getOrElse(k, null) | ||
} | ||
} | ||
|
||
class TestSpan(event: TestEvent) extends LwcEvent.Span { | ||
|
||
override def spanId: String = "test" | ||
|
||
override def parentId: String = "parent" | ||
|
||
override def rawEvent: Any = event | ||
|
||
override def timestamp: Long = 0L | ||
|
||
override def value: Any = event.duration | ||
|
||
override def extractValue(key: String): Any = { | ||
extractSpanValue(event)(key) | ||
} | ||
} | ||
} |