最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - Golang & opentelemtry log collection pkg : why is my pipeline not sending logs from input operator to output operat

programmeradmin2浏览0评论

recently i started working on log files and handling then i tried to write a simple program trying to tail a log file and send the log lines to a new log file. I used opentelemetry-log-collection package from pkg.go.dev for this implementation. I configured both operators and build a pipeline to connect these 2 operators to send the log data and receive the log data at the end. But after running the code nothing was added in my output log file.

I tried debugging then i found that both operators are working fine but pipeline is either not receiving the log lines or not sending them

what did i do wrong ?

This is my main.go code for this

package main

import (
    "context"
    "log"
    "os"
    "time"

    "go.uber/zap"
    fileInput "github/open-telemetry/opentelemetry-log-collection/operator/input/file"
    fileOutput "github/open-telemetry/opentelemetry-log-collection/operator/output/file"
    "github/open-telemetry/opentelemetry-log-collection/operator"
    "github/open-telemetry/opentelemetry-log-collection/pipeline"
)


type nopPersister struct{}

func (n *nopPersister) Get(ctx context.Context, key string) ([]byte, error) { return nil, nil }

func (n *nopPersister) Set(ctx context.Context, key string, value []byte) error { return nil }

func (n *nopPersister) Delete(ctx context.Context, key string) error { return nil }

func NewNopPersister() *nopPersister {
    return &nopPersister{}
}

func main() {
    logger, err := zap.NewProduction()
    if err != nil {
        log.Fatalf("Failed to create logger: %v", err)
    }
    defer logger.Sync()
    sugaredLogger := logger.Sugar()

    sourceFilePath := "/mnt/c/Users/BhavanaMatala/Desktop/Go files/logs/app.log"
    if _, err := os.Stat(sourceFilePath); os.IsNotExist(err) {
        log.Fatalf("Source file does not exist: %s", sourceFilePath)
    }

    inputConfig := fileInput.NewInputConfig("file_input")
    inputConfig.Include = []string{sourceFilePath}
    inputConfig.StartAt = "beginning"             

    inputOperator, err := inputConfig.Build(sugaredLogger)
    if err != nil {
        log.Fatalf("Failed to build file_input operator: %v", err)
    }

    outputConfig := fileOutput.NewFileOutputConfig("file_output")
    outputConfig.Path = "/mnt/c/Users/BhavanaMatala/Desktop/Go files/logs/db.log" 
    outputConfig.Format = "none"                                                 

    outputOperator, err := outputConfig.Build(sugaredLogger)
    if err != nil {
        log.Fatalf("Failed to build file_output operator: %v", err)
    }

    operators := []operator.Operator{inputOperator, outputOperator}
    directedPipeline, err := pipeline.NewDirectedPipeline(operators)
    if err != nil {
        log.Fatalf("Failed to create directed pipeline: %v", err)
    }

    persister := NewNopPersister()
    err = directedPipeline.Start(persister)
    if err != nil {
        log.Fatalf("Failed to start the pipeline: %v", err)
    }

    log.Println("Pipeline is running. It will run for 30 seconds before stopping...")
    time.Sleep(30 * time.Second)

    err = directedPipeline.Stop()
    if err != nil {
        log.Printf("Failed to stop the pipeline: %v", err)
    }

    log.Println("Pipeline stopped.")
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论