|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This code is licensed under the MIT License. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files(the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions : |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +package com.microsoft.identity.common.java.opentelemetry |
| 24 | + |
| 25 | +import com.microsoft.identity.common.java.controllers.CommandDispatcher |
| 26 | +import io.opentelemetry.api.common.AttributeKey |
| 27 | +import io.opentelemetry.api.common.Attributes |
| 28 | +import io.opentelemetry.api.trace.Span |
| 29 | +import io.opentelemetry.api.trace.SpanContext |
| 30 | +import io.opentelemetry.api.trace.StatusCode |
| 31 | +import java.util.concurrent.TimeUnit |
| 32 | + |
| 33 | +interface IBenchmarkSpan { |
| 34 | + /** |
| 35 | + * Returns a list of status changes along with their timestamps in nanoseconds. |
| 36 | + **/ |
| 37 | + fun getStatuses(): List<Pair<String, Long>> |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the span name. |
| 41 | + **/ |
| 42 | + fun getSpanName(): String |
| 43 | + |
| 44 | + /** |
| 45 | + * The start time of the span in nanoseconds. |
| 46 | + **/ |
| 47 | + fun getStartTimeInNanoSeconds(): Long |
| 48 | + |
| 49 | + /** |
| 50 | + * The end time of the span in nanoseconds. |
| 51 | + **/ |
| 52 | + fun getEndTimeInNanoSeconds(): Long |
| 53 | + |
| 54 | + /** |
| 55 | + * # of concurrent active silent requests when this span is started. |
| 56 | + **/ |
| 57 | + fun getConcurrentSilentRequestSize(): Int |
| 58 | + |
| 59 | + /** |
| 60 | + * The exception recorded on this span, if any. |
| 61 | + **/ |
| 62 | + fun getException(): Throwable? |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * A span wrapper class for benchmarking purposes. |
| 67 | + * |
| 68 | + * @param originalSpan The original span to be wrapped. |
| 69 | + * @param printer The printer to print the benchmark results. |
| 70 | + * @param spanName The name of the span. |
| 71 | + **/ |
| 72 | +class BenchmarkSpan( |
| 73 | + val originalSpan: Span, |
| 74 | + val printer: IBenchmarkSpanPrinter, |
| 75 | + private val spanName: String) : Span, IBenchmarkSpan { |
| 76 | + |
| 77 | + // Pair of (status name, timestamp in nano seconds) |
| 78 | + val statuses : ArrayList<Pair<String, Long>> = arrayListOf() |
| 79 | + private var exception: Throwable? = null |
| 80 | + |
| 81 | + private var startTimeInNanoSeconds: Long = System.nanoTime() |
| 82 | + private var endTimeInNanoSeconds: Long = 0L |
| 83 | + |
| 84 | + // # of concurrent active silent requests when this span is started. |
| 85 | + private var concurrentSize = 1 |
| 86 | + |
| 87 | + override fun getStatuses(): List<Pair<String, Long>> { |
| 88 | + return statuses |
| 89 | + } |
| 90 | + |
| 91 | + override fun getSpanName(): String { |
| 92 | + return spanName |
| 93 | + } |
| 94 | + |
| 95 | + override fun getStartTimeInNanoSeconds(): Long { |
| 96 | + return startTimeInNanoSeconds |
| 97 | + } |
| 98 | + |
| 99 | + override fun getEndTimeInNanoSeconds(): Long { |
| 100 | + return endTimeInNanoSeconds |
| 101 | + } |
| 102 | + |
| 103 | + override fun getConcurrentSilentRequestSize(): Int { |
| 104 | + return concurrentSize |
| 105 | + } |
| 106 | + |
| 107 | + override fun getException(): Throwable? { |
| 108 | + return exception |
| 109 | + } |
| 110 | + |
| 111 | + fun start(){ |
| 112 | + startTimeInNanoSeconds = System.nanoTime() |
| 113 | + concurrentSize = CommandDispatcher.getSilentRequestActiveCount() |
| 114 | + } |
| 115 | + |
| 116 | + override fun end() { |
| 117 | + endTimeInNanoSeconds = System.nanoTime() |
| 118 | + printer.printAsync(this) |
| 119 | + return originalSpan.end() |
| 120 | + } |
| 121 | + |
| 122 | + override fun end(timestamp: Long, unit: TimeUnit) { |
| 123 | + endTimeInNanoSeconds = System.nanoTime() |
| 124 | + printer.printAsync(this) |
| 125 | + return originalSpan.end(timestamp, unit) |
| 126 | + } |
| 127 | + |
| 128 | + override fun <T : Any?> setAttribute( |
| 129 | + key: AttributeKey<T>, |
| 130 | + value: T |
| 131 | + ): Span? { |
| 132 | + statuses.add(Pair(key.toString(), System.nanoTime())) |
| 133 | + return originalSpan.setAttribute(key, value) |
| 134 | + } |
| 135 | + |
| 136 | + override fun addEvent( |
| 137 | + name: String, |
| 138 | + attributes: Attributes |
| 139 | + ): Span? { |
| 140 | + statuses.add(Pair(name, System.nanoTime())) |
| 141 | + return originalSpan.addEvent(name, attributes) |
| 142 | + } |
| 143 | + |
| 144 | + override fun addEvent( |
| 145 | + name: String, |
| 146 | + attributes: Attributes, |
| 147 | + timestamp: Long, |
| 148 | + unit: TimeUnit |
| 149 | + ): Span? { |
| 150 | + statuses.add(Pair(name, System.nanoTime())) |
| 151 | + return originalSpan.addEvent(name, attributes, timestamp, unit) |
| 152 | + } |
| 153 | + |
| 154 | + override fun setStatus( |
| 155 | + statusCode: StatusCode, |
| 156 | + description: String |
| 157 | + ): Span? { |
| 158 | + statuses.add(Pair("SetStatus:$statusCode", System.nanoTime())) |
| 159 | + return originalSpan.setStatus(statusCode, description) |
| 160 | + } |
| 161 | + |
| 162 | + override fun recordException( |
| 163 | + exception: Throwable, |
| 164 | + additionalAttributes: Attributes |
| 165 | + ): Span? { |
| 166 | + val timestamp = System.nanoTime() |
| 167 | + statuses.add(Pair("recordException", timestamp)) |
| 168 | + this.exception = exception |
| 169 | + return originalSpan.recordException(exception, additionalAttributes) |
| 170 | + } |
| 171 | + |
| 172 | + override fun updateName(name: String): Span? { |
| 173 | + return originalSpan.updateName(name) |
| 174 | + } |
| 175 | + |
| 176 | + override fun getSpanContext(): SpanContext? { |
| 177 | + return originalSpan.spanContext |
| 178 | + } |
| 179 | + |
| 180 | + override fun isRecording(): Boolean { |
| 181 | + return originalSpan.isRecording |
| 182 | + } |
| 183 | +} |
0 commit comments