Keith D. Jensen - My Father

Dad is a mountain of a man. At 79 his grip is still firm. His hands are made from the large bones and rawhide of decades of daily hard work. His eyes pierce you as he looks through your exterior to see your soul with love beyond comprehension, as if to say, "It'll be alright. Just keep'a goin'."

Born the second youngest of four boys and four girls, Dad grew up working at the saw mill and on the farm. His own father suffered from allergies and alcoholism, so Dad spent his teen years taking care of the farm by himself. He learned quickly to rely on himself to solve the problems that come with running a farm. 

As a boy he responded to the invitation of a neighbor to attend church and became a devoted servant of God, attending his meetings, reading the scriptures and serving others. He wanted to serve a mission but funds were scarce. Instead he attended Carbon College, married his high school sweetheart, LaRue McElprang, and started a family.

Dad and Mom raised eight kids on a farm just outside of Roosevelt, Utah -- Todd, me, Ruth, Melissa, Rebecca, Maria, David, and Leo. We all learned to work and shoulder responsibility. We learned to love one another and to get along (not always perfectly). 

Dad raised a lot of hay. He fed some of it to his own cows and horses. He sold most of it to pay the bills. And he gave much of it away to neighbors who promised to pay one day but never could. He never bothered them about it. Eventually, when David was old enough to go to school, Mom went to work at the hospital to make up for the dwindling of the farm income. But Dad never gave up on the farm. He worked and worked and some years were good and others not so much.

As Alzheimer's began to take it's toll, Dad did the bravest and most humble thing I have ever witnessed. He agreed with Mom to sell all the equipment he had accumulated over the years to pay off the farm and house and hang up his farmer's hat. It may have been the most difficult thing I ever saw him do. He put on a brave face as friends, neighbors and relatives attended the auction to take away the instruments of his livelihood, the tools and machines that had come to symbolize and define so much of his life.

Even then for years after that, Dad would put on his hat and coat twice a day to venture out to the corrals to feed the dwindling number of horses and cows. He wore a trail through to the ground making this trip more than 700 times a year. This ritual is such a part of him, that he still puts on his hat and coat to go out. He gets as far as the garage and cannot remember what's next, eventually returning to the house, not knowing why.

Now with his memories beyond his reach, I introduce myself to him when I visit and tell him, "I'm Tyler. I'm your son," and he smiles with sweet surprise and joy, hugs me and asks, "Did I do a good job?" 

Yes, Dad, you did a very good job!

Happy Father's Day!

Hello World in D, Go and Rust in VS Code

A few days ago a friend asked me what languages I’m learning. I told him I had been rather lazy of late and was not currently studying any new languages in my programming repertoire. So he said something like this:

每天早上刷牙后研究一种新语言。
Měitiān zǎoshang shuāyá hòu yánjiū yī zhǒng xīn yǔyán.

When I told him I had no idea what he just said, he laughed and explained that he is learning Mandarin and had just told me, “Study a new language after you brush your teeth every morning.” To be honest, I pulled the above Mandarin from Google Translate because I had no way to remember exactly how he had said what he said. But he had successfully goaded me into getting back on the polyglot track. We talked about Rust and Go and D. I’ve played with D some years ago, but have never done anything real with Rust, Go or D.

Here’s Part 1 of my journey through these three languages. The Hello World with a tiny input twist looks like this:

image

I decided to get each of them working in Visual Studio Code. I’m a Windows user, so if you’re on a Mac or using Linux, your mileage will vary. Hopefully what I’ve found here will help you get a start on one or all of these languages as well.

Visual Studio Code

First things first. If you don’t already have it installed, follow the link in the header above and get and install it. It’s simple so I won’t walk you through it here. My first love is the big old fashioned Visual Studio and I’ll continue using it for most of my work, but I wanted to learn more about VS Code as I learn more about these three programming languages. Here’s the version I’m using:

image

Of course you can use your own favorite editor. We’re not going to use any special integrations in VS Code for the Hello World examples here.

Hello in DLang

You can get a pretty good history of DLang here. There are three compilers that support D. In this post, we will only use the DMD compiler, the reference compiler for the language. Download and install DMD. Once installed, open a new console and enter the command DMD. You should get something like this:

[path]>dmd 

DMD32 D Compiler v2.086.0
Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright

Once you have the compiler installed, install the DLang VS Code extension for D. (There are several. After some experimentation, I found that I liked this one the best but this is by no means a comparison of them, so I’m not mentioning the ones I decided not to use.)

I created the following Hello World in app.d and ran the DMD app.d command in the terminal window in VS Code.

import std.stdio;

void main()
{
	string name;
	write("Hello, what's your name? ");
	readf("%s\n", &name);
	writeln("Hello, ", name);
}

The app.exe produced was 312KB in file size. At run time, it consumed 1,728KB.

Hello in GoLang

I downloaded and installed Go and then the Go VS Code extension. The extension is built by Microsoft, so I expected the VS Code experience to be superior to the other two languages. I was right. This included the automatic suggestion to install several additional Go related extensions which I did.

I was able to run the following code through the debugger, but rather than get into debugging these languages for this post, I wanted to focus on building an executable that would be as close to the same modified Hello World as I could get.

package main

import "fmt"

func main() {
	var name string
	fmt.Printf("Hello, what's your name? ")
	fmt.Scanf("%s\n", &name)
	fmt.Printf("Hello, %s\n", name)
}

The command line to build the executable was a little more tricky but not bad. There are many sites that can help you here. The command go build -o app.exe app.go produced an app.exe that 2,164KB file size but only consumed 1,424KB at runtime. That surprised me a bit. I expect that Go is packing in a whole lot of things into the executable that my little code base is not using.

Hello in Rust-Lang

Next I downloaded and installed Rust for Windows and then added the Rust(rls) VS Code extension. When I first tried to compile this bit of code, I got an error error[E0601]: `main` function not found in crate `app` which seemed odd since there was definitely a main function. After closing and starting VS Code again, the rustc app.rs command line compiled the executable just fine. Perhaps a path had not been picked up.

use std::io;

fn main() {
	let mut name = String::new();
	println!("Hello, what's your name? ");
	io::stdin().read_line(&mut name).expect("Failed to read line.");
	println!("Hello, {}", name);
}

The Rust compiler wins the size competition with an executable coming in at on 154KB for a file size and only 324KB at runtime. Call me impressed.

Video Tutorials

If you like to learn by watching, here are three fairly good YouTube tutorials that I’ve found. There are many more of course. I’m not saying these are the best, but I liked them. I will return to them a few more times as I continue to learn each of these languages.

What’s Next

Most programming language documentation will take you on a journey of variables, control flow, etc. I figure you can read those on your own. Google works just as well for you as it does for me. So next I want to explore the following. Hold me to it.

  1. How to write object oriented code
  2. How to write SOLID code
  3. How to write a RESTful service client and server
  4. How to write scalable code (threading, message passing, etc.)

That seems like a fair number of things to explore. There are a multitude of others that may emerge as I walk down these three learning paths.

Update

In preparation for more on this language exploration, I have create a GitHub repo here.

Wild Horses on Missouri Creek

A few days ago, we found this bay stallion with his two mares just over the Colorado border on Missouri Creek near Dragon, Utah, an old gilsonite mining ghost town. As we stopped to watch them, he nervously paced back and forth protecting the mares that had trotted away when we pulled up. I was impressed by his defiance of the threat and his willingness to protect his own.

His beautiful bay coat is covered in dry mud which I'm sure was a cool roll in a recent rain slicked earth. The black mane and overall conformation makes me think he's descended from a fine Morgan line.

I wonder if we look out for those we love as well as this amazing stallion does his small harem. Are we willing to stand up to forces we do not recognize or understand? Do we overcome our own fears to watch over those who rely upon us? What can we learn from the Stallion of Missouri Creek?

HttpContext and Logging to Elasticsearch on a Background Thread

“HttpContext is not thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.” (from docs.microsoft.com)

image

I am a big fan of Elasticsearch (ELK) logging and have built this into the Ioka.Services.Foundation using the Serilog libary, my current favorite .NET Core logging library. There are many ways to approach this and my intent is not to explore those but to show one way of taking on the task of logging in a background thread while preserving some request context detail in your log.

Following the recommendation on the above docs page, we copy the elements of the HttpContext that we need for our background thread logging using the LogContextFactory. Here’s a snippet of that code. For your scenario, you’ll want to modify what values you wish to preserve and you may wish to remove the fairly heavy duty user agent parser if you don’t care about seeing user agent data broken down in the log message.

public static LogContext Create(HttpContext context)
{
   return CreateFactory(() => context)();
}

public static Func CreateFactory(Func httpContextFactory)
{
   if (null == httpContextFactory) throw new ArgumentNullException(nameof(httpContextFactory));
   return new Func(() =>
   {
      try
      {
         var httpCtx = httpContextFactory();
         var httpRequestFeature = httpCtx.Request.HttpContext.Features.Get();
         var context = new LogContext();
         context["_ThreadId"] = Environment.CurrentManagedThreadId.ToString(); 
         context["_Source"] = Assembly.GetEntryAssembly().GetName().Name;
         context["_IpAddress"] = httpCtx.Connection.RemoteIpAddress.ToString();
         context["_UserId"] = httpCtx.Request.Headers["APPUID"].Count > 0 
            ? httpCtx.Request.Headers["APPUID"][0] 
            : context["_UserId"];
         context["_HttpMethod"] = httpCtx.Request.Method;

In the controller we call the Create method to get a copy of what we need to pass into the background thread async method called DoMathWithLogging (cheesy name for demo purposes only) like this:

public async Task<ActionResult<IEnumerable<string>>> Get()
{
    var msg1 = "Another message";
    var msg3 = new CustomError { Name = "Second", Message = "Second other message" };
    _logger.Debug("This is a debug message. {msg1}, {@msg3}", msg1, msg3);

    var logContext = LogContextFactory.Create(this.HttpContext);
    var result = await _mathDemoProvider.DoMathWithLogging(logContext, _logger);

    return new string[] 
    {
        logContext["_UserId"],
        logContext["_RequestId"],
        result.ToString()
    };
}

Now in the DoMathWithLogging method, we use the ILog interface With method to pass the LogContext object into the logger to preserve what we have copied from HttpContext to the LogContext object.

public async Task<long> DoMathWithLogging(LogContext logContext, ILog logger)
{
    long x = 0;
    try
    {
        var rand = new Random();
        for (int i = 0; i < 10; i++)
        {
            x = 1000 * (long)rand.NextDouble();
            Thread.Sleep(10);
        }
        Thread.Sleep(100);
        var c = 0;
        x = 77 / c;
    }
    catch (Exception e)
    {
        //uses new logger with saved context as this 
        //is not on the request background thread
        logger.With(logContext).Error(e, "Error: value of {LargeValue}", x);
    }
    return x;
}

Note that in our demo code, we deliberately throw a divide by zero error and log it. And now in the implementation of the With method looks like this, capturing the current thread in the “_ThreadId-With” property on the context.

public ILog With(LogContext context)
{
    context["_ThreadId-With"] = Environment.CurrentManagedThreadId.ToString();
    var list = _enrichers.Where(x => x.GetType() != typeof(LogEnricher)).ToList();
    list.Insert(0, new LogEnricher(context, null));
    return new Log(_config, _level, () => _index, _failureSink, _failureCallback, list.ToArray());
}

In the With method, we insert a new log enricher for the Serilog logger. This allows us to capture the copied context values in the log messages, such as the Error logged like this:

{
  "_index": "test",
  "_type": "logevent",
  "_id": "JAdXF2oB9fWPG6gy8H_9",
  "_version": 1,
  "_score": null,
  "_source": {
    "@timestamp": "2019-04-13T15:36:40.2296224+00:00",
    "level": "Error",
    "messageTemplate": "Error: value of {LargeValue}",
    "message": "Error: value of 0",
    "exception": {
      "Depth": 0,
      "ClassName": "",
      "Message": "Attempted to divide by zero.",
      "Source": "Ioka.Services.Demo",
      "StackTraceString": "   at Ioka.Services.Demo.Providers.MathLoggingDemoProvider.DoMathWithLogging(LogContext logContext, ILog logger) in D:\\Code\\Github\\Ioka.Services.Foundation\\src\\Ioka.Services.Demo\\Providers\\MathLoggingDemoProvider.cs:line 30",
      "RemoteStackTraceString": "",
      "RemoteStackIndex": -1,
      "HResult": -2147352558,
      "HelpURL": null
    },
    "fields": {
      "LargeValue": 0,
      "_UserId": "root",
      "_IpAddress": "::ffff:172.18.0.1",
      "_Source": "Ioka.Services.Demo",
      "_MachineName": "6cf7fdb5f3cf",
      "_ThreadId": "14",
      "_HttpMethod": "GET",
      "_RequestId": "50d32de9-df69-4aee-ae48-075f22b8ac2d",
      "_Url": "https://localhost:44370/api/Values",
      "_Query": "Microsoft.AspNetCore.Http.Internal.QueryCollection",
      "_WebUser": null,
      "_Browser": "Chrome 73.0.3683 Windows 10",
      "_Header_Connection": "keep-alive",
      "_Header_Accept": "text/plain",
      "_Header_Accept-Encoding": "gzip, deflate, br",
      "_Header_Accept-Language": "en-US,en;q=0.9",
      "_Header_Host": "localhost:44370",
      "_Header_Referer": "https://localhost:44370/api-docs/index.html",
      "_Header_User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
      "_ThreadId-With": "14"
    }
  },
  "fields": {
    "@timestamp": [
      "2019-04-13T15:36:40.229Z"
    ]
  },
  "sort": [
    1555169800229
  ]
}

Note the "_ThreadId-With" property above has the same vs!he as the request thread is. This is because not all async methods will run on a background thread. In previous versions of this code, I forced a long running task to spin up on another thread to verify this. Generally I would not recommend that in practice.

Also note the "_RequestId" property which would allow you to filter in Kibana for all entries with that value in order to trace all log entries for a given request. This can be a very useful tool when you're trying to figure out what happened.

This is the first of many upcoming posts on the code and use of the Ioka.Services.Foundation. The code base is not intended for production use but to provide a guide when you create your own internal libraries to spin up a set of .NET Core microservices that have a common set of fundamental elements that will make working on them, supporting them and using them easier.

Be Not Afraid

In Matthew chapter 25, Jesus relates the parable of the talents. A talent then was a great deal of money. One received five, another two and another one. When their master returned to see what they had done, he found the first two had been faithful and worked hard to improve their lot. The third responded thus:

"And I was afraid, and went and hid thy talent in the earth:" (verse 25)

Let us not be afraid. Let us go and do as the Lord has commanded us. Let us use the gifts he has given us, exercising faith to act and minister to the needs of others. If we set aside our fear, we will be blessed as we build on our faith to serve the Lord and his children.

Trying to Be a Latter-day Saint

The name of the church I attend is The Church of Jesus Christ of Latter-day Saints.

Paul called the members of the church in the meridian of time saints. Those who believed in Christ and sought to understand and practice the gospel of repentance which he had taught were known then as saints. Not because they had performed miracles but because they relied on the miracles of Christ, especially that of the Atonement.

They were not perfect. Nor are his latter-day saints. As we strive to become like him, let us remember the words of one who serves him.

“If we don’t try, we’re just latter-day sinners; if we don’t persevere, we’re latter-day quitters; and if we don’t allow others to try, we’re just latter-day hypocrites” ~Dale G. Renlund, a member of the Quorum of the Twelve Apostles.

And They Followed Him

While Jesus and his disciples were leaving Jericho, there were two blind men sitting by the road. Think of them as ourselves. Are we too blind?

These men called out to Jesus and begged for mercy. He stopped and asked them what they wanted him to do for them. Of course they asked to receive their sight. Matthew records the Master's response in this way.

Matthew 20:34 (KJV): "So Jesus had compassion on them, and touched their eyes: and immediately their eyes received sight, and they followed him."

In such a short sentence Matthew conveys some great lessons. First, is the Lord's great capacity for compassion. Second, that Christ will touch those on whom he has compassion. Third, that we can receive immediate blessings from Him through faith. And finally, that the mark of true disciples who have received their sight is that they follow Him.

Do you call out to Him in faith? Will you see? Will you follow Him?

Recognize the Lord in Our Lives

When Jesus entered Jerusalem for the last time, he looked out upon her and wept for its wickedness and imminent destruction. These were his people and they, except for a few, had not recognized him, as had been prophesied, yet he had compassion on them.

Luke 19 (KJV)

41 ¶ And when he was come near, he beheld the city, and wept over it,

42 Saying, If thou hadst known, even thou, at least in this thy day, the things which belong unto thy peace! but now they are hid from thine eyes.

43 For the days shall come upon thee, that thine enemies shall cast a trench about thee, and compass thee round, and keep thee in on every side,

44 And shall lay thee even with the ground, and thy children within thee; and they shall not leave in thee one stone upon another; because thou knewest not the time of thy visitation.

I hope that we will each learn to recognize the works of the Lord in our own lives, that we will embrace him and love to serve him as we serve others.

Take a moment today to recognize someone in your path as one you can lift a little with a kind word or a hug. Listen for a moment and see them as the Lord might. Then you will bring a little of the Lord's peace into their life and your life.