|
| 1 | +/* |
| 2 | + * Geneva FFI C Example (synchronous only) |
| 3 | + * |
| 4 | + * This example demonstrates: |
| 5 | + * - Reading configuration from environment |
| 6 | + * - Creating a Geneva client via geneva_client_new (out-param) |
| 7 | + * - Encoding/compressing ResourceLogs |
| 8 | + * - Uploading batches synchronously with geneva_upload_batch_sync |
| 9 | + * |
| 10 | + * Note: The non-blocking callback-based mechanism has been removed. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <string.h> |
| 16 | +#include <time.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include "../include/geneva_ffi.h" |
| 19 | + |
| 20 | +/* Prototypes from the example-only builder dylib (otlp_builder) */ |
| 21 | +extern int geneva_build_otlp_logs_minimal(const char* body_utf8, |
| 22 | + const char* resource_key, |
| 23 | + const char* resource_value, |
| 24 | + uint8_t** out_ptr, |
| 25 | + size_t* out_len); |
| 26 | +extern void geneva_free_buffer(uint8_t* ptr, size_t len); |
| 27 | + |
| 28 | +/* Helper to read env or default */ |
| 29 | +static const char* get_env_or_default(const char* name, const char* defval) { |
| 30 | + const char* v = getenv(name); |
| 31 | + return v ? v : defval; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +int main(void) { |
| 36 | + printf("Geneva FFI Example (synchronous API)\n"); |
| 37 | + printf("====================================\n\n"); |
| 38 | + |
| 39 | + /* Required env */ |
| 40 | + const char* endpoint = getenv("GENEVA_ENDPOINT"); |
| 41 | + const char* environment = getenv("GENEVA_ENVIRONMENT"); |
| 42 | + const char* account = getenv("GENEVA_ACCOUNT"); |
| 43 | + const char* namespaceName = getenv("GENEVA_NAMESPACE"); |
| 44 | + const char* region = getenv("GENEVA_REGION"); |
| 45 | + const char* cfg_ver_str = getenv("GENEVA_CONFIG_MAJOR_VERSION"); |
| 46 | + |
| 47 | + if (!endpoint || !environment || !account || !namespaceName || !region || !cfg_ver_str) { |
| 48 | + printf("Missing required environment variables!\n"); |
| 49 | + printf(" GENEVA_ENDPOINT\n"); |
| 50 | + printf(" GENEVA_ENVIRONMENT\n"); |
| 51 | + printf(" GENEVA_ACCOUNT\n"); |
| 52 | + printf(" GENEVA_NAMESPACE\n"); |
| 53 | + printf(" GENEVA_REGION\n"); |
| 54 | + printf(" GENEVA_CONFIG_MAJOR_VERSION\n"); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + int cfg_ver = atoi(cfg_ver_str); |
| 59 | + if (cfg_ver <= 0) { |
| 60 | + printf("Invalid GENEVA_CONFIG_MAJOR_VERSION: %s\n", cfg_ver_str); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + /* Optional env with defaults */ |
| 65 | + const char* tenant = get_env_or_default("GENEVA_TENANT", "default-tenant"); |
| 66 | + const char* role_name = get_env_or_default("GENEVA_ROLE_NAME", "default-role"); |
| 67 | + const char* role_instance= get_env_or_default("GENEVA_ROLE_INSTANCE", "default-instance"); |
| 68 | + |
| 69 | + /* Certificate auth if both provided; otherwise managed identity */ |
| 70 | + const char* cert_path = getenv("GENEVA_CERT_PATH"); |
| 71 | + const char* cert_password = getenv("GENEVA_CERT_PASSWORD"); |
| 72 | + int32_t auth_method = (cert_path && cert_password) ? GENEVA_AUTH_CERTIFICATE : GENEVA_AUTH_MANAGED_IDENTITY; |
| 73 | + |
| 74 | + printf("Configuration:\n"); |
| 75 | + printf(" Endpoint: %s\n", endpoint); |
| 76 | + printf(" Environment: %s\n", environment); |
| 77 | + printf(" Account: %s\n", account); |
| 78 | + printf(" Namespace: %s\n", namespaceName); |
| 79 | + printf(" Region: %s\n", region); |
| 80 | + printf(" Config Major Version: %d\n", cfg_ver); |
| 81 | + printf(" Tenant: %s\n", tenant); |
| 82 | + printf(" Role Name: %s\n", role_name); |
| 83 | + printf(" Role Instance: %s\n", role_instance); |
| 84 | + printf(" Auth Method: %s\n", auth_method == GENEVA_AUTH_CERTIFICATE ? "Certificate" : "Managed Identity"); |
| 85 | + if (auth_method == GENEVA_AUTH_CERTIFICATE) { |
| 86 | + printf(" Cert Path: %s\n", cert_path); |
| 87 | + } |
| 88 | + printf("\n"); |
| 89 | + |
| 90 | + /* Build config */ |
| 91 | + GenevaConfig cfg = { |
| 92 | + .endpoint = endpoint, |
| 93 | + .environment = environment, |
| 94 | + .account = account, |
| 95 | + .namespace_name = namespaceName, |
| 96 | + .region = region, |
| 97 | + .config_major_version = (uint32_t)cfg_ver, |
| 98 | + .auth_method = auth_method, |
| 99 | + .tenant = tenant, |
| 100 | + .role_name = role_name, |
| 101 | + .role_instance = role_instance, |
| 102 | + }; |
| 103 | + if (auth_method == GENEVA_AUTH_CERTIFICATE) { |
| 104 | + cfg.auth.cert.cert_path = cert_path; |
| 105 | + cfg.auth.cert.cert_password = cert_password; |
| 106 | + } else { |
| 107 | + cfg.auth.msi.objid = NULL; |
| 108 | + } |
| 109 | + |
| 110 | + /* Create client */ |
| 111 | + GenevaClientHandle* client = NULL; |
| 112 | + GenevaError rc = geneva_client_new(&cfg, &client); |
| 113 | + if (rc != GENEVA_SUCCESS || client == NULL) { |
| 114 | + printf("Failed to create Geneva client (code=%d)\n", rc); |
| 115 | + return 1; |
| 116 | + } |
| 117 | + printf("Geneva client created.\n"); |
| 118 | + |
| 119 | + /* Create ExportLogsServiceRequest bytes via FFI builder */ |
| 120 | + size_t data_len = 0; |
| 121 | + uint8_t* data = NULL; |
| 122 | + GenevaError brc = geneva_build_otlp_logs_minimal("hello from c ffi", "service.name", "c-ffi-example", &data, &data_len); |
| 123 | + if (brc != GENEVA_SUCCESS || data == NULL || data_len == 0) { |
| 124 | + printf("Failed to build OTLP payload (code=%d)\n", brc); |
| 125 | + geneva_client_free(client); |
| 126 | + return 1; |
| 127 | + } |
| 128 | + |
| 129 | + /* Encode and compress to batches */ |
| 130 | + EncodedBatchesHandle* batches = NULL; |
| 131 | + GenevaError enc_rc = geneva_encode_and_compress_logs(client, data, data_len, &batches); |
| 132 | + if (enc_rc != GENEVA_SUCCESS || batches == NULL) { |
| 133 | + printf("Encode/compress failed (code=%d)\n", enc_rc); |
| 134 | + geneva_free_buffer(data, data_len); |
| 135 | + geneva_client_free(client); |
| 136 | + return 1; |
| 137 | + } |
| 138 | + |
| 139 | + size_t n = geneva_batches_len(batches); |
| 140 | + printf("Encoded %zu batch(es)\n", n); |
| 141 | + |
| 142 | + /* Upload synchronously, batch by batch */ |
| 143 | + GenevaError first_err = GENEVA_SUCCESS; |
| 144 | + for (size_t i = 0; i < n; i++) { |
| 145 | + GenevaError r = geneva_upload_batch_sync(client, batches, i); |
| 146 | + if (r != GENEVA_SUCCESS) { |
| 147 | + first_err = r; |
| 148 | + printf("Batch %zu upload failed with error %d\n", i, r); |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /* Cleanup */ |
| 154 | + geneva_batches_free(batches); |
| 155 | + geneva_free_buffer(data, data_len); |
| 156 | + geneva_client_free(client); |
| 157 | + |
| 158 | + if (first_err == GENEVA_SUCCESS) { |
| 159 | + printf("All batches uploaded successfully.\n"); |
| 160 | + return 0; |
| 161 | + } |
| 162 | + printf("Upload finished with error code: %d\n", first_err); |
| 163 | + return 1; |
| 164 | +} |
0 commit comments