Skip to content

Commit

Permalink
Add missing typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
steffan-westcott committed Dec 8, 2023
1 parent 48d6a4d commit 1b8ab2e
Show file tree
Hide file tree
Showing 34 changed files with 125 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

(defn- attribute-type-of
"Returns `AttributeType` inferred from type of `x`."
[x]
^AttributeType [x]
(cond (map? x) AttributeType/STRING
(coll? x) (cond (every? boolean? x) AttributeType/BOOLEAN_ARRAY
(every? integer? x) AttributeType/LONG_ARRAY
Expand Down
11 changes: 6 additions & 5 deletions clj-otel-api/src/steffan_westcott/clj_otel/api/baggage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
"Conversion and manipulation functions for
`io.opentelemetry.api.baggage.Baggage` objects."
(:require [steffan-westcott.clj-otel.context :as context])
(:import (io.opentelemetry.api.baggage Baggage BaggageBuilder BaggageEntry BaggageEntryMetadata)))
(:import (io.opentelemetry.api.baggage Baggage BaggageBuilder BaggageEntry BaggageEntryMetadata)
(io.opentelemetry.context Context)))

(defn get-baggage
"Gets the baggage from a given context, or the bound ot current context if
none is given. If no baggage is found in the context, empty baggage is returned."
([]
(^Baggage []
(get-baggage (context/dyn)))
([context]
(^Baggage [context]
(Baggage/fromContext context)))

(defn assoc-baggage
"Associates baggage with a context and returns the new context."
[context baggage]
^Context [context baggage]
(context/assoc-value context baggage))

(defn- BaggageEntry->value
Expand Down Expand Up @@ -47,5 +48,5 @@
"Converts a map to a `Baggage` instance. Each key in the map is either a
string or keyword. Each value in the map is either `value` or a vector
`[value metadata]`, where `value` and `metadata` are strings."
[m]
^Baggage [m]
(.build ^BaggageBuilder (reduce-kv put-entry (Baggage/builder) m)))
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
|`:version` | Instrumentation library version e.g. `\"1.0.0\"` (default: See `config.edn` resource file).
|`:schema-url` | URL of OpenTelemetry schema used by this instrumentation library (default: See `config.edn` resource file).
|`:open-telemetry`| `OpenTelemetry` instance to get meter from (default: global `OpenTelemetry` instance)."
([]
(^Meter []
(get-meter {}))
([{:keys [name version schema-url open-telemetry]
(^Meter
[{:keys [name version schema-url open-telemetry]
:or {name (:name default-library)
version (:version default-library)
schema-url (:schema-url default-library)}}]
Expand All @@ -55,7 +56,7 @@
(defn set-default-meter!
"Sets the default `io.opentelemetry.api.metrics.Meter` instance used when
creating instruments. Returns `meter`. See also [[get-meter]]."
[meter]
^Meter [meter]
(reset! default-meter meter))

(defn get-default-meter!
Expand Down
8 changes: 4 additions & 4 deletions clj-otel-api/src/steffan_westcott/clj_otel/api/otel.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

(defn get-noop
"Gets a no-op `OpenTelemetry` instance."
[]
^OpenTelemetry []
(OpenTelemetry/noop))

(defn get-global-otel!
Expand All @@ -20,7 +20,7 @@
autoconfiguration failed or otherwise did not occur) will also be set as the
global instance i.e. this function is side-effecting if no instance was
previously set."
[]
^OpenTelemetry []
(GlobalOpenTelemetry/get))

(defn set-global-otel!
Expand All @@ -33,14 +33,14 @@
(defn set-default-otel!
"Sets the default `OpenTelemetry` instance declared and used by `clj-otel`.
Returns `otel`."
[otel]
^OpenTelemetry [otel]
(reset! default-otel otel))

(defn get-default-otel!
"Gets the default `OpenTelemetry` instance declared and used by `clj-otel`. If
no instance has been previously set, this falls back to the global instance
declared by OpenTelemetry Java."
[]
^OpenTelemetry []
(if-let [otel @default-otel]
otel
(set-default-otel! (get-global-otel!))))
Expand Down
15 changes: 8 additions & 7 deletions clj-otel-api/src/steffan_westcott/clj_otel/api/trace/span.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
|`:version` | Instrumentation library version e.g. `\"1.0.0\"` (default: See `config.edn` resource file).
|`:schema-url` | URL of OpenTelemetry schema used by this instrumentation library (default: See `config.edn` resource file).
|`:open-telemetry`| `OpenTelemetry` instance to get tracer from (default: global `OpenTelemetry` instance)."
([]
(^Tracer []
(get-tracer {}))
([{:keys [name version schema-url open-telemetry]
(^Tracer
[{:keys [name version schema-url open-telemetry]
:or {name (:name default-library)
version (:version default-library)
schema-url (:schema-url default-library)}}]
Expand All @@ -39,7 +40,7 @@

(defn noop-tracer
"Gets a no-op tracer."
[]
^Tracer []
(get-tracer {:open-telemetry (otel/get-noop)}))

(defonce ^:private default-tracer
Expand All @@ -48,7 +49,7 @@
(defn set-default-tracer!
"Sets the default `io.opentelemetry.api.trace.Tracer` instance used when
creating spans. Returns `tracer`. See also [[get-tracer]]."
[tracer]
^Tracer [tracer]
(reset! default-tracer tracer))

(defn- get-default-tracer!
Expand Down Expand Up @@ -81,7 +82,7 @@
(Span/fromContext context)))

(defprotocol ^:private AsSpanContext
(^:no-doc span-context [x]))
(^:no-doc ^SpanContext span-context [x]))

(extend-protocol AsSpanContext
SpanContext
Expand All @@ -97,9 +98,9 @@
(defn get-span-context
"Returns the given `SpanContext`, or extracts it from the given span or
context. If no argument is given, extract from the bound or current context."
([]
(^SpanContext []
(get-span-context (get-span)))
([x]
(^SpanContext [x]
(span-context x)))

(defn- add-link
Expand Down
11 changes: 5 additions & 6 deletions clj-otel-api/src/steffan_westcott/clj_otel/context.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

(defn root
"Returns the root context that all other contexts are derived from."
[]
^Context []
(Context/root))

(defn current
"Returns the current context, which is stored in a thread local `Context`
object. If no such context exists, the root context is returned instead."
[]
^Context []
(Context/current))

(defn set-current!
Expand All @@ -36,7 +36,7 @@
(defn dyn
"Returns the bound context, which is stored in a Clojure dynamic var. If no
context is bound, the current context is returned instead."
[]
^Context []
(or *bound-context* (current)))

(defmacro bind-context!
Expand All @@ -61,8 +61,7 @@
(ContextKey/named (name k)))))

(defprotocol AsContextKey
(context-key [k]
"Coerces k to a `ContextKey`."))
(^ContextKey context-key [k] "Coerces k to a `ContextKey`."))

(extend-protocol AsContextKey
ContextKey
Expand Down Expand Up @@ -178,7 +177,7 @@
|----------------------|-------------|
|`:context` | Context to merge with (default: bound or current context).
|`:text-map-propagator`| Propagator used to extract data from the headers map (default: propagator set in global `OpenTelemetry` instance)."
([headers]
(^Context [headers]
(headers->merged-context headers {}))
(^Context
[headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
(defn w3c-baggage-propagator
"Returns an implementation of the [W3C specification for baggage header
propagation](https://www.w3.org/TR/baggage/)."
[]
^W3CBaggagePropagator []
(W3CBaggagePropagator/getInstance))
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
(defn w3c-trace-context-propagator
"Returns an implementation of the [W3C Trace Context propagation
protocol](https://www.w3.org/TR/trace-context/)."
[]
^W3CTraceContextPropagator []
(W3CTraceContextPropagator/getInstance))
6 changes: 3 additions & 3 deletions clj-otel-api/src/steffan_westcott/clj_otel/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
(java.util.concurrent TimeUnit)))

(defprotocol AsDuration
(duration [d]
"Coerce to a `Duration` instance."))
(^Duration duration [d] "Coerce to a `Duration` instance."))

(extend-protocol AsDuration
Duration
Expand All @@ -33,7 +32,8 @@
t))

(defprotocol AsQualifiedName
(qualified-name [x]
(^String qualified-name
[x]
"Given a keyword or symbol, returns the name converted to follow
OpenTelemetry conventions for attribute names; the name is converted to a
snake_case string, where namespace and name are separated by `.`. Given any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@
Ec2Resource
EcsResource
EksResource
LambdaResource)))
LambdaResource)
(io.opentelemetry.sdk.resources Resource)))

(defn beanstalk-resource
"Returns a `Resource` which provides information about the current EC2
instance if running on AWS Elastic Beanstalk."
[]
^Resource []
(BeanstalkResource/get))

(defn ec2-resource
"Returns a `Resource` which provides information about the current EC2
instance if running on AWS EC2."
[]
^Resource []
(Ec2Resource/get))

(defn ecs-resource
"Returns a `Resource` which provides information about the current ECS
container if running on AWS ECS."
[]
^Resource []
(EcsResource/get))

(defn eks-resource
"Returns a `Resource` which provides information about the current ECS
container if running on AWS EKS."
[]
^Resource []
(EksResource/get))

(defn lambda-resource
"Returns a `Resource` which provides information about the AWS Lambda
function."
[]
^Resource []
(LambdaResource/get))
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
(defn aws-xray-propagator
"Returns an implementation of the [AWS X-Ray Trace header propagation
protocol](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader)."
[]
^AwsXrayPropagator []
(AwsXrayPropagator/getInstance))
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
|`:x509-trust-manager` | `^X509TrustManager` \"bring your own SSLContext\" alternative to setting certificate bytes when using TLS.
|`:timeout` | Maximum time to wait for export of a batch of spans. Value is either a `Duration` or a vector `[amount ^TimeUnit unit]` (default: 10s).
|`:compression` | Method used to compress payloads. Value is string `\"gzip\"` or `\"none\"` (default: `\"none\"`)."
([]
(^JaegerGrpcSpanExporter []
(span-exporter {}))
([{:keys [endpoint trusted-certificates-pem client-private-key-pem client-certificates-pem
(^JaegerGrpcSpanExporter
[{:keys [endpoint trusted-certificates-pem client-private-key-pem client-certificates-pem
ssl-context x509-trust-manager timeout compression]}]
(let [builder (cond-> (JaegerGrpcSpanExporter/builder)
endpoint (.setEndpoint endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
|-----------|-------------|
|`:endpoint`| Jaeger endpoint, used with the default sender (default: `\"http://localhost:14268/api/traces\"`).
|`:sender` | `ThriftSender` instance to use for communication with the backend (default: `ThriftSender` instance configured to use `:endpoint`)."
([]
(^JaegerThriftSpanExporter []
(span-exporter {}))
([{:keys [endpoint thrift-sender]}]
(^JaegerThriftSpanExporter [{:keys [endpoint thrift-sender]}]
(let [builder (cond-> (JaegerThriftSpanExporter/builder)
endpoint (.setEndpoint endpoint)
thrift-sender (.setThriftSender thrift-sender))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
"Exporters that log telemetry data in OTLP JSON format using
`java.util.logging`. Intended for debugging only."
(:import (io.opentelemetry.exporter.logging.otlp OtlpJsonLoggingMetricExporter
OtlpJsonLoggingSpanExporter)))
OtlpJsonLoggingSpanExporter)
(io.opentelemetry.sdk.metrics.export MetricExporter)
(io.opentelemetry.sdk.trace.export SpanExporter)))

(defn span-exporter
"Returns a span exporter that logs every span in OTLP JSON format using
`java.util.logging`."
[]
^SpanExporter []
(OtlpJsonLoggingSpanExporter/create))

(defn metric-exporter
"Returns a metric exporter that logs every metric in OTLP JSON format using
`java.util.logging`."
[]
^MetricExporter []
(OtlpJsonLoggingMetricExporter/create))
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

(defn span-exporter
"Returns a span exporter that logs every span using `java.util.logging`."
[]
^LoggingSpanExporter []
(LoggingSpanExporter/create))

(defn metric-exporter
Expand All @@ -16,8 +16,9 @@
| key | description |
|--------------------------|-------------|
|`:aggregation-temporality`| ^AggregationTemporality Time period over which metrics should be aggregated (default: `CUMULATIVE`)."
([]
(^LoggingMetricExporter []
(LoggingMetricExporter/create))
([{:keys [aggregation-temporality]
(^LoggingMetricExporter
[{:keys [aggregation-temporality]
:or {aggregation-temporality AggregationTemporality/CUMULATIVE}}]
(LoggingMetricExporter/create aggregation-temporality)))
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
|`:timeout` | Maximum time to wait for export of a batch of spans. Value is either a `Duration` or a vector `[amount ^TimeUnit unit]` (default: 10s).
|`:aggregation-temporality-selector`| Function which takes an `InstrumentType` and returns an `AggregationTemporality` (default: same as constantly `AggregationTemporality/CUMULATIVE`).
|`:default-aggregation-selector` | Function which takes an `InstrumentType` and returns default `Aggregation` (default: same as `DefaultAggregationSelector/getDefault`)."
([]
(^OtlpGrpcMetricExporter []
(metric-exporter {}))
([{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
(^OtlpGrpcMetricExporter
[{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
ssl-context x509-trust-manager compression-method timeout
aggregation-temporality-selector default-aggregation-selector]}]
(let [builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
|`:compression-method` | Method used to compress payloads, `\"gzip\"` or `\"none\"` (default: `\"none\"`).
|`:timeout` | Maximum time to wait for export of a batch of spans. Value is either a `Duration` or a vector `[amount ^TimeUnit unit]` (default: 10s).
|`:meter-provider` | ^MeterProvider to collect metrics related to export (default: metrics not collected)."
([]
(^OtlpGrpcSpanExporter []
(span-exporter {}))
([{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
(^OtlpGrpcSpanExporter
[{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
ssl-context x509-trust-manager compression-method timeout meter-provider]}]
(let [builder (cond-> (OtlpGrpcSpanExporter/builder)
endpoint (.setEndpoint endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
|`:compression-method` | Method used to compress payloads, `\"gzip\"` or `\"none\"` (default: `\"none\"`).
|`:timeout` | Maximum time to wait for export of a batch of spans. Value is either a `Duration` or a vector `[amount ^TimeUnit unit]` (default: 10s).
|`:aggregation-temporality-selector`| Function which takes an `InstrumentType` and returns an `AggregationTemporality` (default: constantly `AggregationTemporality/CUMULATIVE`)."
([]
(^OtlpHttpMetricExporter []
(metric-exporter {}))
([{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
(^OtlpHttpMetricExporter
[{:keys [endpoint headers trusted-certificates-pem client-private-key-pem client-certificates-pem
compression-method timeout aggregation-temporality-selector]}]
(let [builder (cond-> (OtlpHttpMetricExporter/builder)
endpoint (.setEndpoint endpoint)
Expand Down
Loading

0 comments on commit 1b8ab2e

Please sign in to comment.