# Hyperledger Fabric Operations Service Tutorial

## Goal: Operation Service 기능을 byfn network에서 실행

### Environment

* Ubuntu 18.04 LTS
* Hyperledger Fabric v2.0.0

### Configuration

peer와 orderer의 설정 파일에서 listenAddress, metrics provider와 TLS 설정을 확인 및 변경한다.

* peer: fabric-samples/config/core.yaml

```yaml
###############################################################################
#
#    Operations section
#
###############################################################################
operations:
    # host and port for the operations server
    listenAddress: 127.0.0.1:9443

    # TLS configuration for the operations endpoint
    tls:
        # TLS enabled
        enabled: false

        # path to PEM encoded server certificate for the operations server
        cert:
            file:

        # path to PEM encoded server key for the operations server
        key:
            file:

        # most operations service endpoints require client authentication when TLS
        # is enabled. clientAuthRequired requires client certificate authentication
        # at the TLS layer to access all resources.
        clientAuthRequired: false

        # paths to PEM encoded ca certificates to trust for client authentication
        clientRootCAs:
            files: []

###############################################################################
#
#    Metrics section
#
###############################################################################
metrics:
    # metrics provider is one of statsd, prometheus, or disabled
    provider: prometheus

    # statsd configuration
    statsd:
        # network type: tcp or udp
        network: udp

        # statsd server address
        address: 127.0.0.1:8125

        # the interval at which locally cached counters and gauges are pushed
        # to statsd; timings are pushed immediately
        writeInterval: 10s

        # prefix is prepended to all emitted statsd metrics
        prefix: 
```

* orderer: fabric-samples/config/orderer.yaml

```yaml
################################################################################
#
#   Orderer Configuration
#
#   - This controls the type and configuration of the orderer.
#
################################################################################
General:
    # Listen address: The IP on which to bind to listen. (Prometheus)
        ListenAddress: 127.0.0.1:8443  

    # Listen port: The port on which to bind to listen.
    ListenPort: 7050

    # TLS: TLS settings for the GRPC server.
    TLS:
        Enabled: false
        # PrivateKey governs the file location of the private key of the TLS certificate.
        PrivateKey: tls/server.key
        # Certificate governs the file location of the server TLS certificate.
        Certificate: tls/server.crt
        RootCAs:
          - tls/ca.crt
        ClientAuthRequired: false
        ClientRootCAs:

###############################################################################
#
#    Metrics section
#
###############################################################################
metrics:
    # metrics provider is one of statsd, prometheus, or disabled
    provider: prometheus

    # statsd configuration
    statsd:
        # network type: tcp or udp
        network: udp

        # statsd server address
        address: 127.0.0.1:8125

        # the interval at which locally cached counters and gauges are pushed
        # to statsd; timings are pushed immediately
        writeInterval: 10s

        # prefix is prepended to all emitted statsd metrics
        prefix:
```

* 모니터링을 위한 container(peer 및 orderer) 환경 변수 설정: \~/fabric-samples/first-network/base/docker-compose-base.yaml
  1. CORE\_OPERATIONS\_LISTENADDRESS
  2. CORE\_METRICS\_PROVIDER

```yaml
# 환경 변수 설정 1(orderer)
orderer.example.com:
    container_name: orderer.example.com
    extends:
      file: peer-base.yaml
      service: orderer-base
    environment:
      - ORDERER_OPERATIONS_LISTENADDRESS=orderer.example.com:8443
      - ORDERER_METRICS_PROVIDER=prometheus
    volumes:
        - ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
        - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
        - orderer.example.com:/var/hyperledger/production/orderer
    ports:
      - 7050:7050

# 환경 변수 설정 2(아래는 peer0.org1.example.com): 모니터링을 원하는 피어의 environmnet 섹션에 환경 변수를 동일하게 추가
peer0.org1.example.com:
    container_name: peer0.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LISTENADDRESS=0.0.0.0:7051
      - CORE_PEER_CHAINCODEADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org1.example.com:8051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_OPERATIONS_LISTENADDRESS=peer0.org1.example.com:9443
      - CORE_METRICS_PROVIDER=prometheus
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
        - peer0.org1.example.com:/var/hyperledger/production
    ports:
      - 7051:7051
```

설정을 끝냈다면 byfn network를 실행 후 cli container로 접속한다.

```bash
# change directory
cd ~/first-network

# byfn network start
./byfn.sh up

# cli container로 접속
docker exec -it cli bash
```

### **logspec API**

```bash
# curl command로 peer0.org1.example.com에 logspec API 요청(GET)
curl peer0.org1.example.com:9443/logspec
{"spec":"info"}
```

{% hint style="info" %}
docker container 안에 curl이 설치되지 않은 경우에는 curl command를 사용할 수 없음<br>

```bash
# docker container 안에서 명령어 실행
apk add curl
```

{% endhint %}

### healthz API

```bash
# orderer.example.com에 healthz API 요청(GET)
curl orderer.example.com:8443/healthz
{"status":"OK","time":"2020-03-02T06:36:26.278635314Z"}
```

### metrics API

metrics API를 요청하면 [https://hyperledger-fabric.readthedocs.io/en/release-1.4/metrics\_reference.htm](https://hyperledger-fabric.readthedocs.io/en/release-1.4/metrics_reference.html)에 따른 peer/orderer 별 수집 가능한 메트릭 이름과 값이 전부 출력됨

```bash
# peer0.org1.example.com에 metrics API 요청(GET)
curl peer0.org1.example.com:9443/metrics
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boohyung.gitbook.io/tech/development/hyperledger-fabric-operations-service-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
