Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

即、New Relic / New Relic NOW!

uzulla
June 15, 2022

即、New Relic / New Relic NOW!

at: NRUG (New Relic User Group) Vol.3
https://nrug.connpass.com/event/247057/

uzulla

June 15, 2022
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. ଈɺNew Relic ʙPHPer͕ҙࣝ௿͘΍͍ͬͯ͘ʙ at: NRUG (New Relic User Group) Vol.3

    https://nrug.connpass.com/event/247057/ date: 2022/06/15 speaker: uzulla
  2. $ mkdir unconscious-app; cd unconscious-app; $ docker run -d -v

    `pwd`:/var/www -p 8080:80 \ --name unconscious-app php:8.1-apache $ echo "<?php phpinfo();" > html/index.php $ find . ./html/index.php ʢphp-fpm͡Όͳ͍ͷ΋ҙࣝ௿͍ʢॾઆ͋Δʣʣ ʢhtmlҎԼʹ͓޷͖ͳPHPΛ͓͚͹ΑΖ͍͠ʣ ʢhtml͸dockerىಈ࣌ʹࣗಈ࡞੒͞ΕΔҙࣝͷ௿͞ʣ
  3. ҰԠɺىಈɾఀࢭɾফ͠ํ # ఀࢭ $ docker stop unconscious-app # ࠶։ $

    docker start unconscious-app # ࡟আ $ docker rm unconscious-app # image΋ফ͍ͨ͠ਓ $ docker image ls |grep 8.1-apache php 8.1-apache c9248ee25e78 34 hours ago 458MB $ docker image rm c9248ee25e78 imageͷ c9248ee25e78 ͸มΘΔͷͰɺదٓஔ͖׵͍͑ͯͩ͘͞ɻ ଞίϯςφͰ࢖ͬͯΔͱফͤ·ͤΜ͠ɺ࡟আඞਢͰ͸ͳ͍Ͱ͢ɻ
  4. # ҎԼɺNRࢦఆͷϫϯϥΠφʔΛվ଄ͨ͠෺ɺ౰࠲͸ಈ͘ͱࢥ͍·͢ $ apt update -y && apt install -y

    gnupg $ echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | \ tee /etc/apt/sources.list.d/newrelic.list # wgetͱ͔ɺPHPerͳΒ͍ΒΜΑͶʂphpͰ୅༻ʂ $ php -r "readfile('https://download.newrelic.com/548C16BF.gpg');" | \ apt-key add - # php5ͬͯ͋Δ͚Ͳɺ8Ͱ΋5Ͱ͢ ☺ $ apt-get update && apt-get -y install newrelic-php5 $ newrelic-install install
  5. Ωʔ͕ೖ͔ͬͨνΣοΫ $ php -i |grep newrelic |grep license newrelic.license =>

    b7...AL => b7...AL Φοέʔʂʂʂ ʢ͜ΕͰnewrelic.soͷϩʔυνΣοΫ΋ಉ࣌ʹ͍ͯ͠·͢ʣ
  6. mod_phpʹ൓ө͢ΔͨΊɺ࠶ىಈ root@5b8ea9bb3d5a:/var/www/html# exit # docker಺͔Βexit͠ɺϗετͰ $ docker stop unconscious-app $

    docker start unconscious-app # ҰԠىಈΛ֬ೝʢStatus͕਺ඵͳΒ࠶ىಈͯ͠ΔͰ͠ΐ͏ʣ $ docker ps CONTAINER ID IMAGE ུ STATUS PORTS NAMES 27f0166df71b php:8.1-apache ུ Up 6 seconds 0.0.0.0:8080->80/tcp unconscious-app
  7. Agent͕ىಈ͍ͯ͠Δ͔νΣοΫ # Dockerͷίϯςφʹ͸͍Δ $ docker exec -it unconscious-app bash psͰ֬ೝ

    root@27f0166df71b:/var/www/html# ps -fax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 apache2 -DFOREGROUND 27 ? Sl 0:00 /usr/bin/newrelic-daemon --agent (ུ) newrelic-daemon ͕͍Ε͹OKʢPHP͕ىಈͯ͘͠Ε·͢ʣ
  8. ͜͜Ͱ໰୊ • ϩάసૹ͸ެࣜͷ newrelic-infra agent ͕ఆ൪ • ࣮͸ɺ݁ߏ Installཁ͕݅͋ΔɺDockerͰ͸໘౗ •

    ࢲʮ໘౗…ҙࣝߴ͍…͋ɺsyslogରԠͯ͠Δ͡ΌΜʂʯ • https://docs.newrelic.com/jp/docs/logs/log-api/use-tcp- endpoint-forward-logs-new-relic
  9. Ϧϩʔυͯ͠ɺϑΝΠϧ͕Ͱ͖͍ͯΔ͜ͱΛݟΔ # cat /var/www/my.log MyApp: can you hear me??. MyApp:

    can you hear me??. MyApp: can you hear me??. MyApp: can you hear me??.
  10. ConfigΛॻ͘ $ apt install -y vim # Ͱ΋͍͍͠ɺnanoͰ΋ྑ͍͕ $ vi

    /etc/syslog-ng/syslog-ng.conf (NR΁ͷϩάసૹʹ͔͠࢖Θͳ͍ͷͰɺ த਎Λશ෦ফͯ͠ɺ࣍ͷϖʔδͷ಺༰ΛೖΕΔ)
  11. @version: 3.27 source s_files { file("/var/www/my.log"); }; template NRFormat {

    template( "b77{ུ}NRAL <${PRI}>1 ${ISODATE} ${HOST:--} ${PROGRAM:--} ${PID:--} ${MSGID:--} ${SDATA:--} $MSG\n" ); template_escape(no); }; destination d_newrelic { network( "newrelic.syslog.nr-data.net" port(6514) transport("tls") tls(peer-verify(no)) template(NRFormat) ); }; log { source(s_files); destination(d_newrelic); }; ʢ࠷௿ݶͷҙࣝ௿͍ConfྫɺΩʔ͸ॻ͖׵͍͑ͯͩ͘͞ʣ
  12. 10෼଴ͬͯ΋ग़ͳ͍ͳΒࣦഊͯ͠Δ͔΋ # ࣏͢ɺΩʔؒҧ͑ͯͳ͍͔ʁͱ͔ root@27f0166df71b:/etc/syslog-ng# vi /etc/syslog-ng/syslog-ng.conf # PID͠Β΂ͯkillʢ͋Δ͍͸࠶ىಈʣ root@27f0166df71b:/etc/syslog-ng# ps

    -A|grep syslog-ng 1452 ? 00:00:00 syslog-ng 1453 ? 00:00:00 syslog-ng root@27f0166df71b:/etc/syslog-ng# kill 1452 1453 # ىಈ root@27f0166df71b:/etc/syslog-ng# syslog-ng --no-caps # ͜ͷޙͰɺԿ౓͔Ϧϩʔυͯ͠ϩάΛ࠶౓௥ه͢Δ
  13. Incident Driven New Relic • ൃੜͨ͠Τϥʔ΍ྫ֎͕ͲΜͲΜೖͬͯศར • ϩάΛసૹ͢Δͱݕࡧ͠์୊ͩ͠ɺΞϥʔτ΋Մೳ • ʮΞϥʔτઃఆͱ͔ͪΐͬͱҙ͕ࣝߴ͍ͳʁʯ

    • ͱΓ͍͋͑ͣΕͱ͘ɺ໰୊͕Ͱͯ͘Δ·Ͱ͸์ஔ • ࢲ͕NRΛΈΔͷ͸େମ໰୊͕ى͖ͨ࣌ • ·͋ɺౖΒΕ͔ͯΒݟ͍͍ͨͬͯ͡Όͳ͍
  14. ৮͓ͬͯ΅͑ΖNew Relic • NR͕ͳͯ͘΋ੈք͸ճΔ͕ศར • खݩͷDockerͱ͔Ͱ׳ΕΔ => ʮͱΓ͍͋͑ͣΕͱ͔͘ʯ • ʮࣗ෼ͷNRʯ͕ͳ͍ͱɺؾܰʹࢼͤͳ͍ΑͶɺΘ͔Δ

    • ͱΓ·ɺαΠϯΞοϓͯ͠ςϨϝτϦͱ͔ϩάͱ͔ૹΓ͚ͭͯ ͓͚͹͍ʔΜ͡ΌͶʔͷʁ • ා͘ͳ͘ͳͬͨʁͦͨ͠ΒstgʹͰ΋Ϳͬ͜ΜͰΈ·͠ΐ͏ʙʙ