Skip to content

Khả năng quan sát

Tổng quan

Fountain cung cấp logging có cấu trúc, distributed tracing và metrics để service có thể quan sát ngay từ đầu mà không cần cấu hình phức tạp.

Các package hiện có:

Context tracing được lan truyền qua các ranh giới HTTP và gRPC khi sử dụng các server và client tương ứng.

Logging

flog là logger tiêu chuẩn của Fountain, xây dựng trên zerolog. Khởi tạo một lần với NewFountainLoggerOnce() và sử dụng lại ở mọi nơi:

go
import "gitlab.soludian.com/soludian/fountain/libs/flog"

var logger = flog.NewFountainLoggerOnce()

func doSomething(ctx context.Context) error {
    logger.WEvent("process_order").WData("order_id").Infof("Processing order")
    // ...
    if err != nil {
        logger.WErr(err).WEvent("process_order_failed").Errorf("Failed to process order")
        return err
    }
    return nil
}

TIP

Không dùng log từ thư viện chuẩn. Luôn dùng flog.

Tracing

go
import "gitlab.soludian.com/soludian/fountain/libs/ftracer"

// Cài đặt tracer từ config key "tracer"
tracer := ftracer.WithConfigKey("tracer").InstallFountainInstance()

Ví dụ

HTTP server được instrument với tracing:

go
/* !!
 * File: main.go
 * File Created: Tuesday, 28th June 2022 4:24:43 pm
 * Author: Kim Ericko ([email protected])
 * -----
 * Last Modified: Thursday, 30th June 2022 4:26:15 pm
 * Modified By: Kim Ericko ([email protected])
 * -----
 * Copyright 2022 Soludian, soludian.com
 * All rights reserved.
 *
 * Licensed under the SOLUDIAN TECHNOLOGY SOLUTION CO., LTD Software License Agreement.
 * Unauthorized use, reproduction, or distribution is prohibited (the "License");
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * [email protected] / https://www.soludian.com/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -----
 *
 * HISTORY:
 *
 * Date      	By	Comments
 * ----------	---	---------------------------------------------------------
 */

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v3"
	"gitlab.soludian.com/soludian/fountain"
	"gitlab.soludian.com/soludian/fountain/libs/flog"
	"gitlab.soludian.com/soludian/fountain/libs/fnet/fhttp"
)

var logger = flog.NewFountainLoggerOnce()

type APIServer struct {
	*fhttp.Server
}

func (s *APIServer) Initialize() error {
	s.Server = fhttp.InstallFountainInstance()
	s.Server.Get("/panic", func(ctx fiber.Ctx) error {
		err := fmt.Errorf("Route panic")
		logger.WErr(err).Panicf(err.Error())
		return err
	})

	s.Server.Get("/200", func(ctx fiber.Ctx) error {
		return ctx.SendString("hello")
	})

	s.Server.Get("/hello", func(ctx fiber.Ctx) error {
		return ctx.JSON("Hello client: " + ctx.Get("app"))
	})

	s.Server.Get("/500", func(ctx fiber.Ctx) error {
		return ctx.Status(500).JSON("Hello client: " + ctx.Get("app"))
	})

	return nil
}

// export FOUNTAIN_DEBUG_MODE=true && go run main.go
// ab -n 10 -c 10 http://127.0.0.1:9007/hello, you can see 429, indicating current limit
func main() {
	server := &APIServer{}
	fountain.WithAppInstances(server).Serving()
}

Xem thêm