Slide 1

Slide 1 text

PerlͰ΋෼ࢄτϨʔγϯά͍ͨ͠ʂ AWS::XRay ʹΑΔղੳͱͦͷ࣮૷ ɹ 2019.01.26 YAPC::Tokyo @fujiwara

Slide 2

Slide 2 text

@fujiwara ٕज़෦ SRE github.com/fujiwara sfujiwara.hatenablog.com ISUCON8 ຊબग़୊νʔϜ

Slide 3

Slide 3 text

Game & Community

Slide 4

Slide 4 text

Agenda ෼ࢄτϨʔγϯάͱ͸ AWS X-Ray ͷ঺հ X-Ray ʹΑΔ ISUCON8 ຊબ໰୊ͷղੳྫ AWS::XRay ͷ಺෦࣮૷ ίετͷ࿩

Slide 5

Slide 5 text

෼ࢄτϨʔγϯάͱ͸

Slide 6

Slide 6 text

෼ࢄτϨʔγϯάͱ͸ GoogleͷDapper࿦จ1͕΋ͱ (2010೥) ෳ਺ͷݴޠͰߏங͞ΕΔେن໛෼ࢄγεςϜͷύϑΥʔϚϯε Λղੳ͢ΔͨΊʹ࡞ΒΕͨ 1 https://ai.google/research/pubs/pub36356

Slide 7

Slide 7 text

෼ࢄτϨʔγϯάͷඞཁੑ ෼ࢄγεςϜ(ϚΠΫϩαʔϏε)Ͱ͸ͻ ͱͭͷॲཧ͕ෳ਺ͷίϯϙʔωϯτʹ· ͕ͨΔ ύϑΥʔϚϯεͷ໰୊͕෼͔ΓͮΒ͍ ো֐Λݟ͚ͭΔͷ΋೉͍͠ ਤ͸ Dapper࿦จ 1 ΑΓ 1 https://ai.google/research/pubs/pub36356

Slide 8

Slide 8 text

෼ࢄτϨʔγϯάͰՄࢹԽ ෳ਺ͷίϯϙʔωϯτʹ·͕ͨͬͯॲཧ ͞Εͨ݁ՌΛՄࢹԽͯ͠೺ѲΛ༰қʹ ֤ίϯϙʔωϯτ͕ݺͼग़͞Εͨॱংɺ λΠϛϯάɺཁ͕ͨ࣌ؒ͠ݟ͑Δ

Slide 9

Slide 9 text

֓೦ Trace͸ෳ਺ͷSpanΛؚΉ༗޲ඇ८ճάϥϑ5 5 https://github.com/opentracing/specification/blob/master/specification.md

Slide 10

Slide 10 text

֓೦ Span ͸ҎԼͷཁૉΛ΋ͭ ໊લ ։࢝࣌ࠁ ऴྃ࣌ࠁ Context (ID, Trace-ID, Parent) Tag {Key: Value},... Log {Key: Value},...

Slide 11

Slide 11 text

෼ࢄτϨʔγϯάͷ࣮૷ͱ࢓༷ Zipkin(OpenZipkin)2: Twitter → OSS 2012 Jeager3: Uber → CNCF OSS 2016 OpenTracing4: ෼ࢄτϨʔγϯάͷ࢓༷ CNCF 2016 AWS X-Ray: AWS͕ఏڙ ϚωʔδυαʔϏε 2016 StackDriver Trace: Google͕ఏڙ ZipkinΫϥΠΞϯτޓ׵ 2017 4 https://opentracing.io/ 3 https://www.jaegertracing.io/ 2 https://zipkin.io/

Slide 12

Slide 12 text

Ͳ͏΍ͬͯτϨʔε͢Δ͔ ΞϓϦέʔγϣϯͰ ΞϓϦέʔγϣϯ(SDK)ͰτϨʔεσʔ λΛ࡞੒ͯ͠సૹ༻ΤʔδΣϯτʹૹ৴ ͔ͦ͜Βαʔό(ίϨΫλ)ʹૹΒΕΔ ! ΞϓϦέʔγϣϯ಺෦ͷڍಈ΋τ ϨʔεͰ͖Δ " ݴޠʹґଘ(SDK)

Slide 13

Slide 13 text

Ͳ͏΍ͬͯτϨʔε͢Δ͔ αʔϏεϝογϡͰ αʔϏεϝογϡͷσʔλϓϨʔϯ(ྫ Envoy) Ͱߦ͏ ! ݴޠʹґଘ͠ͳ͍ " ΞϓϦέʔγϣϯ಺෦ͷڍಈ͸τ ϨʔεͰ͖ͳ͍

Slide 14

Slide 14 text

AWS X-Ray

Slide 15

Slide 15 text

AWS X-Ray AWS ʹΑΔ෼ࢄτϨʔγϯάͷϚωʔδυαʔϏε

Slide 16

Slide 16 text

AWS X-Ray ͷߏ੒ཁૉ6 X-Ray API AWS͕ఏڙ͍ͯ͠ΔAPI X-Ray daemon τϨʔεసૹ༻σʔϞϯ X-Ray SDK X-Ray daemonʹτϨʔεΛૹ৴͢Δͨ ΊͷϥΠϒϥϦ X-Ray console τϨʔεΛՄࢹԽ͢ΔViewer 6 https://docs.aws.amazon.com/ja_jp/xray/latest/devguide/aws-xray.html

Slide 17

Slide 17 text

ϚωʔδυαʔϏεͳͷͰτϨʔεΛૹΔ͚ͩͰ͙͢࢖͑Δ σʔλͷอଘ΍αʔόͷՄ༻ੑΛؾʹ͢Δඞཁ͕શ͘ͳ͍

Slide 18

Slide 18 text

ͲͷΑ͏ʹτϨʔεΛૹ৴͢Δ͔ X-Ray daemon7 ͕ UDP port 2000 Λ listen ΞϓϦέʔγϣϯ(SDK)͔ΒτϨʔεσʔλΛUDPͰ౤͛Δ {"format": "json", "version": 1} { "trace_id": "1-5759e988-bd862e3fe1be46a994272793", "id": "defdfd9912dc5a56", "start_time": 1461096053.37518, "end_time": 1461096053.4042, "name": "MyApp" } 7 https://github.com/aws/aws-xray-daemon

Slide 19

Slide 19 text

ͲͷΑ͏ʹτϨʔεΛૹ৴͢Δ͔ X-Ray daemon ͕όοϑΝϦϯάͯ͠όονͰ X-Ray API ʹૹ৴ X-Ray API ʹಧ͍ͨ͋ͱ͸׬શʹϚωʔδυ σʔλอ࣋ 30೔(ݹ͍ͷ͸ফ͑Δ) τϨʔε਺ͱදࣔ࣌ͷεΩϟϯͨ͠ྔʹΑΔ׬શैྔ՝ۚ

Slide 20

Slide 20 text

X-Ray ͷΞʔΩςΫνϟͷ͍͍ͱ͜Ζ τϨʔεσʔλ͸େྔʹൃੜ͢Δ 1ϦΫΤετॲཧதʹଟ͍ͱ਺ेҎ্ ຖճΞϓϦέʔγϣϯ͔Βಉظ௨৴(TCP) → ϨΠςϯγ૿Ճʹ௚݁͢Δ ಛʹো֐࣌ X-Ray daemon ʹ͸UDPͰ౤͛ͬͺͳ͠ → ΞϓϦέʔγϣϯ΁ͷӨڹ͕ܰඍ X-Ray daemon ͕མ͍ͪͯͯ΋ΞϓϦʹӨڹ͠ͳ͍

Slide 21

Slide 21 text

X-Ray ༻ޠͰ͸ εύϯ → ηάϝϯτ ࠷ॳʹઆ໌ͨ͠Ұൠͷ༻ޠ(εύϯ)ͱ͸ҧ͏͚Ͳಉ͡΋ͷ Ҏ߱͸ X-Ray ͷ࿩ͳͷͰηάϝϯτͱݺͼ·͢

Slide 22

Slide 22 text

AWS X-Ray Λ Perl Ͱ࢖͓͏

Slide 23

Slide 23 text

X-Ray SDK τϨʔεΛ X-Ray daemon ʹૹΔϥΠϒϥϦ AWS͕ఏڙ͍ͯ͠Δͷ͸ Java, Go, Node, Python, Ruby, .NET ͋Εʁࠓ೔͸ YAPC (Yet Another Perl Conference)…

Slide 24

Slide 24 text

! Perl SDK ͸ެࣜʹ͸ͳ͍ CPAN ʹ΋ݟ౰ͨΒͳ͍ X-Ray API Λୟ͘ϥΠϒϥϦ͸͋Δ (Paws::XRay)

Slide 25

Slide 25 text

͍͍ͩͨશ෦ Perl ࠷ۙͷ৽͍͠ϓϩδΣΫτ͸ Go ͚ͩͲ௕೥ͷப͸ Perl

Slide 26

Slide 26 text

AWS::XRay Λॻ͖·ͨ͠ ཁ͢Δʹ JSON Λ UDP Ͱ౤͛Ε͹͍͍͚ͩ ͳͷͰ؆୯ 300ߦ͙Β͍͔͠ͳ͍ capture $name, sub {} ͰηάϝϯτΛ࡞ͬͯૹ৴ use AWS::XRay qw/ capture /; capture "myApp", sub { capture "foo", sub { # do something ... capture "bar", sub { # ... }; }; };

Slide 27

Slide 27 text

Plack::Middleware::XRay ૊ΈࠐΉͱτϨʔεΛ։࢝ͯ͘͠ΕΔ Plack Middleware 100ߦ͙Β͍ .psgi Ͱ enable ͢Δ͚ͩ (Φϓγϣϯ͸ޙड़) use Plack::Builder; builder { enable "XRay", name => "myApp"; $app; };

Slide 28

Slide 28 text

AWS::XRay ։ൃܦҢ Perl Ͱ΋ X-Ray ࢖͍͍ͨ… → JSONΛUDPͰ౤͛Δ͚ͩͩ͠؆୯ͩΑͶʁ → Ͱ΋τϨʔε࢓ࠐΉͷΊΜͲ͍͘͞ͳ… → ! Devel::KYTProf ͕औͬͨ݁ՌΛૹΕ͹ʂ

Slide 29

Slide 29 text

Devel::KYTProf::Logger::XRay Devel::KYTProf ͱ͍͏ϓϩϑΝΠϥͷϩΨʔͱͯ͠ಈ࡞ͯ͠ τϨʔεσʔλΛૹ৴ Devel::KYTProf ͸ use ͢Δ͚ͩͰDBIͳͲͷݺͼग़͠Λܭଌ ϩάΛग़ྗͯ͘͠ΕΔศརϓϩϑΝΠϥ use Devel::KYTProf; # your code 315.837 ms [DBI::st] select * from table where name = ? (1 rows) | main:23 1464.204 ms [LWP::UserAgent] GET http://www.hatena.ne.jp/ | main:25

Slide 30

Slide 30 text

Devel::KYTProf::Logger::XRay use Devel::KYTProf::Logger::XRay; Devel::KYTProf->logger("Devel::KYTProf::Logger::XRay"); ͜ΕͰDevel::KYTProfͰܭଌͰ͖Δ΋ͷ͸શͯτϨʔεର৅ʹ DBI, LWP::UserAgent, Furl::HTTP, Cache::Memcached::Fast, MogileFS::Client ࣗ෼Ͱର৅ϞδϡʔϧΛ௥Ճ͢Δ͜ͱ΋Մೳ (Redis::Fastͱ͔)

Slide 31

Slide 31 text

AWS::XRay ։ൃܦҢ Perl Ͱ΋ X-Ray ࢖͍͍ͨ… → JSONΛUDPͰ౤͛Δ͚ͩͩ͠؆୯ͩΑͶʁ → Ͱ΋τϨʔε࢓ࠐΉͷΊΜͲ͍͘͞ͳ… → ! Devel::KYTProf ͕औͬͨ݁ՌΛૹΕ͹ʂ → Logger ࠩ͠ସ͑Ͱ͖Δ KYTProf++ ਖ਼௚ KYTProf ͕ͳ͔ͬͨΒ࡞ͬͯͳ͔͔ͬͨ΋͠Εͳ͍

Slide 32

Slide 32 text

ISUCON 8 ຊબ໰୊ͷղੳ

Slide 33

Slide 33 text

ISUCON is ͳʹ ͍͍ײ͡ʹεϐʔυΞοϓίϯςετͷུ ఏڙ͞Εͨαʔό্Ͱಈ͘WebΞϓϦέʔγϣϯΛߴ଎Խ !!!!!!!!!! ༏উ৆ۚ100ສԁ 2011೥͔Βຖ೥։࠵͞Ε͍ͯΔ (ओ࠵ LINEࣾ) 2018೥ͷ ISUCON 8 Λ DeNA ͞ΜͱΧϠοΫͰग़୊ ࣍ͷ࿮͸ karupanerura ͞ΜʹΑΔ ʮISUCON8༧બ໰୊࡞੒ͷཪଆʯͷτʔΫͰ͢

Slide 34

Slide 34 text

ISUCON 8 ຊબ໰୊ Ծ૝௨՟औҾαʔϏε ISUCOIN ۜߦAPI, ϩάه࿥API͸ӡӦ͕༻ҙͨ͠ ϒϥοΫϘοΫεͰఏڙ͞Εɺhttps Ͱ ϦΫΤετΛૹΔඞཁ͕͋Δ ֎෦APIݺͼग़͠Λ͍͔ʹ্ख͘ॲཧ͢ Δ͔͕ϙΠϯτ

Slide 35

Slide 35 text

ISUCON 8 ຊબ໰୊

Slide 36

Slide 36 text

ISUCON 8 ຊબ໰୊Λ X-Ray Ͱղੳͯ͠ΈΔ

Slide 37

Slide 37 text

Perl ࣮૷ʹ AWS::XRay Λ૊ΈࠐΉ ఏڙ͞Εͨݴޠ࣮૷͸ Go, Perl, PHP, Python, Ruby YAPC ͳͷͰ౰વ Perl ࣮૷Ͱ https://github.com/isucon/isucon8-final

Slide 38

Slide 38 text

X-Ray daemon Λ༻ҙ ॳظ࣮૷͸ docker-compose ͳͷͰద౰ʹ Dockerfile Λ༻ҙ8 FROM amazonlinux RUN yum install -y unzip RUN curl -o daemon.zip https://s3.dualstack.us-east-2.amazonaws.com/ aws-xray-assets.us-east-2/xray-daemon/aws-xray-daemon-linux-3.x.zip RUN unzip daemon.zip && cp xray /usr/bin/xray ENTRYPOINT ["/usr/bin/xray", "-b", "0.0.0.0:2000", "--local-mode"] EXPOSE 2000/udp 8 https://docs.aws.amazon.com/ja_jp/xray/latest/devguide/xray-daemon-local.html

Slide 39

Slide 39 text

docker-compose.yml services: isucoin: environment: # ུ AWS_XRAY_DAEMON_ADDRESS: 'xray:2000' links: - mysql - xray xray: build: xray environment: AWS_REGION: "ap-northeast-1" AWS_ACCESS_KEY_ID: "000000000000" AWS_SECRET_ACCESS_KEY: "****"

Slide 40

Slide 40 text

AWS::XRay ૊ΈࠐΈ app.psgi ʹ5ߦ௥Ճ + cpanfile # app.psgi use Devel::KYTProf::Logger::XRay; use AWS::XRay; Devel::KYTProf->logger("Devel::KYTProf::Logger::XRay"); builder { enable 'XRay', name => 'isucoin'; # ... # cpanfile requires 'AWS::XRay'; requires 'Plack::Middleware::XRay'; requires 'Devel::KYTProf::Logger::XRay';

Slide 41

Slide 41 text

ϕϯνΛ࣮ߦ͢Δͱ…τϨʔε͕ʂ

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

ϘτϧωοΫൃݟ SELECT * FROM trade ORDER BY id DESC 23ສߦΛಡΈऔͬͯ1ߦ࢖ࣺ͍ͬͯͯͯΔ LIMIT 1 ͢Ε͹ OK ! ʮ͜Ε͸΂ͭʹ෼ࢄτϨʔγϯά͍Βͳ͍ΑͶ?ʯ ! ʮslowlog ݟΕ͹෼͔Δ΍Ζʯ

Slide 50

Slide 50 text

֎෦APIݺͼग़͠Λղੳ

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

֎෦ͷHTTP APIݺͼग़͠ͷதͰ஗͍΋ͷΛಛఆ (ग़୊ҙਤͰ) ֎෦API͝ͱʹϨΠςϯγʹࠩΛ෇͚͍ͯͨ ! ʮ͜Ε΋ผʹʰ෼ࢄʱͰ͸ͳ͍ΑͶʯ ! ʮDevel::KYTProf ͰϩάΛग़ͤ͹෼͔Δ΍Ζʯ

Slide 55

Slide 55 text

֎෦API࣮૷ʹ X-Ray Λ૊ΈࠐΜͰΈΔ (ڝٕͰ͸࣮ࡍ͸ϒϥοΫϘοΫε͚ͩͲ) xray-sdk-go Λ૊ΈࠐΈ // sql.Open -> xray.SQL db, err := xray.SQL("mysql", dsn) // http.Handler Λ xray.Handler Ͱ wrap return xray.Handler( xray.NewFixedSegmentNamer("Isubank"), myHandler, )

Slide 56

Slide 56 text

Perl ଆͷ HTTPϦΫΤετૹ৴΋ capture ݺͼग़͠ݩΛ఻͑ΔͨΊʹ X-Amzn-Trace-ID ϔομΛ෇༩ capture "externalHTTP", sub { my $segment = shift; $res = $self->client->post( $self->endpoint . $p, [ "Content-Type" => "application/json", "Authorization" => "Bearer " . $self->app_id, "X-Amzn-Trace-ID" => $segment->trace_header, ], $body, ); };

Slide 57

Slide 57 text

Isucoin(ΞϓϦ)→Isubank(API)

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

֎෦APIݺͼग़͠ͷதͰߦΘΕͨSQLΫΤϦ΋ಉ͡ը໘Ͱ

Slide 61

Slide 61 text

ʰ෼ࢄʱτϨʔγϯάʂ

Slide 62

Slide 62 text

ϓϩηεΛ·͕ͨͬͨ௥੻ͷ࢓૊Έ Isucoin(Perl) ϦΫΤετ։࢝࣌ʹ X-Amzn-Trace-ID Λൃߦ 1-5c4902d7-e8fe620cab66e01e57df8a12 1-[timestamp]-[unique_id] sprintf( "1-%x-%s", CORE::time(), unpack("H*", Crypt::URandom::urandom(12)), );

Slide 63

Slide 63 text

֎෦ϦΫΤετݺͼग़͠ʹϔομΛ෇༩ use AWS::XRay qw/capture/; capture "externalHTTP", sub { # Ͳ͜Ͱ΋ݺ΂Δ(਌͕͍ͳ͚Ε͹RootʹͳΔ) my $segment = shift; $furl->POST( "https://example.com", [ "X-Amzn-Trace-ID" => $segment->trace_header ], $body, ); }; POST / HTTP/1.1 Host: example.com X-Amzn-Trace-ID: Root=1-5c4902d7-e8fe620cab66e01e57df8a12;Parent=851cf9ec07b528c2 ...

Slide 64

Slide 64 text

ϔομΛड৴ͨ͠API(Isubank)͕Ҿ͖ܧ͙ Root=1-5c4902d7-e8fe620cab66e01e57df8a12;Parent=851cf9ec07b528c2 # ࣮ࡍ͸Go͚ͩͲઆ໌ͷͨΊʹPerl AWS::XRay::Segment->new( id => new_id(), trace_id => "1-5c4902d7-e8fe620cab66e01e57df8a12", parent_id => "851cf9ec07b528c2", ); ͦͷτϨʔεΛ X-Ray daemon ΁ૹ৴ → X-Ray consoleͰτϨʔε͕࿈݁ͯ͠දࣔ͞ΕΔ

Slide 65

Slide 65 text

AWS::XRay ͷ಺෦࣮૷

Slide 66

Slide 66 text

τϨʔε͍ͨ͠ͱ͜ΖͰʮݱࡏͷʯRoot΍Parent͕ඞཁ package App; sub main { capture "App::main", sub { # Root my $segment = shift; Model::call($param); # ֎෦ݺͼग़͠Λ൐͏ॲཧ }; }; package Model; sub call() { my $param = shift; # ࣮ࡍʹ֎ΛݺͿͷ͸͕ͩ͜͜ݺͼग़͠ݩͷsegmentΛ஌Βͳ͍ͱRoot͕Θ͔Βͳ͍ $furl->post($url, [ "X-Amzn-Trace-ID" => "????", ], $param); } call($param)Λcall($segment,$param)ʹॻ͖׵͑ͨ͘ͳ͍

Slide 67

Slide 67 text

AWS::XRay Ͱ͸Ҿ਺Λ౉͞ͳ͍͍ͯ͘Α͏ʹͳͬͯΔ package App; sub main { capture "App::main", sub { # Root my $segment = shift; Model::call($param); # ֎෦ݺͼग़͠Λ൐͏ॲཧ }; }; package Model; sub call() { my $param = shift; # ਌ͷ͜ͱ͸஌Βͳ͍͚Ͳͱʹ͔͘ capture ͢Ε͹Α͍ capture "Model::call", sub { my $segment = shift; # ਌͔Βݺ͹Ε͍ͯΕ͹ࣗಈతʹࢠʹͳΔ $furl->post($url, [ "X-Amzn-Trace-ID" => $segment->trace_header, ], $param); }; }

Slide 68

Slide 68 text

Ͳ͏΍࣮ͬͯ૷͞Ε͍ͯΔ͔ Perl ͷຐ๏ͷͻͱͭ local perldoc -f local ͋ͳ͕ͨຊ౰ʹ๬ΜͰ͍Δͷ͸ my ͷํͰ͠ΐ͏; local ͸΄ͱΜ Ͳͷਓʑ͕ʮϩʔΧϧʯͱߟ͑Δ΋ͷͱ ҧ͏͔ΒͰ͢ɻ "local" ͸ϦετΞοϓ͞Εͨม਺Λɺғ͍ͬͯΔϒϩοΫɺ ϑΝΠϧɺeval ͷதͰɺϩʔΧϧͳ΋ͷʹ͠·͢ɻ !!!!!!!

Slide 69

Slide 69 text

local → μΠφϛοΫείʔϓ (ݺͼग़͠ݩͰείʔϓ͕ܾ·Δ) my → ϨΩγΧϧείʔϓ (ιʔείʔυͷࣈ໘Ͱείʔϓ͕ܾ·Δ) package main; our $Foo = "A"; # ύοέʔδม਺ sub foo { print $Foo; } foo(); #= A { local $Foo = "B"; # ͜ͷϒϩοΫ͔Βݺ͹Ε͍ͯΔؒ͸B foo(); #= B } foo(); #= A ϒϩοΫΛൈ͚Δͱࣗಈతʹ໭Δ

Slide 70

Slide 70 text

Կճωετͯ͠ݺͼग़ͯ͠΋ॱ൪ʹ্ॻ͖ͯ͠ɺ΋ͲΕ͹෮ݩ foo(); #= A { local $Foo = "B"; foo(); #= B { local $Foo = "C"; foo(); #= C { local $Foo = "D"; foo() #= D } foo(); #= C } foo(); #= B } foo(); #= A

Slide 71

Slide 71 text

AWS::XRay Ͱ͸۩ମతʹ͸͜Μͳײ͡ ݱࡏͷ Trace-ID, Segment-ID Λ local Ͱอ࣋ package AWS::XRay; our $TRACE_ID; # Root our $SEGMENT_ID; # ݱࡏͷSegmentID sub capture { my ($name, $code) = @_; # $TRACE_ID͕ະఆٛͳΒRootͳͷͰ৽نIDൃߦ local ʹ local $TRACE_ID = $TRACE_ID // new_trace_id(); # ৽͍͠ηάϝϯτΛ࡞Δ # $SEGMENT_ID ͕͋Ε͹ parent_id => $SEGMENT_ID ʹͳ͍ͬͯΔ my $segment = AWS::XRay::Segment->new({ name => $name, type => $SEGMENT_ID ? "subsegment" : undef, parent_id => $SEGMENT_ID, });

Slide 72

Slide 72 text

local $SEGMENT_ID = $segment->{id}; # ৽͍͠ηάϝϯτIDΛ্ॻ͖ͯ͠localʹଋറ # ίʔυΛ࣮ߦ # ͜ͷதͰ͸ $SEGMENT_ID ͕ઃఆ͞Ε͍ͯΔͷͰ # ͞Βʹ capture ͨ͠Β $segment ͷࢠڙʹͳΔ $ret = $code->($segment); $segment->close(); # ηάϝϯτσʔλૹ৴ return $ret; # $segment ͸ফ͑Δ(my͔ͩΒ) # $SEGMENT_ID ΋ݺͼग़࣌͠ͷ஋ʹ໭Δ # RootͳΒ $TRACE_ID ΋ফ͑Δ(ݩ͕ະఆ͔ٛͩΒ) }

Slide 73

Slide 73 text

Perl ʹ͓͚Δ local ͦͷϒϩοΫ(౳)͔Βݺͼग़͞Ε͍ͯΔ͚ؒͩม਺ͷ஋Λมߋ ྫ֎Ͱ΋ԿͰ΋ൈ͚ͨΒݩʹ໭Δ Կஈ֊Ͱ΋ωετͰ͖Δ ݴޠͰαϙʔτ͞Ε͍ͯΔͷͰʮ࣮֬ʹʯ໭Δ Perl ࠷ߴศརʂʂ

Slide 74

Slide 74 text

༨ஊ Go Ͱ X-Ray SDK Λ࢖͏৔߹ ϦΫΤετͰൃੜͨ͠ context.Context ΛͲ͜·Ͱ΋ Ҿ਺ʹ౉͍͔ͯ͠ͳ͍ͱτϨʔεͰ͖ͳ͍ ్தʹ ctx ΛҾ͖ܧ͛ͳ͍ϥΠϒϥϦ(ORMͱ͔)͕ڬ·Δͱ (Isubank ΋࣮ࡍ͸40ՕॴҎ্ ctx Λ౉͢Α͏ʹॻ͖׵͑ͨ)

Slide 75

Slide 75 text

! ίετͷ࿩

Slide 76

Slide 76 text

! ίετͷ࿩ ʮX-Ray ͸Αͦ͞͏͚ͩͲɺ͓ߴ͍ΜͰ͠ΐ͏…?ʯ ௚઀ίετ = X-Ray ར༻ྉۚ ؒ઀ίετ = ΞϓϦέʔγϣϯͷύϑΥʔϚϯεྼԽ (݁Ռతʹ࢖༻Ϧιʔε͕૿͑ΔɺϢʔβମݧ͕ଛͳΘΕΔͳͲ)

Slide 77

Slide 77 text

ΞϓϦέʔγϣϯͷύϑΥʔϚϯεྼԽ τϨʔεΛ૊ΈࠐΉ͜ͱʹΑͬͯͲΕ͙Β͍஗͘ͳΔ͔ • 1ॲཧͰ100ݸͷ capture (ίʔυ͸ۭ sub{}) Λੜ੒ • αϯϓϦϯά͠ͳ͍ͰશͯͷτϨʔεΛૹ৴ Λ 500/sec ఔ౓࣮ߦͰ͖Δ (macbook pro 13 2017) ͭ·Γ1ॲཧ͋ͨΓ2msఔ౓ͷΦʔόʔϔου τϨʔεΛૹ৴͠ͳ͍(captureΛωετ͢Δ͚ͩ)ͳΒ 10,000/sec, 0.1msఔ౓ͷΦʔόʔϔου

Slide 78

Slide 78 text

ΞϓϦέʔγϣϯͷύϑΥʔϚϯεྼԽ ͍͍ͨͯͷWebΞϓϦέʔγϣϯʹରͯ͠͸໰୊ͳ͍଎౓ ࣮ࡍʹ͸αϯϓϦϯάΛߦ͏ͷͰߋʹӨڹ͸ܰඍ αϯϓϦϯά1%Ͱຊ൪౤ೖͯ͠ύϑΥʔϚϯε΁ͷӨڹͳ͠ ࣮ࡍͷܭଌίʔυ͸ xt/01_overhead.t10 ࢀর 10 https://github.com/fujiwara/AWS-XRay/blob/master/xt/01_overhead.t

Slide 79

Slide 79 text

X-Ray ར༻ྉۚ ຖ݄ͷແྉ࿮ τϨʔεͷه࿥͸ 10 ສճ·Ͱແྉ τϨʔεͷऔಘͱεΩϟϯ͸߹Θͤͯ 100 ສճ·Ͱແྉ ௥ՃͰ τϨʔεͷه࿥ 100 ສ݅͋ͨΓ 5 USD τϨʔεͷऔಘͱεΩϟϯΛ߹Θͤͯ 100 ສ݅͋ͨΓ 0.50 USD

Slide 80

Slide 80 text

࣮ࡍͲΕ͙Βֻ͍͔Δͷʁ ฏۉ 100 req/sec ͷ৔߹ 100 * 86,400 * 30 / 1,000,000 * 5 USD = 1,296 USD ฏۉ 1,000 req/sec ͳΒ 12,960 USD / month શͯͷϦΫΤετʹ͍ͭͯه࿥͢Δͷ͸ίετ͕ߴ͍

Slide 81

Slide 81 text

αϯϓϦϯά͢Δ ద౰ʹαϯϓϦϯά͢Ε͹܏޲͸௫ΊΔ ͋·Γൃੜ͠ͳ͍Τϥʔɺslowdown ࣌ͷௐࠪʹ͸࢖͍ͮΒ͍

Slide 82

Slide 82 text

αϯϓϦϯάͷ޻෉ ྫɿ1ඵ͝ͱʹ࠷ॳͷϦΫΤετͱ ௥ՃϦΫΤετͷ5%Λه࿥ (ެࣜSDKͷσϑΥϧτઃఆ) 100 req/sec ͷ৔߹ 1 + 100 * 5% = 6 req/sec ෼͕ه࿥͞ΕΔ ϦΫΤετ͕গͳͯ͘΋ 1 req/sec ͸࣮֬ʹه࿥ ެࣜ SDK ͸ X-Ray console ͔ΒαϯϓϦϯάϧʔϧΛઃఆՄ (AWS::XRay Ͱ͸ະରԠ…)

Slide 83

Slide 83 text

X-Ray console ͰͷαϯϓϦϯάઃఆ

Slide 84

Slide 84 text

Plack::Middleware::XRay ͰͷαϯϓϦϯάઃఆ ݻఆϨʔτ enable "XRay" name => "myApp", sampling_rate => 0.05; # 5%

Slide 85

Slide 85 text

αϯϓϦϯάϧʔϧΛίʔυͰهड़ PSGI $env Λड͚औͬͯਅِΛฦ͢ίʔυΛࢦఆͰ͖Δ enable "XRay" name => "myApp", sampler => sub { my $env = shift; state %paths; if ( $paths{$env->{PATH_INFO}++ == 0 ) { # ಛఆͷPATHΛॳΊͯड৴ͨ͠৔߹͸ඞͣه࿥ return 1; } rand() < 0.05; # ͦΕҎ֎͸ 5% };

Slide 86

Slide 86 text

Τϥʔ΍஗͍ϦΫΤετ͸ඞͣه࿥͍ͨ͠ 5% αϯϓϦϯάˠ ΤϥʔΛه࿥Ͱ͖Δ֬཰΋5% શϦΫΤετͰϝϞϦ্ʹτϨʔεΛอଘ͓ͯ͘͠ ϨεϙϯεΛฦ࣌͢఺ͰτϨʔεΛૹ৴͢Δ͔Ͳ͏͔ΛܾΊΔ enable "XRay" name => "myApp", response_filter => sub { my ($env, $res, $elapsed) = @_; # 5xx OR 1ඵҎ্ֻ͔ͬͨ৔߹ʹૹ৴ return $res->[0] >= 500 || $elapsed >= 1; };

Slide 87

Slide 87 text

·ͱΊ

Slide 88

Slide 88 text

·ͱΊ ෼ࢄτϨʔγϯά͸ύϑΥʔϚϯεɺো֐ղੳʹେม༗༻ ର ISUCON ༻ܾઓฌثͱͯ͠΋༗༻ AWS X-Ray ͸ϚωʔδυͰಋೖ͕༰қ αϯϓϦϯάઃఆʹ͸ཁ஫ҙ ! Perl SDK ͸ͳ͔ͬͨͷͰ AWS::XRay Λ࡞ͬͨ पลϞδϡʔϧؚΊͯ΋਺ඦߦͰ͍͍͔Μ͡ʹ