Stay Hungry Stay Foolish

Alaways be curious to learn more and achieve more.

0%

Prometheus

  • First Steps
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # Downloading Prometheus
    ➜ wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-arm64.tar.gz
    ➜ tar -xvf prometheus-2.30.0.linux-arm64.tar.gz
    ➜ cd prometheus-2.30.0.linux-arm64

    # Configuring Prometheus
    ➜ tree -L 1
    .
    ├── console_libraries
    ├── consoles
    ├── LICENSE
    ├── NOTICE
    ├── prometheus
    ├── prometheus.yml -- a sample configuration
    └── promtool

    2 directories, 5 files

    # Starting Prometheus

Rust

一门非常重视开发者用户体验的语言
如果你想从其他语言迁移到Rust,必须经过一段时期的思维转换(Paradigm Shift)

1
2
3
4
命令式(imperative)编程语言转换为函数式(function)编程语言
变量的可变性(mutable)迁移到不可变性(immutable)
弱类型语言迁移到强类型语言
从手工或者自动内存管理到通过生命周期来管理内存

Rust最大的思维转换就是变量的所有权和声明周期

阅读全文 »

Raspberry Pi Pico

a microcontroller development board,

  • RP2040

    1
    2
    3
    4
    > new microcontroller chip designed by Raspberry Pi in the UK.
    - dual-core Arm Cortex-M0+
    - 264KB RAM
    - 16MB off-chip Flash
  • Micro USB Port

    1
    2
    - provide power make Pico run
    - USB port talk to pico
  • BOOTSEL

    1
    - BOOTSEL is short for boot selection.
阅读全文 »

Blockchain

区块链技术萌芽于比特币,诞生在以太坊, 区块链是价值的载体,是一种新型的社会生产关系。基于区块链技术,打通现实世界与网络世界的屏障,将物质虚拟化,将价值实体化.

阅读全文 »

Huawei LiteOS

面向IOT领域,构建轻量级物联网操作系统, 目前支持ARM64,ARM Contex-A, ARM Cortex-M0, Cortex-M3, Cortex-M4, Cortex-M7

  • 优势:
    • 高实时性,高稳定性
    • 超小内核,基础内核体积可以裁剪至不到10K
    • 低功耗
    • 支持功能静态裁剪
阅读全文 »

Go Profling - 剖析

Using “pprof” to profiling your program
Go语言可视化性能分析工具,PProf以profile.proto读取分析样本集合,并生成报告可视化帮助分析数据(支持文本和图形报告)

  • 使用场景

    • runtime/pprof: 采集程序指定区块的运行数据分析
    • net/http/pprof: 基于HTTP Server运行,采集运行时数据分析
    • go test: 通过运行测试用例,指定所需标识进行采样
  • 安装可视化工具graphviz查看profile

    1
    2
    # Mac: $ brew install graphviz
    # Linux: $ sudo apt install graphviz
  • 支持模式

    • 报告生成 Report generation
    • 交互终端使用 Interactive terminal use
    • Web页面 Web interface
  • 可以做那些

    • CPU Profiling: CPU分析
      • 按照一定频率采集监听应用程序CPU使用情况,确定应用程序的主动消耗CPU周期时花费时间的位置
    • Memory Profiling: 内存分析
      • 程序进行堆分配时记录堆栈跟踪,监视当前和历史内存使用情况,检查内存泄漏
    • Block Profiling: 阻塞分析
      • 记录Goroutine阻塞等待同步的位置,默认不开启,需要调用runtime.SetBlockProfileRate 设置
    • Mutex Profiling: 互斥锁分析
      • 报告互斥锁的竞争情况, 默认不开启, runtime.SetMutexProfileFraction 设置
    • Goroutine Profiling: Goroutine分析
      • 对当前应用程序正在运行的Goroutine进行堆栈跟踪和分析
阅读全文 »

Constants 常量

Go是静态类型语言,不允许同步类型的数值类型进行计算. 不允许float64 + int 甚至 int32 + int. 如果你希望不同类型进行计算需要显式转换后在进行 int(float64) + int.

阅读全文 »

Ansible

Tool to automate IT tasks.

  • SIMPLE
    • Human readable automation
    • No special coding skills needed
    • Tasks executed in order
  • POWERFUL
    • App deployment
    • Configuration management
    • Workflow orchestration
  • AGENTLESS
    • Agentless architecture
    • Use OpenSSH & WinRM

What Can I Use Ansible To Do?

  • Config Management
  • App deployment
  • Provisioning
  • Continuous delivery
  • Security & Compliance
  • orchestration

Ansible Architecture

ansible architecture

  • MODULES:

Quick Start Ansible

  • Installing an Ansible
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    $ python get-pip.py --user
    $ python -m pip install --user ansible

    ansible --version
    ansible [core 2.11.4]
    config file = None
    configured module search path = ['/Users/chyiyaqing/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
    ansible collection location = /Users/chyiyaqing/.ansible/collections:/usr/share/ansible/collections
    executable location = /usr/local/bin/ansible
    python version = 3.9.6 (default, Jun 29 2021, 05:25:02) [Clang 12.0.5 (clang-1205.0.22.9)]
    jinja version = 2.10.1
    libyaml = True

flag

命令行flag解析

  • flag 长短选项

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    func main() {
    var name string

    // -flag: 仅支持布尔类型
    // -flag x: 仅支持非布尔类型
    // -flag=x: 均支持
    // 长短选项 分开两次调用
    flag.StringVar(&name, "name", "Golang flag tour", "help")
    flag.StringVar(&name, "n", "Golang flag tour", "help")
    flag.Parse()

    log.Printf("name: %s", name)
    }

    // ➜ go run flag/demo.go -name='hello world' -n 'hello world'
    // 2021/09/11 18:26:39 name: hello world
  • flag 子命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    func main() {
    var name string

    flag.Parse()
    args := flag.Args()
    if len(args) <= 0 {
    return
    }

    switch args[0] {
    case "go":
    goCmd := flag.NewFlagSet("go", flag.ExitOnError)
    goCmd.StringVar(&name, "name", "Golang language", "help")
    _ = goCmd.Parse(args[1:])
    case "py":
    pyCmd := flag.NewFlagSet("py", flag.ExitOnError)
    pyCmd.StringVar(&name, "name", "Python language", "help")
    _ = pyCmd.Parse(args[1:])
    }

    log.Printf("name: %s", name)
    }

    // ➜ go run flag/flag_sub_cli.go go -name=golang
    // 2021/09/11 18:39:47 name: golang
    //
    // ➜ go run flag/flag_sub_cli.go go -n=python
    // flag provided but not defined: -n
    // Usage of go:
    // -name string
    // help (default "Golang language")
    // exit status 2
  • flag 源码分析

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    # flag.Parse

    // CommandLine is the default set of command-line flags, parsed from os.Args.
    // The top-level functions such as BoolVar, Arg, and so on are wrappers for the
    // methods of CommandLine.
    var CommandLine = NewFlagSet(os.Args[0], ExitOnError)

    // Parse parses the command-line flags from os.Args[1:]. Must be called
    // after all flags are defined and before flags are accessed by the program.
    func Parse() {
    // Ignore errors; CommandLine is set for ExitOnError.
    CommandLine.Parse(os.Args[1:])
    }

    # FlagSet.Parse
    > parse异常分流处理
    // Parse parses flag definitions from the argument list, which should not
    // include the command name. Must be called after all flags in the FlagSet
    // are defined and before flags are accessed by the program.
    // The return value will be ErrHelp if -help or -h were set but not defined.
    func (f *FlagSet) Parse(arguments []string) error {
    f.parsed = true
    f.args = arguments
    for {
    seen, err := f.parseOne()
    if seen {
    continue
    }
    if err == nil {
    break
    }
    switch f.errorHandling {
    case ContinueOnError:
    return err
    case ExitOnError:
    if err == ErrHelp {
    os.Exit(0)
    }
    os.Exit(2)
    case PanicOnError:
    panic(err)
    }
    }
    return nil
    }

    # FlagSet.parseOne
    // parseOne parses one flag. It reports whether a flag was seen.
    func (f *FlagSet) parseOne() (bool, error) {
    if len(f.args) == 0 {
    return false, nil
    }
    s := f.args[0]
    if len(s) < 2 || s[0] != '-' {
    return false, nil
    }
    numMinuses := 1
    if s[1] == '-' {
    numMinuses++
    if len(s) == 2 { // "--" terminates the flags
    f.args = f.args[1:]
    return false, nil
    }
    }
    name := s[numMinuses:]
    if len(name) == 0 || name[0] == '-' || name[0] == '=' {
    return false, f.failf("bad flag syntax: %s", s)
    }

    // it's a flag. does it have an argument?
    f.args = f.args[1:]
    hasValue := false
    value := ""
    for i := 1; i < len(name); i++ { // equals cannot be first
    if name[i] == '=' {
    value = name[i+1:]
    hasValue = true
    name = name[0:i]
    break
    }
    }
    m := f.formal
    flag, alreadythere := m[name] // BUG
    if !alreadythere {
    if name == "help" || name == "h" { // special case for nice help message.
    f.usage()
    return false, ErrHelp
    }
    return false, f.failf("flag provided but not defined: -%s", name)
    }

    if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
    if hasValue {
    if err := fv.Set(value); err != nil {
    return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err)
    }
    } else {
    if err := fv.Set("true"); err != nil {
    return false, f.failf("invalid boolean flag %s: %v", name, err)
    }
    }
    } else {
    // It must have a value, which might be the next argument.
    if !hasValue && len(f.args) > 0 {
    // value is the next arg
    hasValue = true
    value, f.args = f.args[0], f.args[1:]
    }
    if !hasValue {
    return false, f.failf("flag needs an argument: -%s", name)
    }
    if err := flag.Value.Set(value); err != nil {
    return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
    }
    }
    if f.actual == nil {
    f.actual = make(map[string]*Flag)
    }
    f.actual[name] = flag
    return true, nil
    }

Cobra