Skip to content

Instantly share code, notes, and snippets.

@ooola

ooola/.gitignore Secret

Last active April 3, 2020 21:35
Show Gist options
  • Save ooola/b626b2c71569402f1009187299127e56 to your computer and use it in GitHub Desktop.
Save ooola/b626b2c71569402f1009187299127e56 to your computer and use it in GitHub Desktop.
Using the Optimizely Go SDK C Wrapper in Nginx

Using the Optimizely Go SDK C Wrapper in Nginx

Introduction

This code demonstrates how to use the Optimizely Go SDK C Wrapper in Nginx.

I used the code from Perusio's nginx-hello-world-module. He in turn used code from from Dominic's nginx module guide.

This code is not the prettiest and is intended as a demonstration only.

Installation

  1. First build and install Nginx as usual with make and make install.

  2. Make sure the Optimizely C SDK is built and the library (.so) is copied to this directory for whatever platform you are working on.

  3. Adjust the paths in the Makefile to Nginx source. Then do make config build install in this directory. See the Makefile for additional supported targets when the source code is updated.

  4. Configure the module. There's only one directive hello_world that is supported in the location context only.

    When loading dynamically first add the following to the Nginx config file.

    load_module modules/nginx_hello_world_module.so
    

    Then in the listener section invoke the module in a specific location

    location = /test {
       
       hello_world;
    
    }
    

    Now doing something like:

    curl -i http://example.com/test
    

    should return the hello world string as the response body.

  5. Restart Nginx - make restart.

Additional details

/proc/cpu

[ec2-user@ip-172-31-19-13 ~]$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 63
model name      : Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz
stepping        : 2
microcode       : 0x43
cpu MHz         : 2400.112
cache size      : 30720 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm cpuid_fault invpcid_single pti fsgsbase bmi1 avx2 smep bmi2 erms invpcid xsaveopt
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs
bogomips        : 4800.6
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:
ngx_addon_name=ngx_http_hello_world_module
if test -n "$ngx_module_link"; then
ngx_module_type=HTTP
ngx_module_name=ngx_http_hello_world_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_hello_world_module.c"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
fi
PWD =`pwd`
NGINX ?= "/home/ec2-user/nginx-1.16.1" # path to nginx source
MODULE_DIR ?= "/home/ec2-user/module"
NGINX_INSTALL_DIR ?= "/usr/local/nginx/sbin"
NGINX_MODULE_DIR ?= "/usr/local/nginx/modules"
ifeq (,$(wildcard optimizely-sdk.so))
$(error optimizely sdk is missing please copy it here)
endif
ifeq (,$(wildcard nginx))
$(error nginx source code is missing please copy it here)
endif
.PHONY: all
all: build install restart
.PHONY: config
config:
(cd ${NGINX} && ./configure --add-dynamic-module=${MODULE_DIR}) # --with-debug for nginx dbg logging
.PHONY: build
build:
(cd ${NGINX} && make)
.PHONY: install
install:
(cd ${NGINX} && sudo make install)
sudo cp optimizely-sdk.so ${NGINX_INSTALL_DIR}
.PHONY: restart
restart:
-sudo killall nginx
sudo /usr/local/nginx/sbin/nginx
.PHONY: sync
sync:
rsync -e ssh -r ${PWD}/* passrole:module/
.PHONY: runwithoptimizelysdk
runwithoptimizelysdk:
sudo cp hello_world_with_optimizely_sdk.so ${NGINX_MODULE_DIR}/ngx_http_hello_world_module.so
${MAKE} restart
.PHONY: runhelloworld
runhelloworld:
sudo cp hello_world_module.so ${NGINX_MODULE_DIR}/ngx_http_hello_world_module.so
${MAKE} restart
.PHONY: bench
bench:
ab -n 100000 -c 500 -g out.tsv http://localhost:80/
.PHONY: plot
plot: plot.p
gnuplot plot.p
gnuplot timeseries.p
/**
* @file ngx_http_hello_world_module.c
* @author António P. P. Almeida <appa@perusio.net>
* @date Wed Aug 17 12:06:52 2011
*
* @brief A hello world module for Nginx.
*
* @section LICENSE
*
* Copyright (C) 2011 by Dominic Fallows, António P. P. Almeida <appa@perusio.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "optimizely-sdk.h"
#define HELLO_WORLD "hello world\r\n"
#define OPTIMIZELY_SDK_ENABLED 1
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r);
#if OPTIMIZELY_SDK_ENABLED
static int optly_sdk_initialized = 0;
static int optly_sdk_handle = 0;
static int (*optly_sdk_init)();
static int (*optly_sdk_client)(char *);
static int (*optly_sdk_is_feature_enabled)(int handle, char*, optimizely_user_attributes*, char**);
static char* (*optly_sdk_get_feature_variable_string)(int, char*, char*, optimizely_user_attributes*, char**);
static char *sdk_key = NULL; // THIS MUST BE A VALID KEY
#endif
/**
* This module provided directive: hello world.
*
*/
static ngx_command_t ngx_http_hello_world_commands[] = {
{ ngx_string("hello_world"), /* directive */
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes
no arguments*/
ngx_http_hello_world, /* configuration setup function */
0, /* No offset. Only one context is supported. */
0, /* No offset when storing the module configuration on struct. */
NULL},
ngx_null_command /* command termination */
};
/* The hello world string. */
static u_char ngx_hello_world[] = HELLO_WORLD;
/* The module context. */
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
/* Module definition. */
ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx, /* module context */
ngx_http_hello_world_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
#if OPTIMIZELY_SDK_ENABLED
/**
* This loads the optimizely sdk and calls OptimizelySDKInit()
* if successful set optly_sdk_initialized = 1
*/
static void initialize_optimizely_sdk(char *sdk_path, ngx_http_request_t *r)
{
if (optly_sdk_initialized == 1) {
return;
}
if (sdk_key == NULL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "no sdk_key defined");
exit(1);
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "initalizing the optimizely sdk");
void *handle;
char *error;
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "calling dlopen()");
handle = dlopen (sdk_path, RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, dlerror());
exit(1);
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "dlopen succeded");
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "looking up optimizely_sdk_init");
optly_sdk_init = dlsym(handle, "optimizely_sdk_init");
if ((error = dlerror()) != NULL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, error);
exit(1);
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "looking up optimizely_sdk_client");
optly_sdk_client = dlsym(handle, "optimizely_sdk_client");
if ((error = dlerror()) != NULL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, error);
exit(1);
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "looking up optimizely_sdk_is_feature_enabled");
optly_sdk_is_feature_enabled = dlsym(handle, "optimizely_sdk_is_feature_enabled");
if ((error = dlerror()) != NULL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, error);
exit(1);
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "looking up optimizely_sdk_get_feature_variable_string");
optly_sdk_get_feature_variable_string = dlsym(handle, "optimizely_sdk_get_feature_variable_string");
if ((error = dlerror()) != NULL) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, error);
exit(1);
}
//create the sdk client handle
optly_sdk_init();
optly_sdk_handle = optly_sdk_client(sdk_key);
optly_sdk_initialized = 1;
}
#endif
/**
* Content handler.
*
* @param r
* Pointer to the request structure. See http_request.h.
* @return
* The status of the response generation.
*/
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
u_char *message;
/* Set the Content-Type header. */
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";
/* Allocate a new buffer for sending out the reply. */
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_ERROR;
}
/* Insertion in the buffer chain. */
out.buf = b;
out.next = NULL; /* just one buffer */
/* TODO: add proper error handling - this code is for demonstration only */
#if OPTIMIZELY_SDK_ENABLED
char *optly_error = NULL;
optimizely_user_attributes a = {0};
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "checking on feature");
initialize_optimizely_sdk("/usr/local/nginx/sbin/optimizely-sdk.so", r);
if (optly_sdk_is_feature_enabled(optly_sdk_handle, "current_greeting", &a, &optly_error)) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "getting current greeting");
message = (u_char *) optly_sdk_get_feature_variable_string(optly_sdk_handle, "current_greeting",
"greeting", &a, &optly_error);
} else {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "using default message");
message = ngx_hello_world; // "hello world\r\n"
}
#else
message = ngx_hello_world; // "hello world\r\n"
#endif
/* Always copy the message, even when read-only. This should ensure that
both the hello world and optimizely sdk cases do the same amount of
memory work */
ngx_buf_t *msg = ngx_palloc(r->pool, ngx_strlen(message));
if (msg == NULL) {
return NGX_ERROR;
}
ngx_memcpy(msg, message, ngx_strlen(message));
if (message != ngx_hello_world) {
free(message);
}
b->pos = (u_char *)msg;
b->last = (u_char *)msg + ((long int)ngx_strlen((const char *)msg)); /* last position in memory of the data */
b->temporary = 1; /* content is in read-write memory */
b->last_buf = 1; /* there will be no more buffers in the request */
/* Sending the headers for the reply. */
r->headers_out.status = NGX_HTTP_OK; /* 200 status code */
/* Get the content length of the body. */
r->headers_out.content_length_n = ((long int)ngx_strlen((const char *)msg));
ngx_http_send_header(r); /* Send the headers */
/* Send the body, and return the status code of the output filter chain. */
return ngx_http_output_filter(r, &out);
} /* ngx_http_hello_world_handler */
/**
* Configuration setup function that installs the content handler.
*
* @param cf
* Module configuration structure pointer.
* @param cmd
* Module directives structure pointer.
* @param conf
* Module configuration structure pointer.
* @return string
* Status of the configuration setup.
*/
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */
/* Install the hello world handler. */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_world_handler;
return NGX_CONF_OK;
} /* ngx_http_hello_world */
/* Code generated by cmd/cgo; DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-export-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
#endif
/* Start of preamble from import "C" comments. */
#line 19 "csdk.go"
#include <stdlib.h>
typedef struct optimizely_user_attribute {
char *name;
int var_type; // 1 = string, 2 = bool, 3 = float, 4 = int
void *data;
} optimzely_user_attribute;
typedef struct optimizely_user_attributes{
char *id;
int num_attributes;
struct optimizely_user_attribute *user_attribute_list;
} optimizely_user_attributes;
#line 1 "cgo-generated-wrapper"
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern void optimizely_sdk_init();
extern GoInt32 optimizely_sdk_client(char* p0);
extern void optimizely_sdk_delete_client(GoInt32 p0);
extern GoInt32 optimizely_sdk_is_feature_enabled(GoInt32 p0, char* p1, optimizely_user_attributes* p2, char** p3);
extern char* optimizely_sdk_get_feature_variable_string(GoInt32 p0, char* p1, char* p2, optimizely_user_attributes* p3, char** p4);
extern GoUint8 optimizely_sdk_get_feature_variable_boolean(GoInt32 p0, char* p1, char* p2, optimizely_user_attributes* p3, char** p4);
extern GoFloat64 optimizely_sdk_get_feature_variable_double(GoInt32 p0, char* p1, char* p2, optimizely_user_attributes* p3, char** p4);
extern GoInt optimizely_sdk_get_feature_variable_integer(GoInt32 p0, char* p1, char* p2, optimizely_user_attributes* p3, char** p4);
extern char* optimizely_sdk_get_variation(GoInt32 p0, char* p1, optimizely_user_attributes* p2, char** p3);
extern char* optimizely_sdk_get_feature_variable(GoInt32 p0, char* p1, char* p2, optimizely_user_attributes* p3, char** p4, char** p5);
extern char* optimizely_sdk_activate(GoInt32 p0, char* p1, optimizely_user_attributes* p2, char** p3);
extern char** optimizely_sdk_get_enabled_features(GoInt32 p0, optimizely_user_attributes* p1, int* p2, char** p3);
// this only returns the names, not the values
extern char** optimizely_sdk_get_all_feature_variables(GoInt32 p0, char* p1, optimizely_user_attributes* p2, int* p3, int* p4, char** p5);
extern char* optimizely_sdk_track(GoInt32 p0, char* p1, optimizely_user_attributes* p2, float* p3, char** p4);
#ifdef __cplusplus
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment