When the KillSwitch feature is enabled in the client configuration, the Framework queries the KillSwitch URL to determine whether to enable or disable the framework for that session. The KillSwitch URL can be .aspx, .jsp, or .php. To initialize your installation, go to https://github.com/ibm-watson-cxa/SDK_Tools/tree/master/KillSwitch to obtain your KillSwitch URL.

If the Android logging framework is disabled, then the session is not captured and is excluded from the sampled data.

In the samples below, the KillSwitch URL returns 1 to enable the Framework and 0 to disable the Framework. Each KillSwitch URL has a corresponding web.config configuration file.

Sampling functions
ASPX JSP PHP


Sampling function for ASPX

These examples show the illswitch.aspx and web.config configuration file for ASPX

Example killswitch.aspx:

<%@ Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
  public int Sampler()
  {
    Random rand = new Random();
    int nextRandom = rand.Next(1,100);
    int samplepercent = Convert.ToInt32(ConfigurationManager.AppSettings["rate"]);
    if(nextRandom &lt;= samplepercent){
        return 1;
    }
    else{
        return0;
    }
  }
</script>
<%
    if  (ConfigurationManager.AppSettings["killswitchtype"].Equals("percentagesample")) {
%>
    <%= Sampler() %>
<% } else { } %>

Example web.config configuration file for ASPX:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, 
  please visit http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>

  <appSettings>
    <add key="killswitchtype" value="percentagesample"/>
    <add key="rate" value="50"/>
  </appSettings>
</configuration>


Sampling function for JSP

These examples show the killswitch.jsp and web.config configuration file for JSP.

For the JSP, if the:

  • request does not have parameters, then the client framework is always disabled.
  • id request parameter exists, it is used to check the whitelist.
  • randomsample parameter exists, the percentage rate from the config.properties file is used to determine how the server responds.

Example killswitch.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.util.Properties"%>
<%@page import="java.util.Date" %>
<%@ page import="java.net.*"%>
<%@ page import="java.io.*" errorPage=""%>
<%
    InputStream stream = application
            .getResourceAsStream("/config.properties");
    Properties props = new Properties();
    props.load(stream);
    Boolean DEBUG = false;

    DEBUG = ("true").equals(props.getProperty("debug"));
    String id = request.getParameter("id");
    String randomsample = request.getParameter("randomsample");
    String killSwitchResponse = "";
    String debugstr = "";

    // white list
    if (id != null && !id.isEmpty()) {
        InputStream whitestream = application.getResourceAsStream(props
                .getProperty("WhiteListFile"));
        BufferedReader input = new BufferedReader(
                new InputStreamReader(whitestream));
        String line = "";
        Boolean match = false;
        while ((line = input.readLine()) != null) {
            line = line.trim();
            if (line.equals(id)) {
                killSwitchResponse = "1";
                match = true;
                break;
            }
        }
        input.close();
        if (!match) {
            killSwitchResponse = "0";
        }
    }

    // If kill switch is by sample rate
    else if (randomsample != null) {
        int rand = (int) (Math.random() * 100);
        int sampleRate = Integer.parseInt(props
                .getProperty("samplerate"));
        if (rand <= sampleRate) {
            killSwitchResponse = "1";
        } else {
            killSwitchResponse = "0";
        }
    } else {
        killSwitchResponse = "0";
    }

    out.print(killSwitchResponse);

    //always give the path from root. This way it almost always works.
    String nameOfTextFile = props.getProperty("logfile");
    PrintWriter pw;

    if (DEBUG) {
        try {
            pw = new PrintWriter(new FileOutputStream(nameOfTextFile,
                    true));
            Date date = new java.util.Date();
            debugstr = date.toString() + "\t";
            if (request.getQueryString() != null) {
                debugstr += request.getQueryString();
            }
            if("0".equals(killSwitchResponse))
                pw.println(debugstr + "\tDisable");
            else
                pw.println(debugstr + "\tEnable");
            //clean up
            pw.close();
        } catch (IOException e) {
            out.println(e.getMessage());
        }
    }
%>

Example web.config configuration file:

WhiteListFile=whitelist.txt
samplerate =50
debug=true
logfile=/killswitchlog.txt


Sampling function for PHP

These examples show thekillswitch.php and web.config configuration file for PHP.

Example killswitch.php:

<?php
    $ini_array = parse_ini_file("config.ini", true);
    //print_r($ini_array);

    // if sample by percent
    if($ini_array['configtype']['killswitchtype'] === 'percentagesample'){
        $sampleRate = intval($ini_array['percentagesample']['rate']);
        killbysamplerate($sampleRate);
    }
    // if sample by whitelist
    else {

    }


    function killbysamplerate($sampleRate){
        $randomnumber = rand(1,100);
        if($randomnumber <= $sampleRate){
            echo '1';
        }
        else {
            echo '0';
        }
    }

    function killbywhitelist($whitelistpath){

    }
?>

Example web.config configuration file:

; This is a sample configuration file
; Comments start with &#39;;&#39;, as in php.ini

[configtype]
killswitchtype=percentagesample

[percentagesample]
rate = 50

[whitelist]
x
y
z


Join The Discussion

Your email address will not be published. Required fields are marked *