Merge pull request #56 from tatchi/tatchi/per-signals-url

add support for per-signal urls
This commit is contained in:
Simon Cruanes 2024-08-12 10:14:57 -04:00 committed by GitHub
commit 4f6cf08041
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 547 additions and 128 deletions

View file

@ -14,12 +14,30 @@ let debug_ =
let default_url = "http://localhost:4318"
let url =
ref (try Sys.getenv "OTEL_EXPORTER_OTLP_ENDPOINT" with _ -> default_url)
let make_get_from_env env_name =
let value = ref None in
fun () ->
match !value with
| None ->
value := Sys.getenv_opt env_name;
!value
| Some value -> Some value
let get_url () = !url
let get_url_from_env = make_get_from_env "OTEL_EXPORTER_OTLP_ENDPOINT"
let set_url s = url := s
let get_url_traces_from_env =
make_get_from_env "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
let get_url_metrics_from_env =
make_get_from_env "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
let get_url_logs_from_env = make_get_from_env "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
let remove_trailing_slash url =
if url <> "" && String.get url (String.length url - 1) = '/' then
String.sub url 0 (String.length url - 1)
else
url
let parse_headers s =
let parse_header s =

View file

@ -2,7 +2,9 @@ open Common_
type t = {
debug: bool;
url: string;
url_traces: string;
url_metrics: string;
url_logs: string;
headers: (string * string) list;
batch_traces: int option;
batch_metrics: int option;
@ -16,7 +18,9 @@ let pp out self : unit =
let ppheaders = Format.pp_print_list pp_header in
let {
debug;
url;
url_traces;
url_metrics;
url_logs;
headers;
batch_traces;
batch_metrics;
@ -26,17 +30,47 @@ let pp out self : unit =
self
in
Format.fprintf out
"{@[ debug=%B;@ url=%S;@ headers=%a;@ batch_traces=%a;@ batch_metrics=%a;@ \
batch_logs=%a;@ batch_timeout_ms=%d; @]}"
debug url ppheaders headers ppiopt batch_traces ppiopt batch_metrics ppiopt
batch_logs batch_timeout_ms
"{@[ debug=%B;@ url_traces=%S;@ url_metrics=%S;@ url_logs=%S;@ \
headers=%a;@ batch_traces=%a;@ batch_metrics=%a;@ batch_logs=%a;@ \
batch_timeout_ms=%d; @]}"
debug url_traces url_metrics url_logs ppheaders headers ppiopt batch_traces
ppiopt batch_metrics ppiopt batch_logs batch_timeout_ms
let make ?(debug = !debug_) ?(url = get_url ()) ?(headers = get_headers ())
?(batch_traces = Some 400) ?(batch_metrics = Some 20)
?(batch_logs = Some 400) ?(batch_timeout_ms = 500) () : t =
let make ?(debug = !debug_) ?url ?url_traces ?url_metrics ?url_logs
?(headers = get_headers ()) ?(batch_traces = Some 400)
?(batch_metrics = Some 20) ?(batch_logs = Some 400)
?(batch_timeout_ms = 500) () : t =
let url_traces, url_metrics, url_logs =
let base_url =
let base_url =
match get_url_from_env () with
| None -> Option.value url ~default:default_url
| Some url -> remove_trailing_slash url
in
remove_trailing_slash base_url
in
let url_traces =
match get_url_traces_from_env () with
| None -> Option.value url_traces ~default:(base_url ^ "/v1/traces")
| Some url -> url
in
let url_metrics =
match get_url_metrics_from_env () with
| None -> Option.value url_metrics ~default:(base_url ^ "/v1/metrics")
| Some url -> url
in
let url_logs =
match get_url_logs_from_env () with
| None -> Option.value url_logs ~default:(base_url ^ "/v1/logs")
| Some url -> url
in
url_traces, url_metrics, url_logs
in
{
debug;
url;
url_traces;
url_metrics;
url_logs;
headers;
batch_traces;
batch_metrics;

View file

@ -1,8 +1,8 @@
type t = private {
debug: bool;
url: string;
(** Url of the endpoint. Default is "http://localhost:4318",
or "OTEL_EXPORTER_OTLP_ENDPOINT" if set. *)
url_traces: string; (** Url to send traces *)
url_metrics: string; (** Url to send metrics*)
url_logs: string; (** Url to send logs *)
headers: (string * string) list;
(** API headers sent to the endpoint. Default is none or
"OTEL_EXPORTER_OTLP_HEADERS" if set. *)
@ -37,6 +37,9 @@ type t = private {
val make :
?debug:bool ->
?url:string ->
?url_traces:string ->
?url_metrics:string ->
?url_logs:string ->
?headers:(string * string) list ->
?batch_traces:int option ->
?batch_metrics:int option ->
@ -49,6 +52,25 @@ val make :
@param thread if true and [bg_threads] is not provided, we will pick a number
of bg threads. Otherwise the number of [bg_threads] superseeds this option.
@param url base url used to construct per-signal urls. Per-signal url options take precedence over this base url.
Default is "http://localhost:4318", or "OTEL_EXPORTER_OTLP_ENDPOINT" if set.
Example of constructed per-signal urls with the base url http://localhost:4318
- Traces: http://localhost:4318/v1/traces
- Metrics: http://localhost:4318/v1/metrics
- Logs: http://localhost:4318/v1/logs
Use per-signal url options if different urls are needed for each signal type.
@param url_traces url to send traces, or "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" if set.
The url is used as-is without any modification.
@param url_metrics url to send metrics, or "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT" if set.
The url is used as-is without any modification.
@param url_logs url to send logs, or "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT" if set.
The url is used as-is without any modification.
*)
val pp : Format.formatter -> t -> unit

View file

@ -72,8 +72,7 @@ module Httpc : sig
val send :
t ->
config:Config.t ->
path:string ->
url:string ->
decode:[ `Dec of Pbrt.Decoder.t -> 'a | `Ret of 'a ] ->
string ->
('a, error) result Lwt.t
@ -91,17 +90,8 @@ end = struct
let cleanup _self = ()
(* send the content to the remote endpoint/path *)
let send (_self : t) ~(config : Config.t) ~path ~decode (bod : string) :
('a, error) result Lwt.t =
let url =
let url = config.url in
if url <> "" && String.get url (String.length url - 1) = '/' then
String.sub url 0 (String.length url - 1)
else
url
in
let full_url = url ^ path in
let uri = Uri.of_string full_url in
let send (_self : t) ~url ~decode (bod : string) : ('a, error) result Lwt.t =
let uri = Uri.of_string url in
let open Cohttp in
let headers = Header.(add_list (init ()) !headers) in
@ -121,7 +111,7 @@ end = struct
| Error e ->
let err =
`Failure
(spf "sending signals via http POST to %S\nfailed with:\n%s" full_url
(spf "sending signals via http POST to %S\nfailed with:\n%s" url
(Printexc.to_string e))
in
Lwt.return @@ Error err
@ -158,7 +148,7 @@ end = struct
%s\n\
status: %S\n\
%s"
full_url code (Printexc.to_string e) body bt))
url code (Printexc.to_string e) body bt))
in
Lwt.return r
)
@ -292,11 +282,11 @@ let mk_emitter ~stop ~(config : Config.t) () : (module EMITTER) =
let set_on_tick_callbacks = Atomic.set on_tick_cbs_
let send_http_ (httpc : Httpc.t) encoder ~path ~encode x : unit Lwt.t =
let send_http_ (httpc : Httpc.t) encoder ~url ~encode x : unit Lwt.t =
Pbrt.Encoder.reset encoder;
encode x encoder;
let data = Pbrt.Encoder.to_string encoder in
let* r = Httpc.send httpc ~config ~path ~decode:(`Ret ()) data in
let* r = Httpc.send httpc ~url ~decode:(`Ret ()) data in
match r with
| Ok () -> Lwt.return ()
| Error `Sysbreak ->
@ -317,7 +307,8 @@ let mk_emitter ~stop ~(config : Config.t) () : (module EMITTER) =
Metrics_service.default_export_metrics_service_request
~resource_metrics:l ()
in
send_http_ curl encoder ~path:"/v1/metrics"
let url = config.Config.url_metrics in
send_http_ curl encoder ~url
~encode:Metrics_service.encode_pb_export_metrics_service_request x
let send_traces_http curl encoder (l : Trace.resource_spans list list) =
@ -325,7 +316,8 @@ let mk_emitter ~stop ~(config : Config.t) () : (module EMITTER) =
let x =
Trace_service.default_export_trace_service_request ~resource_spans:l ()
in
send_http_ curl encoder ~path:"/v1/traces"
let url = config.Config.url_traces in
send_http_ curl encoder ~url
~encode:Trace_service.encode_pb_export_trace_service_request x
let send_logs_http curl encoder (l : Logs.resource_logs list list) =
@ -333,7 +325,8 @@ let mk_emitter ~stop ~(config : Config.t) () : (module EMITTER) =
let x =
Logs_service.default_export_logs_service_request ~resource_logs:l ()
in
send_http_ curl encoder ~path:"/v1/logs"
let url = config.Config.url_logs in
send_http_ curl encoder ~url
~encode:Logs_service.encode_pb_export_logs_service_request x
(* emit metrics, if the batch is full or timeout lapsed *)
@ -459,7 +452,8 @@ let mk_emitter ~stop ~(config : Config.t) () : (module EMITTER) =
end in
(module M)
module Backend (Arg : sig
module Backend
(Arg : sig
val stop : bool Atomic.t
val config : Config.t
@ -560,8 +554,6 @@ end
let create_backend ?(stop = Atomic.make false) ?(config = Config.make ()) () =
debug_ := config.debug;
if config.url <> get_url () then set_url config.url;
let module B =
Backend
(struct

View file

@ -5,12 +5,6 @@
open Common_
val get_url : unit -> string
val set_url : string -> unit
(** Url of the endpoint. Default is "http://localhost:4318",
or "OTEL_EXPORTER_OTLP_ENDPOINT" if set. *)
val get_headers : unit -> (string * string) list
val set_headers : (string * string) list -> unit

View file

@ -15,12 +15,30 @@ let debug_ =
let default_url = "http://localhost:4318"
let url =
ref (try Sys.getenv "OTEL_EXPORTER_OTLP_ENDPOINT" with _ -> default_url)
let make_get_from_env env_name =
let value = ref None in
fun () ->
match !value with
| None ->
value := Sys.getenv_opt env_name;
!value
| Some value -> Some value
let get_url () = !url
let get_url_from_env = make_get_from_env "OTEL_EXPORTER_OTLP_ENDPOINT"
let set_url s = url := s
let get_url_traces_from_env =
make_get_from_env "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
let get_url_metrics_from_env =
make_get_from_env "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
let get_url_logs_from_env = make_get_from_env "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
let remove_trailing_slash url =
if url <> "" && String.get url (String.length url - 1) = '/' then
String.sub url 0 (String.length url - 1)
else
url
let parse_headers s =
let parse_header s =

View file

@ -2,7 +2,9 @@ open Common_
type t = {
debug: bool;
url: string;
url_traces: string;
url_metrics: string;
url_logs: string;
headers: (string * string) list;
batch_timeout_ms: int;
bg_threads: int;
@ -16,7 +18,9 @@ let pp out self =
let ppheaders = Format.pp_print_list pp_header in
let {
debug;
url;
url_traces;
url_metrics;
url_logs;
headers;
batch_timeout_ms;
bg_threads;
@ -27,18 +31,49 @@ let pp out self =
self
in
Format.fprintf out
"{@[ debug=%B;@ url=%S;@ headers=%a;@ batch_timeout_ms=%d; bg_threads=%d;@ \
ticker_thread=%B;@ ticker_interval_ms=%d;@ self_trace=%B @]}"
debug url ppheaders headers batch_timeout_ms bg_threads ticker_thread
ticker_interval_ms self_trace
"{@[ debug=%B;@ url_traces=%S;@ url_metrics=%S;@ url_logs=%S;@ \
headers=%a;@ batch_timeout_ms=%d; bg_threads=%d;@ ticker_thread=%B;@ \
ticker_interval_ms=%d;@ self_trace=%B @]}"
debug url_traces url_metrics url_logs ppheaders headers batch_timeout_ms
bg_threads ticker_thread ticker_interval_ms self_trace
let make ?(debug = !debug_) ?(url = get_url ()) ?(headers = get_headers ())
?(batch_timeout_ms = 2_000) ?(bg_threads = 4) ?(ticker_thread = true)
?(ticker_interval_ms = 500) ?(self_trace = false) () : t =
let make ?(debug = !debug_) ?url ?url_traces ?url_metrics ?url_logs
?(headers = get_headers ()) ?(batch_timeout_ms = 2_000) ?(bg_threads = 4)
?(ticker_thread = true) ?(ticker_interval_ms = 500) ?(self_trace = false) ()
: t =
let bg_threads = max 1 (min bg_threads 32) in
let url_traces, url_metrics, url_logs =
let base_url =
let base_url =
match get_url_from_env () with
| None -> Option.value url ~default:default_url
| Some url -> remove_trailing_slash url
in
remove_trailing_slash base_url
in
let url_traces =
match get_url_traces_from_env () with
| None -> Option.value url_traces ~default:(base_url ^ "/v1/traces")
| Some url -> url
in
let url_metrics =
match get_url_metrics_from_env () with
| None -> Option.value url_metrics ~default:(base_url ^ "/v1/metrics")
| Some url -> url
in
let url_logs =
match get_url_logs_from_env () with
| None -> Option.value url_logs ~default:(base_url ^ "/v1/logs")
| Some url -> url
in
url_traces, url_metrics, url_logs
in
{
debug;
url;
url_traces;
url_metrics;
url_logs;
headers;
batch_timeout_ms;
bg_threads;

View file

@ -2,9 +2,9 @@
type t = private {
debug: bool;
url: string;
(** Url of the endpoint. Default is "http://localhost:4318",
or "OTEL_EXPORTER_OTLP_ENDPOINT" if set. *)
url_traces: string; (** Url to send traces *)
url_metrics: string; (** Url to send metrics*)
url_logs: string; (** Url to send logs *)
headers: (string * string) list;
(** API headers sent to the endpoint. Default is none or
"OTEL_EXPORTER_OTLP_HEADERS" if set. *)
@ -39,6 +39,9 @@ type t = private {
val make :
?debug:bool ->
?url:string ->
?url_traces:string ->
?url_metrics:string ->
?url_logs:string ->
?headers:(string * string) list ->
?batch_timeout_ms:int ->
?bg_threads:int ->
@ -48,6 +51,25 @@ val make :
unit ->
t
(** Make a configuration.
@param url base url used to construct per-signal urls. Per-signal url options take precedence over this base url.
Default is "http://localhost:4318", or "OTEL_EXPORTER_OTLP_ENDPOINT" if set.
Example of constructed per-signal urls with the base url http://localhost:4318
- Traces: http://localhost:4318/v1/traces
- Metrics: http://localhost:4318/v1/metrics
- Logs: http://localhost:4318/v1/logs
Use per-signal url options if different urls are needed for each signal type.
@param url_traces url to send traces, or "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT" if set.
The url is used as-is without any modification.
@param url_metrics url to send metrics, or "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT" if set.
The url is used as-is without any modification.
@param url_logs url to send logs, or "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT" if set.
The url is used as-is without any modification.
*)
val pp : Format.formatter -> t -> unit

View file

@ -145,8 +145,7 @@ end = struct
mutable send_threads: Thread.t array; (** Threads that send data via http *)
}
let send_http_ ~stop ~config (client : Curl.t) encoder ~path ~encode x : unit
=
let send_http_ ~stop ~config (client : Curl.t) encoder ~url ~encode x : unit =
let@ _sc =
Self_trace.with_ ~kind:Span.Span_kind_producer "otel-ocurl.send-http"
in
@ -160,15 +159,7 @@ end = struct
Pbrt.Encoder.to_string encoder
in
let url =
let url = config.Config.url in
if url <> "" && String.get url (String.length url - 1) = '/' then
String.sub url 0 (String.length url - 1)
else
url
in
let url = url ^ path in
if !debug_ || config.debug then
if !debug_ || config.Config.debug then
Printf.eprintf "opentelemetry: send http POST to %s (%dB)\n%!" url
(String.length data);
let headers =
@ -227,7 +218,7 @@ end = struct
let x =
Logs_service.default_export_logs_service_request ~resource_logs:l ()
in
send_http_ ~stop ~config client encoder ~path:"/v1/logs"
send_http_ ~stop ~config client encoder ~url:config.Config.url_logs
~encode:Logs_service.encode_pb_export_logs_service_request x
let send_metrics_http ~stop ~config curl encoder
@ -242,7 +233,7 @@ end = struct
Metrics_service.default_export_metrics_service_request ~resource_metrics:l
()
in
send_http_ ~stop ~config curl encoder ~path:"/v1/metrics"
send_http_ ~stop ~config curl encoder ~url:config.Config.url_metrics
~encode:Metrics_service.encode_pb_export_metrics_service_request x
let send_traces_http ~stop ~config curl encoder
@ -256,7 +247,7 @@ end = struct
let x =
Trace_service.default_export_trace_service_request ~resource_spans:l ()
in
send_http_ ~stop ~config curl encoder ~path:"/v1/traces"
send_http_ ~stop ~config curl encoder ~url:config.Config.url_traces
~encode:Trace_service.encode_pb_export_trace_service_request x
let[@inline] send_event (self : t) ev : unit = B_queue.push self.q ev
@ -514,7 +505,6 @@ let setup_ ?(stop = Atomic.make false) ?(config : Config.t = Config.make ()) ()
let ((module B) as backend) = create_backend ~stop ~config () in
Opentelemetry.Collector.set_backend backend;
if config.url <> get_url () then set_url config.url;
Atomic.set Self_trace.enabled config.self_trace;
if config.ticker_thread then (

View file

@ -3,12 +3,6 @@
https://opentelemetry.io/docs/reference/specification/protocol/exporter/
*)
val get_url : unit -> string
val set_url : string -> unit
(** Url of the endpoint. Default is "http://localhost:4318",
or "OTEL_EXPORTER_OTLP_ENDPOINT" if set. *)
val get_headers : unit -> (string * string) list
val set_headers : (string * string) list -> unit

View file

@ -13,8 +13,6 @@ let mk_client ~scope =
Opentelemetry_cohttp_lwt.client ~scope (module Cohttp_lwt_unix.Client)
let run () =
Printf.printf "collector is on %S\n%!"
(Opentelemetry_client_cohttp_lwt.get_url ());
let open Lwt.Syntax in
let rec go () =
let@ scope =

View file

@ -78,7 +78,6 @@ let run_job () =
done
let run () =
Printf.printf "collector is on %S\n%!" (Opentelemetry_client_ocurl.get_url ());
T.GC_metrics.basic_setup ();
T.Metrics_callbacks.register (fun () ->

View file

@ -74,8 +74,6 @@ let run_job () : unit Lwt.t =
done
let run () : unit Lwt.t =
Printf.printf "collector is on %S\n%!"
(Opentelemetry_client_cohttp_lwt.get_url ());
T.GC_metrics.basic_setup ();
T.Metrics_callbacks.register (fun () ->

View file

@ -1,4 +1,4 @@
(tests
(names test_get_url)
(names test_urls)
(package opentelemetry-client-cohttp-lwt)
(libraries opentelemetry opentelemetry-client-cohttp-lwt))

View file

@ -1 +0,0 @@
cohttp url = http://localhost:3000

View file

@ -1,9 +0,0 @@
let url = "http://localhost:3000"
let cohttp () =
let config = Opentelemetry_client_cohttp_lwt.Config.make ~url () in
Opentelemetry_client_cohttp_lwt.with_setup ~config () @@ fun () ->
let url = Opentelemetry_client_cohttp_lwt.get_url () in
print_endline @@ Printf.sprintf "cohttp url = %s" url
let () = cohttp ()

View file

@ -0,0 +1,66 @@
--- default_url ---
url_traces = http://localhost:4318/v1/traces
url_metrics = http://localhost:4318/v1/metrics
url_logs = http://localhost:4318/v1/logs
------
--- base_url_from_config ---
url_traces = http://localhost:3000/v1/traces
url_metrics = http://localhost:3000/v1/metrics
url_logs = http://localhost:3000/v1/logs
------
--- base_url_from_env ---
url_traces = http://localhost:5000/v1/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- base_url_from_both_config_and_env ---
url_traces = http://localhost:5000/v1/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_both_config_and_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- set_all_in_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- set_all_in_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- remove_trailing_slash_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- remove_trailing_slash_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------

97
tests/cohttp/test_urls.ml Normal file
View file

@ -0,0 +1,97 @@
open Opentelemetry_client_cohttp_lwt
let test_urls ~name config =
Printf.printf "--- %s ---\n" name;
Printf.printf "url_traces = %s\n" config.Config.url_traces;
Printf.printf "url_metrics = %s\n" config.Config.url_metrics;
Printf.printf "url_logs = %s\n" config.Config.url_logs;
print_endline "------\n"
let default_url () =
let config = Config.make () in
test_urls ~name:"default_url" config
let base_url_from_config () =
let config = Config.make ~url:"http://localhost:3000" () in
test_urls ~name:"base_url_from_config" config
let base_url_from_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:5000";
let config = Config.make () in
test_urls ~name:"base_url_from_env" config
let base_url_from_both_config_and_env () =
(* url from env should take precedence *)
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:5000";
let config = Config.make ~url:"http://localhost:3000" () in
test_urls ~name:"base_url_from_both_config_and_env" config
let override_trace_url_from_config () =
let config =
Config.make ~url:"http://localhost:3000"
~url_traces:"http://localhost:3001/send/traces" ()
in
test_urls ~name:"override_trace_url_from_config" config
let override_trace_url_from_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces";
let config = Config.make () in
test_urls ~name:"override_trace_url_from_env" config
let override_trace_url_from_both_config_and_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces/env";
let config =
Config.make ~url_traces:"http://localhost:3001/send/traces/config" ()
in
test_urls ~name:"override_trace_url_from_both_config_and_env" config
let set_all_in_config () =
let config =
Config.make ~url_traces:"http://localhost:3001/send/traces"
~url_metrics:"http://localhost:3002/send/metrics"
~url_logs:"http://localhost:3003/send/logs" ()
in
test_urls ~name:"set_all_in_config" config
let set_all_in_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces";
Unix.putenv "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
"http://localhost:3002/send/metrics";
Unix.putenv "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
"http://localhost:3003/send/logs";
let config = Config.make () in
test_urls ~name:"set_all_in_env" config
let remove_trailing_slash_config () =
let config = Config.make ~url:"http://localhost:3000/" () in
test_urls ~name:"remove_trailing_slash_config" config
let remove_trailing_slash_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:3000/";
let config = Config.make () in
test_urls ~name:"remove_trailing_slash_env" config
let () = default_url ()
let () = base_url_from_config ()
let () = base_url_from_env ()
let () = base_url_from_both_config_and_env ()
let () = override_trace_url_from_config ()
let () = override_trace_url_from_env ()
let () = override_trace_url_from_both_config_and_env ()
let () = set_all_in_config ()
let () = set_all_in_env ()
let () = remove_trailing_slash_config ()
let () = remove_trailing_slash_env ()

View file

@ -1,5 +1,4 @@
(tests
(names test_get_url)
(names test_urls)
(package opentelemetry-client-ocurl)
(libraries opentelemetry opentelemetry-client-ocurl))

View file

@ -1 +0,0 @@
ocurl url = http://localhost:3000

View file

@ -1,9 +0,0 @@
let url = "http://localhost:3000"
let ocurl () =
let config = Opentelemetry_client_ocurl.Config.make ~url () in
Opentelemetry_client_ocurl.with_setup ~config () @@ fun () ->
let url = Opentelemetry_client_ocurl.get_url () in
print_endline @@ Printf.sprintf "ocurl url = %s" url
let () = ocurl ()

View file

@ -0,0 +1,66 @@
--- default_url ---
url_traces = http://localhost:4318/v1/traces
url_metrics = http://localhost:4318/v1/metrics
url_logs = http://localhost:4318/v1/logs
------
--- base_url_from_config ---
url_traces = http://localhost:3000/v1/traces
url_metrics = http://localhost:3000/v1/metrics
url_logs = http://localhost:3000/v1/logs
------
--- base_url_from_env ---
url_traces = http://localhost:5000/v1/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- base_url_from_both_config_and_env ---
url_traces = http://localhost:5000/v1/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- override_trace_url_from_both_config_and_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:5000/v1/metrics
url_logs = http://localhost:5000/v1/logs
------
--- set_all_in_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- set_all_in_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- remove_trailing_slash_config ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------
--- remove_trailing_slash_env ---
url_traces = http://localhost:3001/send/traces
url_metrics = http://localhost:3002/send/metrics
url_logs = http://localhost:3003/send/logs
------

97
tests/ocurl/test_urls.ml Normal file
View file

@ -0,0 +1,97 @@
open Opentelemetry_client_ocurl
let test_urls ~name config =
Printf.printf "--- %s ---\n" name;
Printf.printf "url_traces = %s\n" config.Config.url_traces;
Printf.printf "url_metrics = %s\n" config.Config.url_metrics;
Printf.printf "url_logs = %s\n" config.Config.url_logs;
print_endline "------\n"
let default_url () =
let config = Config.make () in
test_urls ~name:"default_url" config
let base_url_from_config () =
let config = Config.make ~url:"http://localhost:3000" () in
test_urls ~name:"base_url_from_config" config
let base_url_from_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:5000";
let config = Config.make () in
test_urls ~name:"base_url_from_env" config
let base_url_from_both_config_and_env () =
(* url from env should take precedence *)
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:5000";
let config = Config.make ~url:"http://localhost:3000" () in
test_urls ~name:"base_url_from_both_config_and_env" config
let override_trace_url_from_config () =
let config =
Config.make ~url:"http://localhost:3000"
~url_traces:"http://localhost:3001/send/traces" ()
in
test_urls ~name:"override_trace_url_from_config" config
let override_trace_url_from_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces";
let config = Config.make () in
test_urls ~name:"override_trace_url_from_env" config
let override_trace_url_from_both_config_and_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces/env";
let config =
Config.make ~url_traces:"http://localhost:3001/send/traces/config" ()
in
test_urls ~name:"override_trace_url_from_both_config_and_env" config
let set_all_in_config () =
let config =
Config.make ~url_traces:"http://localhost:3001/send/traces"
~url_metrics:"http://localhost:3002/send/metrics"
~url_logs:"http://localhost:3003/send/logs" ()
in
test_urls ~name:"set_all_in_config" config
let set_all_in_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
"http://localhost:3001/send/traces";
Unix.putenv "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
"http://localhost:3002/send/metrics";
Unix.putenv "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
"http://localhost:3003/send/logs";
let config = Config.make () in
test_urls ~name:"set_all_in_env" config
let remove_trailing_slash_config () =
let config = Config.make ~url:"http://localhost:3000/" () in
test_urls ~name:"remove_trailing_slash_config" config
let remove_trailing_slash_env () =
Unix.putenv "OTEL_EXPORTER_OTLP_ENDPOINT" "http://localhost:3000/";
let config = Config.make () in
test_urls ~name:"remove_trailing_slash_env" config
let () = default_url ()
let () = base_url_from_config ()
let () = base_url_from_env ()
let () = base_url_from_both_config_and_env ()
let () = override_trace_url_from_config ()
let () = override_trace_url_from_env ()
let () = override_trace_url_from_both_config_and_env ()
let () = set_all_in_config ()
let () = set_all_in_env ()
let () = remove_trailing_slash_config ()
let () = remove_trailing_slash_env ()