Chúng tôi sẽ là một ứng dụng “đọc-đánh giá-in-vòng lặp” [REPL] đơn giản. Trong đó, chúng tôi sẽ thu thập một số số liệu để quan sát công việc đang diễn ra trong mã này, chẳng hạn như
- Độ trễ trên mỗi vòng xử lý
- Số dòng đã đọc
- Số lỗi
- độ dài dòng
Đầu tiên, tạo một tệp có tên
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
1touch repl.go
Tiếp theo, đặt mã sau vào bên trong của
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
1package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
Bạn có thể chạy mã qua
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
3Bật số liệu
Gói nhập khẩu
Để bật số liệu, chúng tôi sẽ nhập một vài gói
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
Tạo số liệu
Trước tiên, chúng tôi sẽ tạo các biến cần thiết để sau này ghi lại số liệu của chúng tôi
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
________số 8
Tạo thẻ
Bây giờ, chúng tôi sẽ tạo biến cần thiết sau này để thêm siêu dữ liệu văn bản bổ sung vào số liệu của chúng tôi
var [
KeyMethod, _ = tag.NewKey["method"]
KeyStatus, _ = tag.NewKey["status"]
KeyError, _ = tag.NewKey["error"]
]
touch repl.go
0Sau này chúng ta sẽ sử dụng thẻ này, được gọi là KeyMethod, để ghi lại phương thức nào đang được gọi. Trong kịch bản của chúng tôi, chúng tôi sẽ chỉ sử dụng nó để ghi lại rằng “repl” đang gọi dữ liệu của chúng tôi
Một lần nữa, điều này là tùy ý và hoàn toàn phụ thuộc vào người dùng. Ví dụ: nếu chúng tôi muốn theo dõi hệ điều hành mà người dùng đang sử dụng, chúng tôi có thể làm như thế này
touch repl.go
1Sau này, khi chúng tôi sử dụng osKey, chúng tôi sẽ có cơ hội nhập các giá trị như “windows” hoặc “mac”
Chèn thẻ
Bây giờ chúng ta sẽ chèn một thẻ cụ thể có tên là “repl”. Nó sẽ cung cấp cho chúng tôi một
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
4 mới mà chúng tôi sẽ sử dụng trong khi sau này chúng tôi ghi lại số liệu của mình. package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
5 này có tất cả các thẻ đã được chèn trước đó, do đó cho phép truyền ngữ cảnhVí dụ
touch repl.go
4và để sử dụng hoàn chỉnh
package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
0package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
1Khi ghi lại số liệu, chúng tôi sẽ cần
package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
5 từ package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
7. Chúng tôi sẽ ghi lại các chỉ số trong package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
8, vì vậy hãy tiếp tục và cung cấp package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
5 ngay bây giờpackage main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
2package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
3Số liệu ghi âm
Bây giờ chúng tôi sẽ ghi lại các số liệu mong muốn. Để làm như vậy, chúng tôi sẽ sử dụng
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
0 và chuyển vào package main
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
5 vàpackage main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
4package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
5Bật Chế độ xem
Chúng tôi sẽ thêm gói View.
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
2Gói nhập khẩu
package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
6package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
7Tạo Chế độ xem
Bây giờ, chúng tôi xác định cách tổ chức các số liệu của mình bằng cách tạo
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
3package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
8package main
import [
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
]
func main[] {
// In a REPL:
// 1. Read input
// 2. process input
br := bufio.NewReader[os.Stdin]
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess[br]; err != nil {
if err == io.EOF {
return
}
log.Fatal[err]
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess[br *bufio.Reader] [terr error] {
fmt.Printf["> "]
line, _, err := br.ReadLine[]
if err != nil {
return err
}
out, err := processLine[line]
if err != nil {
return err
}
fmt.Printf["< %s\n\n", out]
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine[in []byte] [out []byte, err error] {
return bytes.ToUpper[in], nil
}
9Đăng ký lượt xem
Bây giờ chúng tôi đăng ký lượt xem và đặt khoảng thời gian báo cáo
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
0import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
1Xuất số liệu thống kê
Đăng ký lượt xem
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
2Gói nhập khẩu
Chúng tôi sẽ bổ sung gói gói xuất khẩu Prometheus Go.
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
4Tạo nhà xuất khẩu
Để các số liệu của chúng tôi được xuất sang Prometheus, ứng dụng của chúng tôi cần được hiển thị dưới dạng điểm cuối cạo. Trình xuất OpenCensus Go Prometheus là trình xuất PHẢI được đính kèm với điểm cuối http “/metrics”
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
3Mã từ đầu đến cuối
Nói chung mã sẽ là
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
4Chạy hướng dẫn
Bước này liên quan đến việc chạy ứng dụng hướng dẫn trong một thiết bị đầu cuối và sau đó chạy chính Prometheus trong một thiết bị đầu cuối khác
Đã cài đặt đúng cách, trong một thiết bị đầu cuối, vui lòng chạy
import [
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
]
5Tệp cấu hình Prometheus
Để cho phép Prometheus thu thập thông tin từ ứng dụng của bạn, chúng tôi phải trỏ nó tới ứng dụng hướng dẫn có máy chủ đang chạy trên “localhost. 8888”
Để làm được điều này, trước tiên chúng ta cần tạo một tệp YAML với cấu hình e. g.
var [
// The latency in milliseconds
MLatencyMs = stats.Float64["repl/latency", "The latency in milliseconds per REPL loop", "ms"]
// Counts/groups the lengths of lines read in.
MLineLengths = stats.Int64["repl/line_lengths", "The distribution of line lengths", "By"]
]
5 có nội dung là