Skip to content

Commit

Permalink
Merge pull request #1 from elastic/add-core-library-types
Browse files Browse the repository at this point in the history
Add core library types
  • Loading branch information
anuragsoni committed Oct 29, 2021
2 parents bfa7a99 + aac8526 commit 2114cce
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
_build
*.install
_opam
4 changes: 4 additions & 0 deletions .ocamlformat
@@ -0,0 +1,4 @@
version = 0.19.0
profile = conventional
parse-docstrings = true
wrap-comments = true
6 changes: 6 additions & 0 deletions core/dune
@@ -0,0 +1,6 @@
(library
(name elastic_apm_core)
(preprocess
(pps ppx_yojson_conv))
(public_name elastic-apm.core)
(libraries mtime ptime yojson))
5 changes: 5 additions & 0 deletions core/duration.ml
@@ -0,0 +1,5 @@
type t = Mtime.Span.t

let yojson_of_t t = `Float (Mtime.Span.to_ms t)

let of_span t = t
3 changes: 3 additions & 0 deletions core/duration.mli
@@ -0,0 +1,3 @@
type t [@@deriving yojson_of]

val of_span : Mtime.Span.t -> t
54 changes: 54 additions & 0 deletions core/id.ml
@@ -0,0 +1,54 @@
let default_rand = Random.State.make_self_init ()

let alphabet = "0123456789abcdef"

let hex_encode s =
let b = Bytes.create (String.length s * 2) in
let rec loop b_idx s_idx =
if s_idx < String.length s then (
let ch = Char.code (String.unsafe_get s s_idx) in
Bytes.unsafe_set b b_idx (String.unsafe_get alphabet ((ch lsr 4) land 15));
Bytes.unsafe_set b (b_idx + 1) (String.unsafe_get alphabet (ch land 15));
loop (b_idx + 2) (s_idx + 1))
in
loop 0 0;
Bytes.unsafe_to_string b

let fill_64_bits state buf ~pos =
assert (Bytes.length buf - pos >= 8);
let a = Random.State.bits state in
let b = Random.State.bits state in
let c = Random.State.bits state in
Bytes.unsafe_set buf pos (Char.chr (a land 0xFF));
Bytes.unsafe_set buf (pos + 1) (Char.chr ((a lsr 8) land 0xFF));
Bytes.unsafe_set buf (pos + 2) (Char.chr ((a lsr 16) land 0xFF));
Bytes.unsafe_set buf (pos + 3) (Char.chr (b land 0xFF));
Bytes.unsafe_set buf (pos + 4) (Char.chr ((b lsr 8) land 0xFF));
Bytes.unsafe_set buf (pos + 5) (Char.chr ((b lsr 16) land 0xFF));
Bytes.unsafe_set buf (pos + 6) (Char.chr (c land 0xFF));
Bytes.unsafe_set buf (pos + 7) (Char.chr ((c lsr 8) land 0xFF))

let fill_128_bits state buf ~pos =
fill_64_bits state buf ~pos;
fill_64_bits state buf ~pos:(pos + 8)

let make_id_module byte_count fill_buffer =
let module M = struct
type t = string

let create_gen state =
let b = Bytes.create byte_count in
fill_buffer state b ~pos:0;
Bytes.unsafe_to_string b

let create () = create_gen default_rand

let to_string t = t

let to_hex t = hex_encode t
end in
(module M : Id_intf.S)

module Span_id = (val make_id_module 8 fill_64_bits)

module Trace_id = (val make_id_module 16 fill_128_bits)
3 changes: 3 additions & 0 deletions core/id.mli
@@ -0,0 +1,3 @@
module Span_id : Id_intf.S

module Trace_id : Id_intf.S
11 changes: 11 additions & 0 deletions core/id_intf.ml
@@ -0,0 +1,11 @@
module type S = sig
type t

val create : unit -> t

val create_gen : Random.State.t -> t

val to_string : t -> string

val to_hex : t -> string
end
65 changes: 65 additions & 0 deletions core/metadata.ml
@@ -0,0 +1,65 @@
module Process = struct
type t = {
pid : int;
title : string;
parent_process_id : (int option[@key "ppid"] [@yojson.option]);
argv : string array;
}
[@@deriving yojson_of]

let make ?parent_process_id ?(argv = [||]) pid title =
{ pid; title; parent_process_id; argv }

let default =
lazy
(let pid = Unix.getpid () in
let parent_process_id =
if Sys.win32 then None else Some (Unix.getppid ())
in
let argv = Sys.argv in
make ?parent_process_id ~argv pid Sys.executable_name)
end

module Container = struct
type t = { id : string } [@@deriving yojson_of]

let make id = { id }
end

module System = struct
type t = {
architecture : string;
hostname : string;
platform : string;
container : (Container.t option[@yojson.option]);
}
[@@deriving yojson_of]

let make ?container ~platform ~hostname ~architecture () =
{ architecture; hostname; platform; container }
end

module Agent = struct
type t = { name : string; version : string } [@@deriving yojson_of]

let make ~name ~version = { name; version }
end

module Framework = struct
type t = { name : string; version : (string option[@yojson.option]) }
[@@deriving yojson_of]

let make ?version name = { name; version }
end

module Language = struct
type t = { name : string; version : string } [@@deriving yojson_of]

let t = { name = "OCaml"; version = Sys.ocaml_version }
end

module Runtime = struct
type t = { name : string; version : string } [@@deriving yojson_of]

let t = { name = "OCaml"; version = Sys.ocaml_version }
end
49 changes: 49 additions & 0 deletions core/metadata.mli
@@ -0,0 +1,49 @@
module Process : sig
type t [@@deriving yojson_of]

val make : ?parent_process_id:int -> ?argv:string array -> int -> string -> t

val default : t Lazy.t
end

module Container : sig
type t [@@deriving yojson_of]

val make : string -> t
end

module System : sig
type t [@@deriving yojson_of]

val make :
?container:Container.t ->
platform:string ->
hostname:string ->
architecture:string ->
unit ->
t
end

module Agent : sig
type t [@@deriving yojson_of]

val make : name:string -> version:string -> t
end

module Framework : sig
type t [@@deriving yojson_of]

val make : ?version:string -> string -> t
end

module Language : sig
type t [@@deriving yojson_of]

val t : t
end

module Runtime : sig
type t [@@deriving yojson_of]

val t : t
end
3 changes: 3 additions & 0 deletions core/system_info.ml
@@ -0,0 +1,3 @@
module Platform = struct
type t = { architecture : string; hostname : string; platform : string }
end
3 changes: 3 additions & 0 deletions core/system_info.mli
@@ -0,0 +1,3 @@
module Platform : sig
type t = { architecture : string; hostname : string; platform : string }
end
5 changes: 5 additions & 0 deletions core/timestamp.ml
@@ -0,0 +1,5 @@
type t = int [@@deriving yojson_of]

let of_time t =
let seconds = Ptime.to_float_s t in
Float.to_int (seconds *. 1000. *. 1000.)
3 changes: 3 additions & 0 deletions core/timestamp.mli
@@ -0,0 +1,3 @@
type t [@@deriving yojson_of]

val of_time : Ptime.t -> t
31 changes: 31 additions & 0 deletions dune-project
@@ -0,0 +1,31 @@
(lang dune 2.9)

(name elastic-apm)

(license Apache-2.0)

(maintainers
"Hezekiah M. Carty <hezekiah.carty@elastic.co>"
"Pete Hampton <peter.hampton@elastic.co>"
"Anurag Soni <anurag.soni@elastic.co>")

(authors
"Adam Ringwood <adam.ringwood@elastic.co>"
"Hezekiah M. Carty <hezekiah.carty@elastic.co>"
"Pete Hampton <peter.hampton@elastic.co>"
"Anurag Soni <anurag.soni@elastic.co>")

(source
(github elastic/apm-agent-ocaml))

(generate_opam_files true)

(package
(name elastic-apm)
(synopsis "Elastic Application Performance Monitoring (APM) client library")
(depends
(ocaml
(>= 4.08.0))
mtime
(ptime
(>= 0.8.5))))
41 changes: 41 additions & 0 deletions elastic-apm.opam
@@ -0,0 +1,41 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Elastic Application Performance Monitoring (APM) client library"
maintainer: [
"Hezekiah M. Carty <hezekiah.carty@elastic.co>"
"Pete Hampton <peter.hampton@elastic.co>"
"Anurag Soni <anurag.soni@elastic.co>"
]
authors: [
"Adam Ringwood <adam.ringwood@elastic.co>"
"Hezekiah M. Carty <hezekiah.carty@elastic.co>"
"Pete Hampton <peter.hampton@elastic.co>"
"Anurag Soni <anurag.soni@elastic.co>"
]
license: "Apache-2.0"
homepage: "https://github.com/elastic/apm-agent-ocaml"
bug-reports: "https://github.com/elastic/apm-agent-ocaml/issues"
depends: [
"dune" {>= "2.9"}
"ocaml" {>= "4.08.0"}
"mtime"
"ptime" {>= "0.8.5"}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/elastic/apm-agent-ocaml.git"

0 comments on commit 2114cce

Please sign in to comment.