Last active
October 21, 2019 13:22
-
-
Save badboy/62a2dacf3fcd11258c3ed56ffc35e7ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
from cffi import FFI | |
import weakref | |
global_weakkeydict = weakref.WeakKeyDictionary() | |
ffi = FFI() | |
ffi.cdef(""" | |
typedef const char *const *RawStringArray; | |
typedef const char *FfiStr; | |
typedef struct { | |
FfiStr data_dir; | |
FfiStr package_name; | |
uint8_t upload_enabled; | |
const int32_t *max_events; | |
} FfiConfiguration; | |
void glean_enable_logging(void); | |
uint64_t glean_initialize(const FfiConfiguration *cfg); | |
uint8_t glean_is_upload_enabled(uint64_t glean_handle); | |
void glean_set_upload_enabled(uint64_t glean_handle, uint8_t flag); | |
uint64_t glean_new_counter_metric(FfiStr category, | |
FfiStr name, | |
RawStringArray send_in_pings, | |
int32_t send_in_pings_len, | |
int32_t lifetime, | |
uint8_t disabled); | |
void glean_counter_add(uint64_t glean_handle, uint64_t metric_id, int32_t amount); | |
uint8_t glean_send_pings_by_name(uint64_t glean_handle, | |
RawStringArray ping_names, | |
int32_t ping_names_len, | |
uint8_t log_ping); | |
uint64_t glean_new_ping_type(FfiStr ping_name, uint8_t include_client_id); | |
void glean_register_ping_type(uint64_t glean_handle, uint64_t ping_type_handle); | |
""") | |
lib = ffi.dlopen("libglean_ffi.dylib") | |
def make_config(data_dir, package_name): | |
dir = ffi.new("char[]", str.encode(data_dir)) | |
name = ffi.new("char[]", str.encode(package_name)) | |
cfg = ffi.new("FfiConfiguration *") | |
cfg.data_dir = dir | |
cfg.package_name = name | |
cfg.upload_enabled = 1 | |
cfg.max_events = ffi.NULL | |
global_weakkeydict[cfg] = (dir, name) | |
return cfg | |
class Counter: | |
def __init__(self, lib, category, name, send_in_pings): | |
category = str.encode(category) | |
name = str.encode(name) | |
argv_keepalive = [] | |
for ping in send_in_pings: | |
argv_keepalive.append(ffi.new("char[]", str.encode(ping))) | |
argv_keepalive.append(ffi.NULL) | |
pings = ffi.new("char *[]", argv_keepalive) | |
self.lib = lib | |
self.handle = lib.glean_new_counter_metric(category, name, pings, len(send_in_pings), 0, 0) | |
def add(self, glean, amount=1): | |
self.lib.glean_counter_add(glean, self.handle, amount) | |
lib.glean_enable_logging() | |
cfg = make_config("./tmp", "glean-python-sample") | |
glean = lib.glean_initialize(cfg) | |
print(f"glean: {glean}") | |
metrics_ping = lib.glean_new_ping_type(b"metrics", 1) | |
lib.glean_register_ping_type(glean, metrics_ping) | |
counter = Counter(lib, "python", "calls", ["metrics"]) | |
print(f"counter: {counter}") | |
for i in range(1, 5): | |
counter.add(glean, 1) | |
argv_keepalive = [ | |
ffi.new("char[]", b"metrics"), | |
ffi.NULL | |
] | |
ping_names = ffi.new("char *[]", argv_keepalive) | |
lib.glean_send_pings_by_name(glean, ping_names, 1, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment