-
Notifications
You must be signed in to change notification settings - Fork 36
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
Observable instruments #162
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
|
||
package org.typelevel.otel4s.metrics | ||
|
||
trait ObservableCounter[F[_], A] | ||
trait ObservableCounter | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
|
||
package org.typelevel.otel4s.metrics | ||
|
||
trait ObservableGauge[F[_], A] | ||
trait ObservableGauge |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||||||||||||||||
/* | ||||||||||||||||||||||
* Copyright 2022 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.metrics | ||||||||||||||||||||||
|
||||||||||||||||||||||
import org.typelevel.otel4s.Attribute | ||||||||||||||||||||||
|
||||||||||||||||||||||
trait ObservableMeasurement[F[_], A] { | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** Records a value with a set of attributes. | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param value | ||||||||||||||||||||||
* the value to record | ||||||||||||||||||||||
* | ||||||||||||||||||||||
* @param attributes | ||||||||||||||||||||||
* the set of attributes to associate with the value | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
def record(value: A, attributes: Attribute[_]*): F[Unit] | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scaladoc:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @christiankjaer it would be nice to preserve laziness via macro. It should be nearly identical to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. The reason why I didn't look at the macro is that I kinda assumed that the observable stuff is for instrumenting from the outside, and the noop is then rarely needed. I will take a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point. Since the callback is a function So, we can keep it as-is and skip macro. |
||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
|
||
package org.typelevel.otel4s.metrics | ||
|
||
trait ObservableUpDownCounter[F, A] | ||
trait ObservableUpDownCounter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2022 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 | ||
package metrics | ||
|
||
import cats.effect.IO | ||
import cats.effect.Ref | ||
import cats.effect.Resource | ||
import munit.CatsEffectSuite | ||
|
||
class ObservableSuite extends CatsEffectSuite { | ||
|
||
import ObservableSuite._ | ||
|
||
test("observable test") { | ||
|
||
for { | ||
_ <- new InMemoryObservableInstrumentBuilder[Double] | ||
.createWithCallback(instrument => | ||
instrument.record(2.0) *> instrument.record(3.0) | ||
) | ||
.use { r => | ||
for { | ||
_ <- r.observations.get.assertEquals(List.empty) | ||
_ <- r.run | ||
_ <- r.observations.get.assertEquals( | ||
List(Record(3.0, Seq.empty), Record(2.0, Seq.empty)) | ||
) | ||
} yield () | ||
} | ||
} yield () | ||
|
||
} | ||
|
||
} | ||
|
||
object ObservableSuite { | ||
|
||
final case class Record[A](value: A, attributes: Seq[Attribute[_]]) | ||
|
||
final case class InMemoryObservable[A]( | ||
callback: ObservableMeasurement[IO, A] => IO[Unit], | ||
observations: Ref[IO, List[Record[A]]] | ||
) { | ||
def run: IO[Unit] = | ||
callback(new ObservableMeasurement[IO, A] { | ||
def record(value: A, attributes: Attribute[_]*): IO[Unit] = | ||
observations.update(Record(value, attributes) :: _) | ||
}) | ||
} | ||
|
||
class InMemoryObservableInstrumentBuilder[A] | ||
extends ObservableInstrumentBuilder[IO, A, InMemoryObservable[A]] { | ||
|
||
type Self = | ||
ObservableInstrumentBuilder[IO, A, InMemoryObservable[A]] | ||
|
||
def withUnit(unit: String): Self = this | ||
|
||
def withDescription(description: String): Self = this | ||
|
||
def createWithCallback( | ||
cb: ObservableMeasurement[IO, A] => IO[Unit] | ||
): Resource[IO, InMemoryObservable[A]] = | ||
Resource | ||
.eval(Ref.of[IO, List[Record[A]]](List.empty)) | ||
.map(obs => InMemoryObservable[A](cb, obs)) | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2022 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. | ||
*/ | ||
|
||
import cats.effect.IO | ||
import cats.effect.IOApp | ||
import cats.effect.Resource | ||
import io.opentelemetry.api.GlobalOpenTelemetry | ||
import org.typelevel.otel4s.java.OtelJava | ||
import org.typelevel.otel4s.metrics.ObservableCounter | ||
|
||
import java.lang.management.ManagementFactory | ||
import javax.management.MBeanServer | ||
import javax.management.ObjectName | ||
|
||
object ObservableExample extends IOApp.Simple { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Thanks for the relevant example. |
||
|
||
val mbeanServer: MBeanServer = ManagementFactory.getPlatformMBeanServer | ||
val mbeanName = new ObjectName("cats.effect.metrics:type=CpuStarvation") | ||
|
||
def meterResource: Resource[IO, ObservableCounter] = | ||
Resource | ||
.eval(IO(GlobalOpenTelemetry.get)) | ||
.evalMap(OtelJava.forAsync[IO]) | ||
.evalMap(_.meterProvider.get("observable-example")) | ||
.flatMap( | ||
_.observableCounter("cats-effect-runtime-cpu-starvation-count") | ||
.withDescription("CE runtime starvation count") | ||
.createWithCallback(obs => | ||
IO( | ||
mbeanServer | ||
.getAttribute(mbeanName, "CpuStarvationCount") | ||
.asInstanceOf[Long] | ||
).flatMap(c => obs.record(c)) | ||
) | ||
) | ||
|
||
def run: IO[Unit] = meterResource.useForever | ||
} |
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.
Not sure what to do with these. It seems strange that they have these type parameters, when we really only use them in the callback. Now I tried to remove them, and that seems to aid in testing at least.
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.
I've been thinking about this for a while and I tend to agree.