go언어 설치

go 2022. 4. 14. 13:34

https://go.dev/doc/install

 

go언어 다운로드

#wget https://go.dev/dl/go1.18.1.linux-amd64.tar.gz

기존 설치된 go 삭제 압축 품

#rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.1.linux-amd64.tar.gz

환경변수 등록

#vi /etc/profile

export PATH=$PATH:/usr/local/go/bin

#reboot

#go version 

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
routing module  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,

routing module

go 2022. 4. 14. 10:46

출처 : https://spectrumdig.blogspot.com/2012/02/routing-module-handler.html?m=0 

 

package main

import (
  "fmt"
)

type Handler func(method string)

type Route struct {
  Method,Pattern string
}

type Routes struct {
  Routes []Route
}
func (r Routes) routeCount() int {
  return len(r.Routes)
}
func (r *Routes) addRoute(method, pattern string, handler Handler) {
  r.Routes=append(r.Routes, Route{Method:method, Pattern:pattern})
  fmt.Printf("route Count : %d\n", len(r.Routes))
  handler(method)
}
func (r *Routes) GET(pattern string, handler Handler) {
  r.addRoute("GET", pattern, handler)
}
func (r *Routes) POST(pattern string, handler Handler) {
  r.addRoute("POST", pattern, handler)
}
func (r *Routes) PUT(pattern string, handler Handler) {
  r.addRoute("PUT", pattern, handler)
}
func (r *Routes) DELETE(pattern string, handler Handler) {
  r.addRoute("DELETE", pattern, handler)
}
func handler(method string) {
  fmt.Printf("%s handler executed \n", method)
}

func main() {
  r:=new(Routes)
  r.GET("/user/:user", handler)
  r.PUT("/add/:user", handler)
  r.POST("/update/:user", handler)
  r.DELETE("/delete/:user", handler)
  for _,v:=range r.Routes {
    fmt.Printf("%s:%s\n",v.Method, v.Pattern)
  }
}

 

 

'go' 카테고리의 다른 글

cryptopunks raritysniper json 파싱  (0) 2022.04.15
JSON Transfer  (0) 2022.04.14
Web Handler  (0) 2022.04.14
go언어 설치  (0) 2022.04.14
golang 개발환경 구축  (0) 2021.06.21
블로그 이미지

iesay

,

출처

https://ichi.pro/ko/prometheus-mich-grafanaleul-sayonghan-node-js-aepeullikeisyeon-moniteoling-194123578535907

 

const http = require('http')
const url = require('url')
const client = require('prom-client')
// Create a Registry which registers the metrics
const register = new client.Registry()
// Add a default label which is added to all metrics
register.setDefaultLabels({
  app: 'example-nodejs-app'
})
// Enable the collection of default metrics
client.collectDefaultMetrics({ register })
// Create a histogram metric
const httpRequestDurationMicroseconds = new client.Histogram({
  name: 'http_request_duration_seconds',
  help: 'Duration of HTTP requests in microseconds',
  labelNames: ['method', 'route', 'code'],
  buckets: [0.1, 0.3, 0.5, 0.7, 1, 3, 5, 7, 10]
})
// Register the histogram
register.registerMetric(httpRequestDurationMicroseconds)
// Define the HTTP server
const server = http.createServer(async (req, res) => {
    // Start the timer
  const end = httpRequestDurationMicroseconds.startTimer()
// Retrieve route from request object
  const route = url.parse(req.url).pathname
if (route === '/metrics') {
    // Return all metrics the Prometheus exposition format
    res.setHeader('Content-Type', register.contentType)
    res.end(register.metrics())
  }
// End timer and add labels
  end({ route, code: res.statusCode, method: req.method })
})
// Start the HTTP server which exposes the metrics on http://localhost:8080/metrics
server.listen(8080)

nodejs server.js

 

프로메테우스 설치 

#apt install prometheus

#cd /etc/prometheus

#vi prometheus.yml

global:
  scrape_interval: 5s
scrape_configs:
  - job_name: "example-nodejs-app"
    static_configs:
      - targets: ["localhost:8080"]

#service prometheus restart

 

prometheus_http_request_duration_seconds_bucket

프로메테우스 실행 완료  

 

Grafana

#cd /etc/prometheus

#vi datasources.ym


apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    orgId: 1
    url: http://docker.for.mac.host.internal:9090
    basicAuth: false
    isDefault: true
    editable: true

 


docker run --rm -p 3000:3000 \
  -e GF_AUTH_DISABLE_LOGIN_FORM=true \
  -e GF_AUTH_ANONYMOUS_ENABLED=true \
  -e GF_AUTH_ANONYMOUS_ORG_ROLE=Admin \
  -v `pwd`/datasources.yml://etc/prometheus \
  grafana/grafana-enterprise:8.3.6-ubuntu


 

 

'시스템' 카테고리의 다른 글

MongoDB Replica Set  (0) 2023.01.10
s3 권한이 없는 경우  (0) 2022.05.17
nginx/1.18.0 (Ubuntu20.04) gzip  (0) 2022.03.07
nginx/1.18.0 (Ubuntu20.04) brotli  (0) 2022.01.11
Gitlab CI react app 배포  (0) 2021.12.31
블로그 이미지

iesay

,